ABAP: Code example to insert record in infotypes

This is a sample code to add data to HCM infotypes.

DATA lt_msgs TYPE hrpad_message_tab.
DATA ls_msgs LIKE LINE OF lt_msgs.

TRY.
    DATA(ls_pskey) = CORRESPONDING pskey( cs_pnnnn ).

    cl_hrpa_masterdata_factory=>get_masterdata_buffer( IMPORTING masterdata_buffer = DATA(lo_pa_infotype_buffer) ).
    cl_hrpa_masterdata_factory=>get_business_logic( IMPORTING business_logic = DATA(lo_masterdata_bl) ).
    lo_masterdata_bl->initialize( ).

    "START_TRIAL
    lo_masterdata_bl->start_trial( IMPORTING magic_cookie = DATA(lo_pa_cookie) ).

    CLEAR cs_return.

    DATA(lo_message_handler) = NEW cl_hrpa_message_list( ).

    CALL METHOD cl_hrpa_masterdata_enq_deq=>enqueue_by_pernr
      EXPORTING
        tclas           = cl_hrpa_tclas=>tclas_employee
        pernr           = ls_pskey-pernr
        message_handler = lo_message_handler
      IMPORTING
        is_ok           = DATA(lv_ok).

    IF lv_ok IS INITIAL AND lo_message_handler->has_error( ) EQ abap_true.

      lo_message_handler->get_error_list( IMPORTING messages = lt_msgs ).

      LOOP AT lt_msgs INTO ls_msgs.
        CLEAR cs_return.
        cs_return-type       = ls_msgs-msgty.
        cs_return-id         = ls_msgs-msgid.
        cs_return-number     = ls_msgs-msgno.
        cs_return-message_v1 = ls_msgs-msgv1.
        cs_return-message_v2 = ls_msgs-msgv2.
        cs_return-message_v3 = ls_msgs-msgv3.
        cs_return-message_v4 = ls_msgs-msgv4.
        RETURN.
      ENDLOOP.

      RETURN.
    ENDIF.

    CALL METHOD lo_masterdata_bl->get_infty_container
      EXPORTING
        tclas           = cl_hrpa_tclas=>tclas_employee
        pskey           = ls_pskey
        no_auth_check   = space
        message_handler = lo_message_handler
      IMPORTING
        container       = DATA(lo_container)
        is_ok           = lv_ok.

    IF lv_ok IS INITIAL AND lo_message_handler->has_error( ) EQ abap_true.

      lo_message_handler->get_error_list( IMPORTING messages = lt_msgs ).

      LOOP AT lt_msgs INTO ls_msgs.
        CLEAR cs_return.
        cs_return-type       = ls_msgs-msgty.
        cs_return-id         = ls_msgs-msgid.
        cs_return-number     = ls_msgs-msgno.
        cs_return-message_v1 = ls_msgs-msgv1.
        cs_return-message_v2 = ls_msgs-msgv2.
        cs_return-message_v3 = ls_msgs-msgv3.
        cs_return-message_v4 = ls_msgs-msgv4.
        RETURN.
      ENDLOOP.

      RETURN.
    ENDIF.

    DATA(lo_container_nnnn) = CAST cl_hrpa_infotype_container( lo_container ).
    lo_container ?= lo_container_nnnn->modify_primary_record( cs_pnnnn ).

    CALL METHOD lo_masterdata_bl->insert
      EXPORTING
        no_auth_check   = space
        message_handler = lo_message_handler
      IMPORTING
        is_ok           = lv_ok
      CHANGING
        container       = lo_container.

    IF lv_ok IS INITIAL AND lo_message_handler->has_error( ) EQ abap_true.

      lo_message_handler->get_error_list( IMPORTING messages = lt_msgs ).

      LOOP AT lt_msgs INTO ls_msgs.
        CLEAR cs_return.
        cs_return-type       = ls_msgs-msgty.
        cs_return-id         = ls_msgs-msgid.
        cs_return-number     = ls_msgs-msgno.
        cs_return-message_v1 = ls_msgs-msgv1.
        cs_return-message_v2 = ls_msgs-msgv2.
        cs_return-message_v3 = ls_msgs-msgv3.
        cs_return-message_v4 = ls_msgs-msgv4.
        RETURN.
      ENDLOOP.

      RETURN.
    ENDIF.

    IF p_test EQ abap_false.
      "Approve trial and commit - PA
      lo_masterdata_bl->approve_trial( EXPORTING magic_cookie = lo_pa_cookie ).
      lo_masterdata_bl->flush( no_commit = abap_true ).

      CALL METHOD lo_masterdata_bl->flush
        EXPORTING
          no_commit = p_test.
    ELSE.
      lo_masterdata_bl->discard_trial( EXPORTING magic_cookie = lo_pa_cookie ).
    ENDIF.

    CALL METHOD cl_hrpa_masterdata_enq_deq=>dequeue_by_pernr
      EXPORTING
        tclas = cl_hrpa_tclas=>tclas_employee
        pernr = ls_pskey-pernr.

  CATCH cx_hrpa_violated_assertion.

ENDTRY.

2 thoughts on “ABAP: Code example to insert record in infotypes”

  1. Hi,

    I have gone through this blog post and seems amazing. I got couple of questions.
    1. Ehst if the infotype is already having the record against any pernr? Would this code work.
    2. As I have understood, this code will insert a new row against a pernr in the given infotype. I have implemented the same and there’s no syntax error. Neither any dump. Nor exception. But still no impact on the fields. Only change and changed by are getting overwritten. Is there anything I am missing? Also if possible could you please provide the driver program for this. Many thanks.

Leave a Reply