SAP ABAP HCM: Test and Upload Data to Decoupled Infotype

Below is sample program to test and upload a record in a decoupled infotype. Methods ‘test_infty9000_entry’ and ‘execute_infty9000_entry’ will have code specific to handle the decoupled infotype.

ABAP
REPORT zhrpy_upload_infty. 

*--------------------------------------------------------------------* 
* CLASS 
*--------------------------------------------------------------------* 

CLASS lcl_main DEFINITION FINAL. 
  PUBLIC SECTION. 
    CONSTANTS: 
      c_icon_green  TYPE icon_d VALUE '@08@', 
      c_icon_yellow TYPE icon_d VALUE '@09@', 
      c_icon_red    TYPE icon_d VALUE '@0A@'. 

    TYPES: 
      ty_s_xlsx_layout TYPE zhrpy_st_9000_xlsx_layout, 
      ty_t_xlsx_layout TYPE zhrpy_tt_9000_xlsx_layout, 

      BEGIN OF ty_s_message, 
        status_icon TYPE icon_d, 
        message	    TYPE bapi_msg, 
        pernr       TYPE  ty_s_xlsx_layout-pernr, 
*        subty       TYPE  ty_s_xlsx_layout-subty, 
*        objps       TYPE  ty_s_xlsx_layout-objps, 
*        sprps       TYPE  ty_s_xlsx_layout-sprps, 
        begda       TYPE  ty_s_xlsx_layout-begda, 
        endda       TYPE  ty_s_xlsx_layout-endda, 
*        seqnr       TYPE  ty_s_xlsx_layout-seqnr, 
        cate        TYPE  categ, 
*        type        TYPE bapi_mtype, 
*        id          TYPE symsgid, 
*        number      TYPE symsgno, 
*        message      TYPE bapi_msg, 
*        log_no      TYPE balognr, 
*        log_msg_no  TYPE balmnr, 
      END OF ty_s_message, 

      ty_t_alv_layout      TYPE STANDARD TABLE OF ty_s_message WITH DEFAULT KEY, 
      ty_o_message_handler TYPE REF TO cl_hrpa_message_list. 

    DATA: 
          a_t_message         TYPE ty_t_alv_layout. 

*    CLASS-METHODS: 

*      class_constructor. 

    METHODS: 

      constructor 
        IMPORTING 
          im_p_lclpth TYPE string 
          im_p_srvpth TYPE string 
          im_p_test   TYPE flag 
          im_p_ovrwrt TYPE flag, 
      upload, 
      show_log. 

*  PROTECTED SECTION. 

  PRIVATE SECTION. 

    DATA: 
      a_lclpth            TYPE string, 
      a_srvpth            TYPE string, 
      a_ovrwrt            TYPE flag, 
      a_test              TYPE flag, 
      a_o_xlsx_data_typed TYPE REF TO data. 

    METHODS: 
      process_upload, 
      validate_population 
        IMPORTING 
                  im_pernr       TYPE persno 
                  im_begda       TYPE begda 
                  im_endda       TYPE endda 
        RETURNING VALUE(rv_flag) TYPE boole_d, 

      search_future_infty9000 
        IMPORTING 
                  im_pernr       TYPE persno 
                  im_begda       TYPE begda 
                  im_endda       TYPE endda 
        RETURNING VALUE(rv_flag) TYPE boole_d, 

      test_infty9000_entry 
        IMPORTING 
          im_s_xlsx_record TYPE ty_s_xlsx_layout, 

      execute_infty9000_entry 
        IMPORTING 
          im_s_xlsx_record TYPE ty_s_xlsx_layout, 

      add_msg 
        IMPORTING 
          im_s_msg             TYPE ty_s_message 
          im_o_message_handler TYPE ty_o_message_handler OPTIONAL 
          im_s_bapireturn1     TYPE bapireturn1 OPTIONAL. 

ENDCLASS. 

 
CLASS lcl_main IMPLEMENTATION. 


