Create new HR Infotype record ABAP Code

Example ABAP HR report to create HR infotype record using SAP function module HR_INFOTYPE_OPERATION. In this example, I will demonstrate creating a new 0105 record by selecting an existing one from the SAP database and updating it’s from and to date. Although the 0105 infotype is being used the code will be similar for almost all other HR infotype updates.

*&-------------------------------*
REPORT ZUPD_INFOTYPE.

PARAMETERS: p_itype type PRELP-INFTY DEFAULT '0105',
            p_pernr type pernr-pernr default '1'.

*Change P0105 to the structure of the infotype you want to update
*Structure can be built dynamically but this wouldn't add any value to
*this example just over complicate it
DATA: ld_record type p0105,
      ld_table  type string.

data: return_struct   type BAPIRETURN1,
      personaldatakey type BAPIPAKEY.

*---------------------------------
START-OF-SELECTION.

  data : it_tabdescr type abap_compdescr_tab,
         wa_tabdescr type abap_compdescr.
  data : ref_table_descr type ref to cl_abap_structdescr.

  CONCATENATE 'PA' p_itype into ld_table.
  SELECT single *
    from (ld_table)
    into CORRESPONDING FIELDS OF ld_record
   where pernr eq p_pernr
     and USRTY eq 'MAIL'. "Only relevant for infotype 0105

* Change values within record you want to create
*  clear: ld_record-???,
*         ld_record-???,
*         ld_record-???.
*  ld_record-??? =
*  ld_record-??? =

  ld_record-endda = '99991231'.
  ld_record-begda = sy-datum.

* HR_INFOTYPE_OPERATION will fail if the record is not locked
CALL FUNCTION 'HR_EMPLOYEE_ENQUEUE'
    EXPORTING
      number = p_pernr.

CALL FUNCTION 'HR_INFOTYPE_OPERATION'
    EXPORTING
      infty         = p_itype
      subtype       = ld_record-subty
      number        = ld_record-pernr     "employeenumber
      validityend   = ld_record-endda
      validitybegin = ld_record-begda
      record        = ld_record
      operation     = 'INS'
      nocommit      = ' '
      dialog_mode   = '0'
    IMPORTING
      return        = return_struct
      key           = personaldatakey
    EXCEPTIONS
      OTHERS        = 0.


* Release lock for the record after the update is completed
CALL FUNCTION 'HR_EMPLOYEE_DEQUEUE'
    EXPORTING
      number = p_pernr.