ABAP: Code example to insert record in infotypes
This is a sample code to add data to HCM infotypes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
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 Replies to “ABAP: Code example to insert record in infotypes”
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.
Hi MJ,
Thanks for you message! about your questions:
1) Not sure I understand your question. If a record already exists and keys are the same, then it will replace the record. If time constraint does not allow overlapping, it will delimite the other record.
2) I’ve created a new post with an example. Please check it at https://www.devtipro.com/sap-abap-hcm-test-and-upload-data-to-decoupled-infotype/.
Hope it can help you!