*  METHOD class_constructor. 

*  ENDMETHOD. 

  METHOD constructor. 

    CREATE DATA: 
       a_o_xlsx_data_typed TYPE ty_t_xlsx_layout. "zhrpy_tt_0008_xlsx_layout. 

    a_lclpth = im_p_lclpth. 
    a_srvpth = im_p_srvpth. 
    a_test   = im_p_test. 
    a_ovrwrt = im_p_ovrwrt. 

  ENDMETHOD. 

  METHOD process_upload. 

    FIELD-SYMBOLS: 
       <lft_xlsx_itab_typed> TYPE ty_t_xlsx_layout. 

    DATA: 
      ls_return  TYPE bapireturn1, 
      ls_message TYPE ty_s_message, 
      lv_error   TYPE boole_d. 

    ASSIGN a_o_xlsx_data_typed->* TO <lft_xlsx_itab_typed>. 

    LOOP AT <lft_xlsx_itab_typed> 
      ASSIGNING FIELD-SYMBOL(<lfs_xlsx_record>). 

      "When editing line then removing information, xlsx uploads a blank line. 
      CHECK <lfs_xlsx_record>-pernr IS NOT INITIAL. 

      CLEAR: 
        ls_message, ls_return, lv_error. 

      "Validate if employee is from zzz population 
      IF validate_population( im_pernr = <lfs_xlsx_record>-pernr 
                                        im_begda = <lfs_xlsx_record>-begda 
                                        im_endda = <lfs_xlsx_record>-endda ) = abap_false. 

        ls_message-pernr = <lfs_xlsx_record>-pernr. 
*        ls_message-subtype        = '0'. 
        ls_message-begda  = <lfs_xlsx_record>-begda. 
        ls_message-endda  = <lfs_xlsx_record>-endda. 
        ls_return-type    = 'E'. 
        ls_return-message = 'Error: Employee does not participate in Company Code zzz, Personnel subarea 0000.'(008). 

        me->add_msg( 
              im_s_msg         = ls_message 
              im_s_bapireturn1 = ls_return ). 

        lv_error = 'X'. 
      ENDIF. 

      "Validate if there are not future records 
      IF a_ovrwrt = abap_false. 
        IF search_future_infty9000( im_pernr = <lfs_xlsx_record>-pernr 
                                      im_begda = <lfs_xlsx_record>-begda 
*                                      im_endda = '99991231' ) = abap_true. 
                                      im_endda = <lfs_xlsx_record>-endda ) = abap_true. 

          MOVE-CORRESPONDING <lfs_xlsx_record> TO ls_message. 
          ls_return-type       = 'E'. 
          ls_return-message    = 'Error: Record in infotype 9000 already exists and it will be replaced by new entry. Upload canceled.'(009). 

          me->add_msg( 
                im_s_msg         = ls_message 
                im_s_bapireturn1 = ls_return ). 

          lv_error = 'X'. 
        ENDIF. 
      ENDIF. 

*      "Only process records without error 
      CHECK lv_error IS INITIAL. 

      IF a_test = abap_true. 
        me->test_infty9000_entry( im_s_xlsx_record = <lfs_xlsx_record> ). 
      ELSE. 
        me->execute_infty9000_entry( im_s_xlsx_record = <lfs_xlsx_record> ). 
      ENDIF. 
    ENDLOOP. 
  ENDMETHOD. 

  METHOD validate_population. 
    DATA: 
      lt_p0001 TYPE TABLE OF p0001. 

    TRY. 
        cl_hrpa_read_infotype=>get_instance( 
*         EXPORTING 
*           masterdata_buffer          =     " HR: Buffer for Access to Master Data 
          IMPORTING 
            infotype_reader            = DATA(lo_infty)    " Reading Infotype Records 
        ). 

        lo_infty->read( 
          EXPORTING 
            tclas                      = 'A'    " Transaction Class for Data Storage 
            pernr                      = im_pernr    " Personnel Number 
            infty                      = '0001'    " Infotype 
*           subty                      = '*'    " Subtype 
*           objps                      = '*'    " Object Identification 
*           sprps                      = '*'    " Lock Indicator for HR Master Data Record 
            begda                      = im_begda    " Valid from date 
            endda                      = im_endda    " Valid To Date 
            no_auth_check              = ''    " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 
          IMPORTING 
            infotype_tab               =  DATA(lt_infty_data)   " HR: Primary and Secondary Infotype and Acct Assignment Data 
*            data_exists                =  DATA(lv_data_found)   " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 
*            missing_auth               =  DATA(lv_authorization)   " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 
        ). 

