"Sometimes we receive files sent through interfaces in Base64 format. In this case, the requirement was to take the file that the interface saves in a Z table, decode it, and then upload it as an attachment to a purchase order (PO) in SAP."
Here is a program that performs this operation in a simple and detailed way.
In this program, I have Base64 data stored in a table that is populated through an IDOC interface. It stores large Base64 data in a Z table, with the purchase order as the key field.
In the program, I group all the Base64 lines for each purchase order into the variable `lv_base64`. Once I have the Base64 data for a purchase order grouped in the variable, I convert it to xstring in the variable `lv_x`.
I then execute the `SAVE` method, which uploads this data as an attachment in the specified format to the purchase order, according to the data in the structure `ls_por`.
**&---------------------------------------------------------------------*
**& Report ZMM_IDOC_EXCEL_OPER
**&---------------------------------------------------------------------*
**& *DECODIFICA EXCEL Y ADJUNTA A OC
**&---------------------------------------------------------------------*
REPORT zmm_idoc_excel_OPER.
TABLES: ekko.
DATA lv_base64_decode TYPE xstring.
DATA lv_resultado TYPE string.
DATA ls_OPER TYPE soli.
DATA lv_string TYPE string.
DATA lv_base64 TYPE string.
DATA lv_x TYPE xstring .
DATA lv_mensaje TYPE string.
DATA lt_BAPIRETTAB TYPE bapirettab.
DATA lo TYPE REF TO cl_fitv_gos.
DATA ls_por TYPE sibflporb.
SELECT-OPTIONS: so_ebeln FOR ekko-ebeln NO INTERVALS . "Business document number
PARAMETERS: p_del AS CHECKBOX.
START-OF-SELECTION.
"Tabla donde estan mis datos base64
SELECT * INTO TABLE @DATA(lt_anexo) FROM zidoc_OPER_exc AS a
WHERE a~ebeln IN @so_ebeln .
IF sy-subrc = 0.
SORT lt_anexo BY ebeln cont .
LOOP AT lt_anexo INTO DATA(ls_anexo).
CONCATENATE lv_base64 ls_anexo-anexo INTO lv_base64.
AT END OF ebeln .
DATA(lv_fin) = abap_true.
ENDAT.
IF lv_fin IS NOT INITIAL.
ls_por-instid = ls_anexo-ebeln.
ls_por-typeid = 'BUS2012'. "The BO for modifying Purchase Orders.
ls_por-catid = 'BO'.
CALL METHOD cl_http_utility=>if_http_utility~decode_x_base64
EXPORTING
encoded = lv_base64
RECEIVING
decoded = lv_x.
CREATE OBJECT lo.
CALL METHOD lo->save
EXPORTING
iv_name = 'File.XLS'
iv_content_hex = lv_x
is_lporb = ls_por
iv_objtp = 'EXT'
iv_commit_on = 'X'
RECEIVING
rt_messages = lt_BAPIRETTAB.
READ TABLE lt_BAPIRETTAB INTO DATA(ls_ret) WITH KEY type = 'E'.
IF sy-subrc = 0.
CONCATENATE ls_ret-message ls_ret-message_v1 ls_ret-message_v2 ls_ret- message_v3 ls_ret-message_v4 INTO lv_mensaje.
ELSE.
CONCATENATE 'Attachment.' ls_anexo-ebeln 'Created' INTO lv_mensaje SEPARATED BY space.
ENDIF.
WRITE lv_mensaje .
CLEAR: lv_fin, lv_base64, lv_mensaje, lv_x, ls_por.
FREE lo.
REFRESH lt_BAPIRETTAB.
ENDIF.
ENDLOOP.
IF p_del IS NOT INITIAL.
DELETE zidoc_OPER_exc FROM TABLE lt_anexo.
COMMIT WORK.
ENDIF.
ENDIF.
I’m providing the table structure to facilitate understanding of the code.
Good luck!..
Comentarios
Publicar un comentario