Welcome, in this article we will look at steps for ABAP developer to create custom logic for custom Rule Action in SAP Corporate Serialization (SAP CorS). More information on CorS you can find here and in SAP Help.
During one of the events we need to add some custom logic to the logic of standard Rule Action. In our example we will save some data in CSV file on application server during Pack event. We will also use standard Rule Action SAP_BN_MT_EVENT, which composes and sends outbound SAP Business Network Material Traceability events - more information can be found in help documentation, for example, here.
So, what do we actually need to do:
After creation of custom Rule Action YCSV_PACKAGE we finally will create a BADI implementation for /CORS/BADI_RULE_ACTION and put some logic in the implementing class. As you can see, this BADI have a few implementations for standard Rule Actions.
We create a new Badi Implementation ZEH_CORS_RULE_ACTION_CSV. As this BADI is supposed to be used for all Rule Actions, we need to put YCSV_PACKAGE as filter value for RULE_ACTION filter.
After the creation of implementing class ZCL_CORS_RULE_ACTION_CSV, we can put the desired logic into /CORS/IF_BADI_RULE_ACTION~EXECUTE method. Code example for our case:
METHOD /CORS/IF_BADI_RULE_ACTION~EXECUTE.
DATA:
lt_output TYPE ZCORS_T_NTF_CONTENT,
lt_file TYPE STANDARD TABLE OF string,
lv_filepath TYPE string.
CONSTANTS:
lc_dir TYPE eps2filnam VALUE '/tmp',
lc_format TYPE c LENGTH 3 VALUE 'csv'.
DATA(lv_filename) = |test{ sy-datum }_{ iv_evtid }|.
"get event data from DB tables
SELECT @( |{ sy-datum DATE = ENVIRONMENT }| ) AS ntf_date,
CONCAT( o1~product, o1~serial ) AS obj1,
CONCAT( o2~product, o2~serial ) AS obj2
FROM /CORS/DM_EVT_REL AS r
INNER JOIN /CORS/DM_OBJ_IDS AS o1
ON r~objid = o1~objid
INNER JOIN /CORS/DM_OBJ_HRY AS h
ON o1~objid = h~objid_child
INNER JOIN /CORS/DM_OBJ_IDS AS o2
ON h~objid_parent = o2~objid
WHERE r~evtid = @iv_evtid
AND o1~obj_type = '18'
AND o2~obj_type = '17'
INTO CORRESPONDING FIELDS OF TABLE @lt_output.
SORT lt_output BY ntf_date obj2 obj1 ASCENDING.
DELETE ADJACENT DUPLICATES FROM lt_output.
IF lt_output IS NOT INITIAL.
"create csv file and save on AS
lt_file = VALUE #( ( |Date,Parent serial no,Child serial ID| ) ).
LOOP AT lt_output ASSIGNING FIELD-SYMBOL(<ls_output>).
lt_file = VALUE #( BASE lt_file ( |{ <ls_output>-ntf_date },{ <ls_output>-obj2 },{ <ls_output>-obj1 }| ) ).
ENDLOOP.
lv_filepath = |{ lc_dir }/{ lv_filename }.{ lc_format }|.
OPEN DATASET lv_filepath FOR OUTPUT IN TEXT MODE ENCODING UTF-8.
IF sy-subrc = 0.
LOOP AT lt_file ASSIGNING FIELD-SYMBOL(<ls_file>).
TRANSFER <ls_file> TO lv_filepath.
ENDLOOP.
CLOSE DATASET lv_filepath.
ENDIF.
ENDIF.
ENDMETHOD.
After execution of packing event from /CORS/COCKPIT, we will be able to find a file in corresponding directory using AL11. A few notes on the code above:
Thank you for taking your time to read this article, I hope you found it helpful. Good luck with your developments!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
27 | |
25 | |
19 | |
14 | |
13 | |
11 | |
10 | |
9 | |
8 | |
6 |