NEW_RECORD__END_ROUTINE – How to use it
SAP BW 7.x system, for more information, visit the Business Intelligence homepage,
This Document describes how to use the method NEW_RECORD__END_ROUTINE.
when a transformation gets created we have the following objects getting generated automatically,
- Program RSTRAN_MASTER_TMPL
- Class lcl_transform
- 2 Types _ty_s_SC_1 for the source and _ty_s_TG_1 for the target
start_routine, inverse_start_routine, end_routine, inverse_end_routine and new_record__end_routine
The focus for us in the new_record__end_routine.
Note – There is no new_record__start_routine, we only have the new_record__end_routine.
In the sap BW transformations we have a method “new_record__end_routine”. Now i am going to explain you on how it can be used.
Scenario — I am loading data from one InfoProvider to another. Now i need to add few more records to the incoming data. In the end routine, i could append these records by simple “Append” statement. But this will definitely give an error, if you are going to add the entries without increment the “data record” number.
From InfoProvider A we load data to InfoProvider B. Consider we have 10 records coming from A, now i look up on another InfoProvider C and add 5 more records to the result_package by using an “Append” statement. But the load will fail because of the newly added records, as the newly added records will not have the “record” number.
End routine will always depend on the structure _ty_s_TG_1.This structure will always have a default field called RECORD. This field is automatically populated for every incoming record from source, but when we append the records to the result_package then this field must be populated explicitly or else the load will fail.
So how we can handle this using ABAP code.
- sort the result package by record is descending order.
- read the record. (Highest record number is received)
- Loop the internal table from where, we are doing to add the records to result_package
- Now for each loop increase the record count by 1, before appending to the result_package
- Sort the result package by record in ascending order (optional statement)
This will resolve the error,
Actual code (ZUSER is the InfoObject from where i read and append records to the result_package)
So now this can be totally avoided using the method NEW_RECORD__END_ROUTINE
see the screen shot below,
We export the current table index (SYST-TABIX) to the method and also the segment id. The method returns the last record + 1 to the counter and counter is assigned to the wa_result_package-record.
Note —request, datapackid, segid are part of end_routine, hence we do not have to specifically declare them.
This is the SAP recomended approach to add the records to the result_package.