*        CHECK lt_infty_data[] IS NOT INITIAL. 

        IF lt_infty_data[] IS INITIAL. 
          lo_infty->read( 
               EXPORTING 
                 tclas                      = 'A'    " Transaction Class for Data Storage 
                 pernr                      = im_pernr    " Personnel Number 
                 infty                      = '0001'    " Infotype 
*                subty                      = '*'    " Subtype 
*                objps                      = '*'    " Object Identification 
*                sprps                      = '*'    " Lock Indicator for HR Master Data Record 
                 begda                      = lo_infty->low_date   " Valid from date 
                 endda                      = lo_infty->high_date  " Valid To Date 
                 no_auth_check              = ''    " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 
               IMPORTING 
                 infotype_tab               =  lt_infty_data   " HR: Primary and Secondary Infotype and Acct Assignment Data 
*                 data_exists                =  lv_data_found   " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 
*                 missing_auth               =  lv_authorization   " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 
             ). 
        ENDIF. 

        CHECK lt_infty_data[] IS NOT INITIAL. 

        CALL METHOD cl_hr_pnnnn_type_cast=>prelp_to_pnnnn_tab 
          EXPORTING 
            prelp_tab = lt_infty_data 
          IMPORTING 
            pnnnn_tab = lt_p0001. 

        LOOP AT lt_p0001 
          INTO DATA(ls_p0001). 

          IF ls_p0001-bukrs = 'ZZZ' AND ls_p0001-btrtl = '0000'. 
            rv_flag = abap_true. 
            EXIT. 
          ENDIF. 
        ENDLOOP. 

      CATCH cx_hrpa_violated_assertion. 
        CLEAR rv_flag. 
    ENDTRY. 

  ENDMETHOD. 

  METHOD search_future_infty9000. 

    DATA: 
      lt_p9000 TYPE TABLE OF p9000. 

 

    TRY. 
        cl_hrpa_read_infotype=>get_instance( 
*         EXPORTING 
*           masterdata_buffer          =     " HR: Buffer for Access to Master Data 
          IMPORTING 
            infotype_reader            = DATA(lo_infty)    " Reading Infotype Records 
        ). 

        lo_infty->read( 
          EXPORTING 
            tclas                      = 'A'    " Transaction Class for Data Storage 
            pernr                      = im_pernr    " Personnel Number 
            infty                      = '9000'    " Infotype 
            subty                      = ''    " Subtype 
*           objps                      = '*'    " Object Identification 
*           sprps                      = '*'    " Lock Indicator for HR Master Data Record 
            begda                      = im_begda    " Valid from date 
            endda                      = im_endda    " Valid To Date 
            no_auth_check              = ''    " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 
          IMPORTING 
            infotype_tab               =  DATA(lt_infty_data)   " HR: Primary and Secondary Infotype and Acct Assignment Data 
*            data_exists                =  DATA(lv_data_found)   " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 
*            missing_auth               =  DATA(lv_authorization)   " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 
        ). 

        CHECK lt_infty_data[] IS NOT INITIAL. 

        CALL METHOD cl_hr_pnnnn_type_cast=>prelp_to_pnnnn_tab 
          EXPORTING 
            prelp_tab = lt_infty_data 
          IMPORTING 
            pnnnn_tab = lt_p9000. 


        LOOP AT lt_p9000 
          INTO DATA(ls_p9000). 

          IF ls_p9000-begda >= im_begda. 
            rv_flag = abap_true. 
            EXIT. 
          ENDIF. 

        ENDLOOP. 

      CATCH cx_hrpa_violated_assertion. 
        CLEAR rv_flag. 

    ENDTRY. 

  ENDMETHOD. 

  METHOD upload. 

    CALL METHOD zcl_xlsx_file=>retrieve_itab_from_xlsx_file 
      EXPORTING 
        im_lclpth = a_lclpth 
        im_srvpth = a_srvpth 
      CHANGING 
        ch_o_data = a_o_xlsx_data_typed. 

    me->process_upload( ). 
  ENDMETHOD. 

  METHOD test_infty9000_entry. 

    DATA: 
      ls_bapireturn1 TYPE bapireturn1, 
      ls_message     TYPE ty_s_message, 
      ls_p9000       TYPE p9000, 
      ls_prelp       TYPE prelp, 
      ls_prelp2      TYPE prelp, 
      ls_pref        TYPE pref. 

    DATA(lo_message_handler) = NEW cl_hrpa_message_list( ). 

    MOVE-CORRESPONDING im_s_xlsx_record TO: 
      ls_p9000, ls_message. 
      ls_p9000-infty = '9000'. 

    CALL METHOD cl_hr_pnnnn_type_cast=>pnnnn_to_prelp 
      EXPORTING 
        pnnnn = ls_p9000 
      IMPORTING 
        prelp = ls_prelp.    " HR Master Data Buffer 

    DATA(lo_infty_9000) = NEW zcl_hrpa_infotype_9000( tclas = 'A' infty = '9000' ). 

    TRY . 
        lo_infty_9000->if_hrpa_infty_legacy_adapter~check_insert( 
                  EXPORTING 
                    tclas                      = 'A'    " Transaction Class for Data Storage 
*                   massn                      =     " Action Type 
*                   massg                      =     " Reason for Action 
                    message_handler            = lo_message_handler  " HR Master Data: Messages 
                  IMPORTING 
                    is_ok                      = DATA(lv_ok)  " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 
                  CHANGING 
                    pnnnn                      = ls_prelp   " HR Master Data Buffer 
                    pnnnn2                     = ls_prelp2   " HR Master Data Buffer 
                    pref                       = ls_pref   " Assignment Values for HR Objects 
                                                                ). 
        IF lv_ok = abap_false OR 
          ( lv_ok = abap_true AND lo_message_handler->has_message( ) = abap_true ). 

          me->add_msg( 
            EXPORTING 
              im_s_msg             = ls_message 
              im_o_message_handler = lo_message_handler ). 

        ELSE. 
          ls_bapireturn1-type            = 'S'. 
          ls_bapireturn1-message         = 'Simulation performed successfully.'(003). 

          me->add_msg( 
                im_s_msg         = ls_message 
                im_s_bapireturn1 = ls_bapireturn1 ). 

        ENDIF. 

      CATCH cx_hrpa_violated_assertion INTO DATA(lo_obj). 
        ls_bapireturn1-type            = 'E'. 
        ls_bapireturn1-message         = lo_obj->get_text( ). 

        me->add_msg( 
          EXPORTING 
            im_s_msg         = ls_message 
            im_s_bapireturn1 = ls_bapireturn1 ). 
 
    ENDTRY. 

  ENDMETHOD. 

  METHOD execute_infty9000_entry. 

    DATA: 
      ls_bapireturn1    TYPE bapireturn1, 
      ls_message        TYPE ty_s_message, 
      ls_pskey          TYPE pskey, 
