SAP ABAP VARIOS
From SapWiki
Contents
- 1 UUID ver nota 935047 - Creating and using GUIDs (UUIDs)
- 2 Grabar XSTRING en equipo como Binario(XML,PDF,etc.)
- 3 Bajar tabla en formato CSV (función hr_cl_if_prvd_generate_file)
- 4 Leer archivo CSV
- 5 Leer archivo Excel
- 6 Leer archivo XML
- 7 Obtener Texto según Valor de dominio
- 8 POPUP_TO_CONFIRM
- 9 F4IF_INT_TABLE_VALUE_REQUEST
- 10 SHA1 encode
- 11 BASE 64
- 12 Leer set de datos
- 13 Eliminar historial para campo especifico
- 14 Encontrar BADI para transacción
- 15 Comprimir String (GZIP)
- 16 Crear Datos SFLIGHT
UUID ver nota 935047 - Creating and using GUIDs (UUIDs)
FORM generar_iddocumento_2 CHANGING p_resultado. DATA lcl_request_id TYPE guid_32. TRY. lcl_request_id = cl_system_uuid=>create_uuid_c32_static( ). CATCH cx_uuid_error. ASSERT 1 = 0. ENDTRY. MOVE lcl_request_id TO p_resultado. ENDFORM.
Grabar XSTRING en equipo como Binario(XML,PDF,etc.)
CONCATENATE ls_dir_d '\' i_vbeln '.xml' INTO ls_fullpath.
CALL METHOD cl_faa_tenv_services=>gui_download_xstring EXPORTING id_fullpath = ls_fullpath id_xstring = i_xml.
IF sy-subrc <> 0.
ENDIF.
Bajar tabla en formato CSV (función hr_cl_if_prvd_generate_file)
FUNCTION hr_cl_if_prvd_generate_file. *"---------------------------------------------------------------------- *"*"Local Interface: *" TABLES *" IT_DATA TYPE STANDARD TABLE *"---------------------------------------------------------------------- FIELD-SYMBOLS: <f_source>. TYPES data_c(4096) TYPE c. TYPES t_data_c TYPE data_c OCCURS 0. CONSTANTS: c_field_separator TYPE c VALUE ';', c_darl_number(12) TYPE c VALUE '1234567890 '. DATA: lt_data_csv TYPE t_data_c, l_struc_index TYPE syindex, l_len_string TYPE i, l_max_field TYPE data_c, l_struc_raw_data TYPE data_c, l_start_string TYPE i, l_len_field_sep TYPE i, l_type, l_date_extern(30) TYPE c, l_help_id LIKE tline-tdline, l_tabix TYPE sytabix. l_len_field_sep = STRLEN( c_field_separator ). LOOP AT it_data. CLEAR: l_struc_index, l_len_string, l_struc_raw_data, l_start_string. DO. ADD 1 TO l_struc_index. CLEAR l_len_string. ASSIGN COMPONENT l_struc_index OF STRUCTURE it_data TO <f_source>. IF sy-subrc <> '0'. EXIT. ELSE. * * special processing when field is type data DESCRIBE FIELD <f_source> TYPE l_type. CASE l_type. WHEN 'P'. WRITE <f_source> TO l_date_extern. CATCH SYSTEM-EXCEPTIONS conversion_errors = 4. IF sy-subrc <> '0'. MESSAGE e899(ux) WITH l_type <f_source> l_help_id l_tabix RAISING conversion_failed. ENDIF. ENDCATCH. ASSIGN l_date_extern TO <f_source>. WHEN 'X'. ASSIGN COMPONENT l_struc_index OF STRUCTURE it_data TO <f_source> TYPE 'C'. WRITE <f_source> TO l_date_extern. ASSIGN l_date_extern TO <f_source>. WHEN 'T'. CLEAR l_date_extern. l_date_extern(2) = <f_source>(2). l_date_extern+2(1) = ':'. l_date_extern+3(2) = <f_source>+2(2). l_date_extern+5(1) = ':'. l_date_extern+6(2) = <f_source>+4(2). ASSIGN l_date_extern TO <f_source>. WHEN 'D'. CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL' EXPORTING date_internal = <f_source> IMPORTING date_external = l_date_extern EXCEPTIONS OTHERS = 4. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 RAISING conversion_failed. ELSE. ASSIGN l_date_extern TO <f_source>. ENDIF. WHEN 'F'. CALL FUNCTION 'FLTP_CHAR_CONVERSION' EXPORTING input = <f_source> IMPORTING flstr = l_date_extern EXCEPTIONS OTHERS = 4. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 RAISING conversion_failed. ELSE. ASSIGN l_date_extern TO <f_source>. ENDIF. WHEN OTHERS. ENDCASE. * remember der seperation character! IF l_struc_index <> 1. l_max_field+l_len_string(l_len_field_sep) = c_field_separator. l_len_string = l_len_field_sep. ENDIF. IF <f_source> CO c_darl_number. CONDENSE <f_source> NO-GAPS. ELSE. CONDENSE <f_source>. ENDIF. l_max_field+l_len_string = <f_source>. l_len_string = STRLEN( l_max_field ). CHECK l_len_string <> '0'. l_struc_raw_data+l_start_string(l_len_string) = l_max_field. l_start_string = l_start_string + l_len_string. ENDIF. ENDDO. IF NOT l_struc_raw_data IS INITIAL. APPEND l_struc_raw_data TO lt_data_csv. ENDIF. ENDLOOP. CALL FUNCTION 'HR_99S_DOWNLOAD' EXPORTING p_initial_directory = 'C:\' p_filename = 'PREVIRED.txt' p_filetype = 'ASC' p_write_lf = 'X' p_trunc_blanks_eol = 'X' p_trunc_blanks = ' ' TABLES data_tab = lt_data_csv EXCEPTIONS file_save_dialog = 1 file_write_error = 2 no_batch = 3 gui_refuse_filetransfer = 4 invalid_type = 5 unknown_error = 6 wrong_tab_format = 7 OTHERS = 8. IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ENDFUNCTION.
Leer archivo CSV
REPORT ytest_csv. TYPE-POOLS truxs. TYPES: BEGIN OF ty_data, col01(100), col02(100), col03(100), col04(100), col05(100), col06(100), col07(100), col08(100), col09(100), col10(100), col11(100), col12(100), col13(100), col14(100), col15(100), col16(100), col17(100), col18(100), col19(100), col20(100), col21(100), col22(100), col23(100), col24(100), col25(100), col26(100), col27(100), col28(100), col29(100), END OF ty_data. DATA gt_data TYPE TABLE OF ty_data WITH HEADER LINE. DATA : rawdata TYPE truxs_t_text_data. *--------------------------------------------------------------------* * *--------------------------------------------------------------------* PARAMETERS: p_file TYPE string LOWER CASE. *--------------------------------------------------------------------* AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. *--------------------------------------------------------------------* CALL FUNCTION 'GUI_FILE_LOAD_DIALOG' IMPORTING FULLPATH = p_file . *--------------------------------------------------------------------* START-OF-SELECTION. *--------------------------------------------------------------------* PERFORM get_data. *--------------------------------------------------------------------* END-OF-SELECTION. *--------------------------------------------------------------------* *&---------------------------------------------------------------------* *& Form get_data *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM get_data. DATA ld_filename TYPE string. ld_filename = p_file. CALL FUNCTION 'GUI_UPLOAD' EXPORTING filename = ld_filename filetype = 'ASC' TABLES data_tab = rawdata EXCEPTIONS file_open_error = 1 file_read_error = 2 no_batch = 3 gui_refuse_filetransfer = 4 invalid_type = 5 no_authority = 6 unknown_error = 7 bad_data_format = 8 header_not_allowed = 9 separator_not_allowed = 10 header_too_long = 11 unknown_dp_error = 12 access_denied = 13 dp_out_of_memory = 14 disk_full = 15 dp_timeout = 16 OTHERS = 17. IF sy-subrc <> 0. EXIT. ENDIF. DATA ls_rawdata LIKE LINE OF rawdata. CALL FUNCTION 'TEXT_CONVERT_CSV_TO_SAP' EXPORTING i_field_seperator = ';' i_tab_raw_data = rawdata TABLES i_tab_converted_data = gt_data EXCEPTIONS conversion_failed = 1 OTHERS = 2. BREAK-POINT. ENDFORM. "get_data
Leer archivo Excel
REPORT YTEST_SUBIR_EXCEL. DATA: gt_data TYPE TABLE OF T001, l_i_tab_raw_data TYPE truxs_t_text_data, gd_tfile TYPE IBIPPARMS-PATH. PARAMETERS p_file type string. *--------------------------------------------------------------------* AT selection-SCREEN ON VALUE-request FOR p_file. *--------------------------------------------------------------------* gd_tfile = p_file. CALL FUNCTION 'F4_FILENAME' EXPORTING field_name = 'P_FILE' IMPORTING file_name = gd_tfile. MOVE gd_tfile TO p_file. *--------------------------------------------------------------------* START-OF-SELECTION. *--------------------------------------------------------------------* data l_file type RLGRAP-FILENAME. move p_file to l_file. * Read data from Excel shet CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP' EXPORTING i_tab_raw_data = l_i_tab_raw_data i_filename = l_file TABLES i_tab_converted_data = gt_data EXCEPTIONS conversion_failed = 1 OTHERS = 2. IF sy-subrc <> 0. case sy-subrc. when 1. MESSAGE i000(0k) WITH 'Error en Cargar Archivo.' 'Favor de validar que archivo' 'no este abierto por otra aplicación' DISPLAY LIKE 'E'. when OTHERS. MESSAGE i000 WITH 'Operación Cancelada' DISPLAY LIKE 'W'. ENDCASE. RETURN. ENDIF.
Leer archivo XML
REPORT YLEE_XML. data gt_data type TABLE OF string. data: g_string type string, g_xstring type xstring. PARAMETERS p_file type string LOWER CASE OBLIGATORY. *--------------------------------------------------------------------* at SELECTION-SCREEN on VALUE-REQUEST FOR p_file. *--------------------------------------------------------------------* data: lt_file_tab type STANDARD TABLE OF sdokpath, ls_file_tab type sdokpath. CALL FUNCTION 'TMP_GUI_FILE_OPEN_DIALOG' TABLES file_table = lt_file_tab EXCEPTIONS CNTL_ERROR = 1 OTHERS = 2 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. else. READ TABLE lt_file_tab into ls_file_tab index 1. p_file = ls_file_tab. ENDIF. *--------------------------------------------------------------------* START-OF-SELECTION. *--------------------------------------------------------------------* CALL FUNCTION 'GUI_UPLOAD' EXPORTING filename = p_file FILETYPE = 'ASC' tables data_tab = gt_data EXCEPTIONS FILE_OPEN_ERROR = 1 FILE_READ_ERROR = 2 NO_BATCH = 3 GUI_REFUSE_FILETRANSFER = 4 INVALID_TYPE = 5 NO_AUTHORITY = 6 UNKNOWN_ERROR = 7 BAD_DATA_FORMAT = 8 HEADER_NOT_ALLOWED = 9 SEPARATOR_NOT_ALLOWED = 10 HEADER_TOO_LONG = 11 UNKNOWN_DP_ERROR = 12 ACCESS_DENIED = 13 DP_OUT_OF_MEMORY = 14 DISK_FULL = 15 DP_TIMEOUT = 16 OTHERS = 17 IF sy-subrc <> 0. * Implement suitable error handling here return. ENDIF. loop at gt_data into data(ls_data). if sy-tabix = 1. g_string = ls_data. else. CONCATENATE g_string ls_data into g_string. endif. ENDLOOP. * encodings se puede ver en ayuda busqueda H_TCP00 CALL FUNCTION 'SCMS_STRING_TO_XSTRING' EXPORTING text = g_string * MIMETYPE = ' ' ENCODING = '1100' "ISO-8859-1 "'4110' " utf-8 IMPORTING BUFFER = g_xstring EXCEPTIONS FAILED = 1 OTHERS = 2 . IF sy-subrc <> 0. * Implement suitable error handling here ENDIF. call function 'DISPLAY_XML_STRING' EXPORTING xml_string = g_xstring.
Obtener Texto según Valor de dominio
FORM get_domvalue USING p_domname p_value CHANGING p_text. DATA: l_domvalue TYPE dd07v-domvalue_l, l_ddtext TYPE dd07v-ddtext. l_domvalue = p_value. CALL FUNCTION 'DOMAIN_VALUE_GET' EXPORTING i_domname = p_domname i_domvalue = l_domvalue IMPORTING e_ddtext = l_ddtext EXCEPTIONS not_exist = 1 OTHERS = 2. IF sy-subrc = 0. MOVE l_ddtext TO p_text. ELSE. CLEAR p_text. ENDIF. ENDFORM. "get_domvalue
POPUP_TO_CONFIRM
data g_answer(1). CALL FUNCTION 'POPUP_TO_CONFIRM' EXPORTING titlebar = 'Se actualizaran Documentos en el Sistema'(p01) text_question = '¿Desea Continuar?'(p02) text_button_1 = 'Sí'(p03) text_button_2 = 'No'(p04) default_button = 2 display_cancel_button = c_true IMPORTING answer = g_answer.
IF g_answer <> '1'. RETURN. ENDIF.
F4IF_INT_TABLE_VALUE_REQUEST
TYPES: BEGIN OF ty_subty, subty TYPE t554s-subty, atext TYPE t554t-atext, END OF ty_subty. DATA gt_subty TYPE TABLE OF ty_subty. SELECT-OPTIONS: s_pernr FOR wa_data-pernr MATCHCODE OBJECT prem, s_awart FOR p2001-awart. *--------------------------------------------------------------------* AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_awart-low. *--------------------------------------------------------------------* PERFORM f4_subty CHANGING s_awart-low. *----------------------------------------------------------------------* * FORM ....... *----------------------------------------------------------------------* FORM f4_subty CHANGING p_value. DATA lt_return TYPE TABLE OF ddshretval WITH HEADER LINE. CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST' EXPORTING retfield = 'SUBTY' value_org = 'S' window_title = 'Clase de Absentismo' TABLES value_tab = gt_subty return_tab = lt_return. READ TABLE lt_return INDEX 1. IF sy-subrc = 0. MOVE lt_return-fieldval TO p_value. ENDIF. ENDFORM.
SHA1 encode
CALL FUNCTION 'CALCULATE_HASH_FOR_CHAR' EXPORTING * ALG = 'SHA1' data = l_firma IMPORTING hashstring = l_hashstring EXCEPTIONS unknown_alg = 1 param_error = 2 internal_error = 3 OTHERS = 4. IF sy-subrc <> 0. * Implement suitable error handling here ENDIF.
BASE 64
desde un string en BASE64 obtener PDF binario
FORM get_pdf_bin USING p_archivo_b64 TYPE string CHANGING p_data_tab TYPE solix_tab p_xstring TYPE xstring. DATA lt_bin TYPE TABLE OF solix. DATA l_output_length TYPE i. PERFORM decode_base_64x USING p_archivo_b64 CHANGING p_xstring. CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' EXPORTING buffer = p_xstring IMPORTING output_length = l_output_length TABLES binary_tab = lt_bin. "p_data_tab. p_data_tab[] = lt_bin[]. ENDFORM. " GET_PDF_BIN FORM decode_base_64x USING p_string_base64 TYPE string CHANGING p_xstring TYPE xstring. DATA: l_http_utility TYPE REF TO cl_http_utility. CREATE OBJECT l_http_utility. CALL METHOD l_http_utility->decode_x_base64 EXPORTING encoded = p_string_base64 RECEIVING decoded = p_xstring. ENDFORM.
Leer set de datos
- Set de datos : transacciones GS02 GS03
- funciones: G_SET_GET_ID_FROM_NAME --> G_SET_FETCH
data: ra_kdgrp type RANGE OF Knvv-KDGRP, wa_kdrgp LIKE LINE OF ra_kdgrp, l_aux type Knvv-KDGRP VALUE '85', l_setid type SETHIER-SETID, lt_lines type TABLE OF RGSBV WITH HEADER LINE.
CALL FUNCTION 'G_SET_GET_ID_FROM_NAME' EXPORTING SHORTNAME = 'ZSD_AAA_001' IMPORTING NEW_SETID = l_setid EXCEPTIONS NO_SET_FOUND = 1 NO_SET_PICKED_FROM_POPUP = 2 WRONG_CLASS = 3 WRONG_SUBCLASS = 4 TABLE_FIELD_NOT_FOUND = 5 FIELDS_DONT_MATCH = 6 SET_IS_EMPTY = 7 FORMULA_IN_SET = 8 SET_IS_DYNAMIC = 9 OTHERS = 10 . IF SY-SUBRC = 0. * Implement suitable error handling here CALL FUNCTION 'G_SET_FETCH' EXPORTING SETNR = l_setid TABLES SET_LINES_BASIC = lt_lines EXCEPTIONS NO_AUTHORITY = 1 SET_IS_BROKEN = 2 SET_NOT_FOUND = 3 OTHERS = 4 . IF SY-SUBRC = 0. * Implement suitable error handling here loop at lt_lines. wa_kdrgp-sign = 'I'. wa_kdrgp-option = 'BT'. wa_kdrgp-low = lt_lines-from. wa_kdrgp-high = lt_lines-to. append wa_kdrgp to ra_kdgrp. ENDLOOP. ENDIF.
if l_aux in ra_kdgrp. write 'OK'. endif. ENDIF.
Eliminar historial para campo especifico
data l_rc type i.
CALL METHOD cl_gui_frontend_services=>disablehistoryforfield EXPORTING fieldname = 'SVALD-VALUE' bdisabled = 'X' changing rc = l_rc EXCEPTIONS field_not_found = 1 disablehistoryforfield_failed = 2 cntl_error = 3 unable_to_disable_field = 4 others = 5 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF.
Encontrar BADI para transacción
- Ir a TR SE24 e ingresar CL_EXITHANDLER .
- Ir a 'Methods' tab.
- Doble click en método 'Get Instance'.
- Colocar un breakpoint en 'CALL METHOD cl_exithandler => get_class_name_by_interface'.
- Correr transacción correspondiente.
- Al parar transacción, ver valor de campo 'EXIT_NAME' que corresponde a la BADI.
Comprimir String (GZIP)
DATA: l_text_in TYPE string, l_text_out TYPE xstring, l_zip_length TYPE i.
TRY. tp_text_in = 'El corona virus me tiene chato'. cl_abap_gzip=>compress_text( EXPORTING text_in = l_text_in IMPORTING gzip_out = l_text_out gzip_out_len = l_zip_length ).
CATCH cx_parameter_invalid_range. CATCH cx_sy_buffer_overflow. CATCH cx_sy_conversion_codepage. CATCH cx_root. ENDTRY.
Crear Datos SFLIGHT
Sap provee de un modelo de datos, basado en aerolineas, para sus programas demo o training, para generar datos usar programa SAPBC_DATA_GENERATOR