Doing development in SAP HCM requires to update InfoType records. SAP provides OO access to execute CRUDQ operations. This is called decoupled framework and works in areas, like Personnel Administration(PA) InfoTypes in HCM, which are made ready for this by SAP. We will get an insight on changing an existing record. Other operations like READ, INSERT and delete are more easy to program.

The below method modifies an existing InfoType record in connection to an employee, and You can see the way to invoke it as well.

Method Definition

I’d like to highlight the importing parameter i_secondary_record. The info type data can be split up across several fragments, so database tables. An example for secondary is country specific information which exists in one country but not in another. Therefore it is an extension to the base table and contains fields relevant for that particular country only. An example for this is InfoType 3395, which is an additional payment data extension for France. The corresponding DDIC structure is P3395.

"! <p class="shorttext synchronized" lang="en">Modify InfoType record</p>
"!
"! @parameter i_pskey | <p class="shorttext synchronized" lang="en">Keys for HR Master Data</p>
"! @parameter i_primary_record | <p class="shorttext synchronized" lang="en">Primary Record</p>
"! @parameter i_secondary_record | <p class="shorttext synchronized" lang="en">Secondary Record</p>
"! @parameter i_no_autch_check | <p class="shorttext synchronized" lang="en">Without Authority Check</p>
"! @parameter i_simulate | <p class="shorttext synchronized" lang="en">Simulation run</p>
"! @parameter i_no_commit | <p class="shorttext synchronized" lang="en">No Commit Work after DB Update</p>
"! @parameter i_tclas | <p class="shorttext synchronized" lang="en">Transaction Class for Data Retention</p>
"! @parameter i_init_buffer | <p class="shorttext synchronized" lang="en">Initialize Buffer</p>
"! @parameter i_mode | <p class="shorttext synchronized" lang="en">Which key matching entries to modify</p>
"! @parameter e_failed | <p class="shorttext synchronized" lang="en">Modification failed</p>
"! @parameter e_message_hndl | <p class="shorttext synchronized" lang="en">Message Handler</p>
"! @parameter e_message_table | <p class="shorttext synchronized" lang="en">Message Table w/o reason and field info</p>
"! @raising cx_hrpa_violated_assertion | <p class="shorttext synchronized" lang="en">HR Master Data: Unfulfilled Program Condition</p>
CLASS-METHODS modify
  IMPORTING
    !i_pskey            TYPE pskey
    !i_primary_record   TYPE any
    !i_secondary_record TYPE any OPTIONAL
    !i_no_autch_check   TYPE boole_d
    !i_simulate         TYPE abap_bool
    !i_no_commit        TYPE abap_bool
    !i_tclas            TYPE tclas DEFAULT cl_hrpa_tclas=>tclas_employee
    !i_init_buffer      TYPE abap_bool DEFAULT abap_true
    !i_mode             TYPE hrpad_read_mode DEFAULT if_hrpa_masterdata_bl=>exact_matching_record
  EXPORTING
    !e_failed           TYPE abap_bool
    !e_message_hndl     TYPE REF TO cl_hrpa_message_list
    !e_message_table    TYPE bapiret1_tab
  RAISING
    cx_hrpa_violated_assertion .

Method Implementation

METHOD modify.

DATA(message_handler) = NEW cl_hrpa_message_list( ).

"Lock employee
cl_hrpa_masterdata_enq_deq=>enqueue_by_pernr(
  EXPORTING
    tclas = i_tclas
    pernr = i_pskey-pernr
    message_handler = message_handler
  IMPORTING
    is_ok = DATA(ok)
).

IF ok EQ abap_false.
 e_failed = abap_true.
 e_message_hndl = message_handler.
 IF e_message_table IS SUPPLIED.
   e_message_table = get_messages( message_handler ).
 ENDIF.
   RETURN.
ENDIF.

cl_hrpa_masterdata_factory=>get_business_logic( IMPORTING business_logic = DATA(md_logic) ).
IF i_init_buffer EQ abap_true.
  md_logic->initialize( ).
ENDIF.

md_logic->start_trial( IMPORTING magic_cookie = DATA(magic_cookie) ).