*      lo_container_nnnn TYPE REF TO cl_hrpa_infotype_container, 
      lo_container_nnnn TYPE REF TO if_hrpa_infty_container_data, 
      ls_p9000          TYPE p9000. 

    DATA(lo_message_handler) = NEW cl_hrpa_message_list( ). 

    MOVE-CORRESPONDING im_s_xlsx_record TO: 
      ls_pskey, ls_p9000, ls_message. 

    ls_pskey-infty = '9000'. 
    ls_p9000-infty = '9000'. 

    TRY . 

        CALL METHOD cl_hrpa_masterdata_enq_deq=>enqueue_by_pernr 
          EXPORTING 
            tclas           = 'A'    " Transaction Class for Data Storage 
            pernr           = ls_p9000-pernr    " Personnel Number 
*           promote_to_exclusive = abap_true    " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 
            message_handler = lo_message_handler    " HR Master Data: Messages 
*           enq_scope       = '2'    " Control Lock Behavior When Calling the Update Task 
*           enq_wait        = SPACE    " Flag Indicating Whether Lock Attempt Is to Be Repeated 
*           enq_collect     = 'X'    " Indicator Whether Locks Should Only Be Collected First 
          IMPORTING 
            is_ok           = DATA(lv_ok)   " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 
*           locking_user    =     " SAP System, User Logon Name 
          . 

        IF lv_ok = abap_false. 
          me->add_msg( 
            EXPORTING 
              im_s_msg             = ls_message 
              im_o_message_handler = lo_message_handler ). 

          RETURN. 
