Transformation Routines



Routines are used to define complex transformation rules. In most of the cases data won’t be coming directly in desired form before sending to the target. In some cases output needs to be derived on some incoming data. In such cases we need to go for writing of routines at the transformation level.
There are four types of Routines available

• Field Routine
• Start Routine
• End Routine
• Expert Routine

The routine which we need to go for depends on when it needs to be executed. Suppose if some logic needs to be implemented before transformation then the start routine needs to be implemented.


FIELD ROUTINE

It operates on a single record for a single characteristic or key figure. The value gets modified in the routine based on one or more source fields before it is transferred to the target

The Key figure total no of days needs to be populated using start and end dates.
So to calculate total no of days we are going to write a field routine. But for that start and end date values need to be mapped to the key figure.






We have mapped start and end date to the key figure and now double click on the equal to symbol








We need to write the logic there. Once written it will take to the result which is nothing but total no of days.


The logic is written it is end date minus start date. Syntax is checked it showed no errors. Save the Routine and activate the transformation.


RESULT = SOURCE_FIELDS-/BIC/RT_ENDT - SOURCE_FIELDS-/BIC/RT_SRTDT.






START ROUTINE

The start routine is run at the start of the transformation. The start routine has a table in the format of the source structure as input and output parameters. It is used to perform preliminary calculations and store these in a global data structure or in a table. This structure or table can be accessed from other routines. You can modify or delete data in the source_package .

For field routine it will act on each record but in start routine it will have all the data in source_package . In Start routine we will have the structure of the source fields. So in this scenario the key figure is not available in source.
So in Start routine we calculate the total no of days for each record with its unique key job number and store them in an internal table.
The internal table is global and it can be accessed every where. So we have to write a field routine for key figure total no of days mapped with job number. Using this job number we will read the value of key figure total no of days from internal table and update the result i.e. total no of days key figure.





Click on start routine and it will take to the editor. In the start routine there will be table source_package which contains all the data and has the structure of source.
Here we will read the job number from source_package and update it into internal table and total no of days key figure value into internal table using start and end dates.




Once this is done the value of key figure is in internal table and from there we need to get into target . So for that a field routine needs to be written to read from value from internal table. The key to internal table to get unique value is job number so job number is mapped to key figure.





Here Job number is mapped to the key figure. And a field routine is written to read the total no of days using this job number.


read table itab into wa_itab
with key job_numb = SOURCE_FIELDS-/BIC/RT_JOBNO.
if sy-subrc = 0.
Result = wa_itab-profile.
endif.




END ROUTINE

An end routine is a routine with a table in the target structure format as input and output parameters. You can use an end routine to postprocess data after transformation on a package-by-package basis. Data is stored in result_package.

End Routine is processed after start routine, mappings, field routines and finally before the values is transferred to the output. End routine has the structure of the target and result_package contains the entire data which finally is the output.
For our scenario we have one to one mapping for all the fields except total no of days so in result_package it will not have the value for each record.
So in End Routine we will calculate the key figure value for each record and modify the result_package and there by the output will also contain the key figure.





In transformation all are mapped one to one. Click End Routine at the top it will take to the editor. There we have to modify the result_package with the key figure value updated.





In the end routine we will loop over the result_package and using the start and end dates of each record the key figure value is calculated and updated again into the result_package. Check the routine and save the routine and activate the transformation.


Data: wa_RESULT_PACKAGE type _ty_s_TG_1.
Loop at RESULT_PACKAGE into wa_RESULT_PACKAGE.
wa_RESULT_PACKAGE-/BIC/RT_WKYDY1 = wa_RESULT_PACKAGE-/BIC/RT_ENDT - wa_RESULT_PACKAGE-/BIC/RT_SRTDT.
if wa_RESULT_PACKAGE-/BIC/RT_WKYDY1 le 10 .
wa_RESULT_PACKAGE-/BIC/RT_PRFL = '(0-10)'.
Else if wa_RESULT_PACKAGE-/BIC/RT_WKYDY1 gt 10 and
wa_RESULT_PACKAGE-/BIC/RT_WKYDY1 le 20.
wa_RESULT_PACKAGE-/BIC/RT_PRFL = '(10-20)'.
else.
wa_RESULT_PACKAGE-/BIC/RT_PRFL = '(20-NN)'.
endif.
modify RESULT_PACKAGE from wa_RESULT_PACKAGE.
End loop.




EXPERT ROUTINE

An Expert routine is a routine with contains both the source and target structure. we can use Expert routine if there are not sufficient functions to perform transformation.

For Expert Routine every things needs to be written using coding. In simple an expert routine performs all the actions of Start Routine, Mappings, Field and End Routines.
In Expert Routine we will read from source_package which contains all the data and update into result_package which should be the output and when doing we will calculate the key figure total no of days.


Click Transformation and go to edit and click the expert routine.
If clicked it asks to confirm the transformation mappings will be deleted and will be taken to coding part editor.





Now the transformation will have only one mapping from source to target saying expert routine .See that start and end routine are also disabled. And activate the transformation.

Want to learn SAP from Experts? - Archon Solutions

Comments

Post a Comment