CALL METHOD md_logic->read
  EXPORTING
    tclas           = i_tclas
    pernr           = i_pskey-pernr
    infty           = i_pskey-infty
    subty           = i_pskey-subty
    objps           = i_pskey-objps
    sprps           = i_pskey-sprps
    begda           = i_pskey-begda
    endda           = i_pskey-endda
    seqnr           = i_pskey-seqnr
    mode            = i_mode
    no_auth_check   = i_no_autch_check
    message_handler = message_handler
  IMPORTING
    container_tab   = DATA(container_tab)
    is_ok           = ok.

IF ok EQ abap_false.
  e_failed = abap_true.
  e_message_hndl = message_handler.
  IF e_message_table IS SUPPLIED.
    e_message_table = get_messages( message_handler ).
  ENDIF. 
  cl_hrpa_masterdata_enq_deq=>dequeue_by_pernr( EXPORTING tclas = i_tclas pernr = i_pskey-pernr ).
  RETURN.
ENDIF.

LOOP AT container_tab INTO DATA(container).
    DATA(container_dao) = CAST if_hrpa_infty_container_data( container ).
    DATA(old_container) = container.
    container_dao ?= old_container->modify_key( i_pskey ).
    container ?= container_dao->modify_primary_record( primary_record = i_primary_record ).
    IF i_secondary_record IS SUPPLIED.
      container_dao = CAST if_hrpa_infty_container_data( container ).
      container ?= container_dao->modify_secondary_record( secondary_record = i_secondary_record ).
    ENDIF.

    CALL METHOD md_logic->modify
      EXPORTING
        old_container   = old_container
        message_handler = message_handler
      IMPORTING
        is_ok           = ok
      CHANGING
        container       = container.

    IF ok = abap_false.
      e_failed = abap_true.
      e_message_hndl = message_handler.
      IF e_message_table IS SUPPLIED.
        APPEND LINES OF get_messages( message_handler ) TO e_message_table.
      ENDIF.
    CONTINUE.
    ENDIF.
ENDLOOP.
IF sy-subrc NE 0.
  e_failed = abap_true.
  APPEND VALUE #( type = 'W' id = 'ZHCM_CORE'  number = '001' ) TO e_message_table.
  cl_hrpa_masterdata_enq_deq=>dequeue_by_pernr( EXPORTING tclas = i_tclas pernr = i_pskey-pernr ).
  RETURN.
ENDIF.

IF e_failed EQ abap_true.
  md_logic->discard_trial( magic_cookie = magic_cookie ).
  cl_hrpa_masterdata_enq_deq=>dequeue_by_pernr( EXPORTING tclas = i_tclas pernr = i_pskey-pernr ).
ELSE.
  e_message_hndl = message_handler.
  IF e_message_table IS SUPPLIED.
    e_message_table = get_messages( message_handler ).
  ENDIF.
  IF i_simulate EQ abap_false.
    md_logic->approve_trial( magic_cookie = magic_cookie ).
    md_logic->flush( no_commit = i_no_commit ).
  ELSE.
    md_logic->discard_trial( magic_cookie = magic_cookie ).
  ENDIF.
ENDIF.

cl_hrpa_masterdata_enq_deq=>dequeue_by_pernr( EXPORTING tclas = i_tclas pernr = i_pskey-pernr ).

ENDMETHOD.

Usage

The below example is finetuned forex for mass operation in the background disabling commit, simulation and authority check, which is done at once in the surrounding program. The rest of the configuration parameters are left on good defaults designed for most of the consumption cases. Use the IMPORTING parameters as the requirement demands it.

DATA:
  payment   TYPE p0015.

 DATA(md_key) = CORRESPONDING pskey( payment ).

your_class_instance->modify(
  EXPORTING
    i_pskey          = md_key
    i_primary_record = payment
    i_no_autch_check = abap_true
    i_simulate       = abap_false
    i_no_commit      = abap_true
  IMPORTING
    e_failed         = DATA(failed)
    e_message_hndl   = DATA(message_handler)
    e_message_table  = DATA(messages)
).