*        ELSE. 
* 
*          ls_bapireturn1-type            = 'S'. 
*          ls_bapireturn1-message         = 'PERNR lock performed successfully.'(004). 
* 
*          me->add_msg( 
*                im_s_msg         = ls_message 
*                im_s_bapireturn1 = ls_bapireturn1 ). 

        ENDIF. 

        CALL METHOD cl_hrpa_masterdata_factory=>get_business_logic 
          IMPORTING 
            business_logic = DATA(lo_masterdata_bl). 

*        CALL METHOD cl_hrpa_masterdata_bl=>get_instance( 
*          IMPORTING 
*            masterdata_bl = DATA(lo_masterdata_bl) ). 

        lo_masterdata_bl->get_infty_container( 
          EXPORTING 
            tclas                      = 'A'    " Transaction Class for Data Storage 
            pskey                      = ls_pskey    " Keys for HR Master Data 
*           itbld                      =     " Infotype screen control 
*           massn                      =     " Action Type 
*           massg                      =     " Reason for Action 
*           no_auth_check              =     " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 
            message_handler            = lo_message_handler   " HR Master Data: Messages 
          IMPORTING 
            container                  = DATA(lo_container)    " HR: Master Data, Reference to Infotype Container 
            is_ok                      = lv_ok ).   " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 

        lo_container_nnnn ?= lo_container. 
        lo_container ?= lo_container_nnnn->modify_primary_record( primary_record = ls_p9000 ). 

        lo_masterdata_bl->insert( 
          EXPORTING 
*           massn                      =     " Action Type 
*           massg                      =     " Reason for Action 
*           update_mode                =     " Which Posting Functions Are to Be Deactivated? 
*           no_auth_check              =     " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 
            message_handler            = lo_message_handler   " Error Handler 
          IMPORTING 
            is_ok                      = lv_ok    " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ") 
          CHANGING 
            container                  = lo_container ).   " HR: Master Data, Reference to Infotype Container 

*            CATCH cx_hrpa_violated_assertion.    " 

        IF lv_ok = abap_false OR 
          ( lv_ok = abap_true AND lo_message_handler->has_message( ) = abap_true ). 

          me->add_msg( 
            EXPORTING 
              im_s_msg             = ls_message 
              im_o_message_handler = lo_message_handler ). 


          CALL METHOD lo_masterdata_bl->flush 
            EXPORTING 
              no_commit = abap_true. 

        ELSEIF lv_ok = abap_true. 

          ls_bapireturn1-type            = 'S'. 
          ls_bapireturn1-message         = 'Record uploaded successfully.'(013). 

          me->add_msg( 
                im_s_msg         = ls_message 
                im_s_bapireturn1 = ls_bapireturn1 ). 

          CALL METHOD lo_masterdata_bl->flush 
            EXPORTING 
              no_commit = abap_false. 

        ENDIF. 

        CALL METHOD cl_hrpa_masterdata_enq_deq=>dequeue_by_pernr 
          EXPORTING 
            tclas = 'A'    " Transaction Class for Data Storage 
            pernr = ls_p9000-pernr.    " Standard Selections for HR Master Data Reporting 

*              ls_bapireturn1-type            = 'S'. 
*              ls_bapireturn1-message         = 'PERNR unlock performed successfully.'(007). 

               me->add_msg( 
*                    im_s_msg         = ls_message 
*                    im_s_bapireturn1 = ls_bapireturn1 ). 

      CATCH cx_hrpa_violated_assertion INTO DATA(lo_obj). 
        IF lo_message_handler->has_message( ) = abap_true. 

          me->add_msg( 
            EXPORTING 
              im_s_msg             = ls_message 
              im_o_message_handler = lo_message_handler ). 

        ENDIF. 

        IF lo_obj->get_text( ) IS NOT INITIAL. 
          ls_bapireturn1-type            = 'E'. 
          ls_bapireturn1-message         = lo_obj->get_text( ). 

          me->add_msg( 
            EXPORTING 
              im_s_msg         = ls_message 
              im_s_bapireturn1 = ls_bapireturn1 ). 
        ENDIF. 
    ENDTRY. 
  ENDMETHOD. 


  METHOD show_log. 

    DATA: 
      lo_alv              TYPE REF TO cl_salv_table, 
      lo_functions        TYPE REF TO cl_salv_functions_list, 
      lo_display_settings TYPE REF TO cl_salv_display_settings, 
      lo_columns          TYPE REF TO cl_salv_columns_table. 

    TRY. 
        CALL METHOD cl_salv_table=>factory 
          IMPORTING 
            r_salv_table = lo_alv 
          CHANGING 
            t_table      = a_t_message[]. "AT_LOG is your log table with all the entries 

      CATCH cx_salv_msg .      "... errors, success ... whatever 
        RETURN. 
    ENDTRY. 

    TRY . 
        "Set texto to non DDIC columns 
        lo_alv->get_columns( )->get_column( 'STATUS_ICON' )->set_short_text( CONV #('Status'(019) ) ). 

        "Allow leading zeros to PERNR field 
        lo_alv->get_columns( )->get_column( 'EMPLOYEENUMBER' )->set_leading_zero( ). 

        lo_alv->get_columns( )->get_column( 'TYPE' )->set_technical( 
            value = if_salv_c_bool_sap=>true 
        ). 

       CATCH cx_salv_not_found. 

    ENDTRY. 

    lo_columns = lo_alv->get_columns( ). 
    lo_columns->set_optimize( ). 
    lo_display_settings = lo_alv->get_display_settings( ). 
    lo_display_settings->set_striped_pattern( if_salv_c_bool_sap=>true ). 
    lo_functions = lo_alv->get_functions( ). 
    lo_functions->set_all( ). 
*    lo_display_settings->set_list_header( ' ' ). "The title of you ALV 

    lo_alv->display( ). "Method to display 

  ENDMETHOD. 

  METHOD add_msg.

    DATA: lt_msg        TYPE hrpad_message_tab, 
          ls_msg        LIKE LINE OF lt_msg, 
          ls_bapireturn TYPE bapiret1, 
          ls_message    LIKE LINE OF a_t_message[]. 

    MOVE-CORRESPONDING im_s_msg TO ls_message. 

    IF im_o_message_handler IS SUPPLIED. 
      IF im_o_message_handler->has_warning( ) = 'X'. 

        CALL METHOD im_o_message_handler->get_warning_list 
          IMPORTING 
            messages = lt_msg. 

      ELSEIF im_o_message_handler->has_abend( ) = 'X'. 

        CALL METHOD im_o_message_handler->get_abend_list 
          IMPORTING 
            messages = lt_msg. 

       ELSEIF im_o_message_handler->has_error( ) = 'X'. 

        CALL METHOD im_o_message_handler->get_error_list 
          IMPORTING 
            messages = lt_msg. 

      ENDIF. 
 

      LOOP AT lt_msg INTO ls_msg. 
        CALL FUNCTION 'BALW_BAPIRETURN_GET1' 
          EXPORTING 
            type       = ls_msg-msgty 
            cl         = ls_msg-msgid 
            number     = ls_msg-msgno 
            par1       = ls_msg-msgv1 
            par2       = ls_msg-msgv2 
            par3       = ls_msg-msgv3 
            par4       = ls_msg-msgv4 
          IMPORTING 
            bapireturn = ls_bapireturn. 

        MOVE-CORRESPONDING ls_bapireturn TO ls_message. 

        "Message type: S Success, E Error, W Warning, I Info, A Abort 
        CASE ls_bapireturn-type. 
          WHEN 'S' OR 'I'. 
            ls_message-status_icon = c_icon_green. 
          WHEN 'W'. 
            ls_message-status_icon = c_icon_yellow. 
          WHEN 'E' OR 'A'. 
            ls_message-status_icon = c_icon_red. 
        ENDCASE. 

        INSERT ls_message INTO TABLE a_t_message[]. 
      ENDLOOP. 

*    " in case infotype is not updated and still no messages were captured 
      IF ( 
          "g_is_ok IS INITIAL AND 
          lt_msg[] IS INITIAL ). 

        ls_bapireturn-id = 'HRESS_CLMS_MSG'. 
        ls_bapireturn-number = '073'. 
        ls_bapireturn-message_v1 = 'Error uploading data.'(014). 

        MOVE-CORRESPONDING ls_bapireturn TO ls_message. 
        INSERT ls_message INTO TABLE a_t_message[]. 

      ENDIF. 
    ENDIF.

    IF im_s_bapireturn1 IS SUPPLIED. 
      MOVE-CORRESPONDING im_s_bapireturn1 TO ls_message. 
      "Message type: S Success, E Error, W Warning, I Info, A Abort 

      CASE im_s_bapireturn1-type. 
        WHEN 'S' OR 'I'. 
          ls_message-status_icon = c_icon_green. 
        WHEN 'W'. 
          ls_message-status_icon = c_icon_yellow. 
        WHEN 'E' OR 'A'. 
          ls_message-status_icon = c_icon_red. 
      ENDCASE. 
      INSERT ls_message INTO TABLE a_t_message[]. 
    ENDIF. 
  ENDMETHOD. 
ENDCLASS. 

*--------------------------------------------------------------------* 
* Selection screen 
*--------------------------------------------------------------------* 
"Test mode 
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE TEXT-002. 
PARAMETERS: p_test TYPE flag AS CHECKBOX DEFAULT 'X'. 
SELECTION-SCREEN END OF BLOCK b2. 

"Input file path 
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001. 
PARAMETERS: 
  p_lclpth TYPE string, 
  p_srvpth TYPE string. 

SELECTION-SCREEN END OF BLOCK b1. 

"Overwriting option 
SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE TEXT-010. 
SELECTION-SCREEN BEGIN OF LINE. 
PARAMETERS: p_ovrwrt TYPE flag AS CHECKBOX. 
SELECTION-SCREEN: COMMENT 3(58) TEXT-011. 
SELECTION-SCREEN END OF LINE. 
SELECTION-SCREEN END OF BLOCK b3. 

*--------------------------------------------------------------------* 
INITIALIZATION. 
*--------------------------------------------------------------------* 
  SET PF-STATUS 'DEFAULT'. 

*--------------------------------------------------------------------* 
AT SELECTION-SCREEN. 
*--------------------------------------------------------------------* 
  CASE sy-ucomm. 

    WHEN 'ONLI'. "Run 
      IF p_lclpth IS NOT INITIAL AND p_srvpth IS NOT INITIAL. 
        SET CURSOR FIELD 'P_LCLPTH'. 
        MESSAGE e016(pn) WITH 'Please enter only local or server path.'(005). 
      ENDIF. 

      IF p_lclpth IS INITIAL AND p_srvpth IS INITIAL. 
        SET CURSOR FIELD 'P_LCLPTH'. 
        MESSAGE e016(pn) WITH 'Please enter local or application server path'(012). 
      ENDIF. 

    WHEN '&XXL'. 
      DATA: 
        lt_xlsx_download  TYPE lcl_main=>ty_t_xlsx_layout, 
        lv_file           TYPE string, 
        lv_file_char(200) TYPE c. 

      DATA(lo_xlsx_file) = NEW zcl_xlsx_file( 
                                  sheet_content          = lt_xlsx_download[] 
*                                p_row_height_header    = C_DEFAULT_ROW_HEIGHT 
*                                p_row_height_body      = C_DEFAULT_ROW_HEIGHT 
*                                p_color_column         = 
                                 p_autofit_column_width = ABAP_TRUE 
                                        ). 

       lo_xlsx_file->build( 
*      EXPORTING 
           add_default_header     = abap_true 
           add_techn_names_header = abap_true 
           add_autofilter         = abap_true 
*           p_freeze_header        = ABAP_TRUE 
                          ). 

      zcl_xlsx_file=>browse_client_fs( 
                        CHANGING 
                          ch_path = lv_file 
                                       ). 

      IF lv_file IS INITIAL. 
        MESSAGE s016(pn) WITH 'Download canceled.'(015). 
      ELSE. 
        lo_xlsx_file->save_file_to_client( 
          EXPORTING 
            fs_filename = lv_file 
            start_excel = abap_true 
        ). 

        lv_file_char = lv_file. 
        MESSAGE s016(pn) WITH 'File downloaded to '(016) lv_file_char(50) lv_file_char+50(50) '.'. 
      ENDIF. 

    WHEN OTHERS. 

  ENDCASE. 

*--------------------------------------------------------------------* 
AT SELECTION-SCREEN OUTPUT. 
*--------------------------------------------------------------------* 
  LOOP AT SCREEN. 

   IF screen-name CS 'PNPBTRTL'. 
      screen-input = '0'. 
      MODIFY SCREEN. 

    ELSEIF screen-name CS 'PNPBUKRS'. 
      screen-input = '0'. 
      MODIFY SCREEN. 
    ENDIF. 
  ENDLOOP. 

*--------------------------------------------------------------------* 
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_lclpth. 
*--------------------------------------------------------------------* 
  zcl_xlsx_file=>browse_client_fs( 
    CHANGING 
      ch_path = p_lclpth 
  ). 

*--------------------------------------------------------------------* 
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_srvpth. 
*--------------------------------------------------------------------* 
  zcl_xlsx_file=>browse_server_fs( 
    CHANGING 
      ch_path = p_srvpth 
  ). 

*--------------------------------------------------------------------* 
START-OF-SELECTION. 
*--------------------------------------------------------------------* 
  DATA(go_main) = NEW lcl_main( 
                        im_p_lclpth = p_lclpth 
                        im_p_srvpth = p_srvpth 
                        im_p_test   = p_test 
                        im_p_ovrwrt = p_ovrwrt 
                              ). 

  go_main->upload( ). 

*--------------------------------------------------------------------* 
END-OF-SELECTION. 
*--------------------------------------------------------------------* 
  IF go_main->a_t_message[] IS INITIAL. 
    DATA lv_string TYPE string. 

    IF p_lclpth IS NOT INITIAL. 
      lv_string = p_lclpth. 
    ELSE. 
      lv_string = p_srvpth. 
    ENDIF. 

    lv_string = 'No data uploaded from:'(017) && ` ` && lv_string && '. Please check your file.'(018). 
    WRITE lv_string. 

  ELSE. 
     go_main->show_log( ). 
   ENDIF. 

Leave a Reply