Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Mark_Zhiltsov
Associate
Associate
0 Kudos
325

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.

 

1. Requirement

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.

 

2. Realization

So, what do we actually need to do:

  1. Create new custom Rule Action using transaction /CORS/C_RULES.
  2. Create BADI implementation for /CORS/BADI_RULE_ACTION.


2.1 Custom Rule Action creation

  1. In tcode /CORS/C_RULES we need to create a new Rule Action under Rule Action Definition tab. In our example we will use YCSV_PACKAGE name.
    Rule_Action.png 
  2. Next we need to create a new Rule under Rule Maintenance tab. We will use ZPACK name.
    Rule.png 
  3. After that we need to assign Rule Action to the Rule. As we also require standard Rule Action SAP_BN_MT_EVENT, we need to take into consideration sequence (RuleActSeq).
    Rule_Action_Assigment.png
  4. Also we need to assign Rule Condition to the Rule. Event Action 15 is Pack event, Event Location group can be created under Location Group Definition tab.
    Rule_Condition_Assignment.png

 

2.2 BADI Implementation

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.
BADI.png

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.
BADI_filter.png

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:

  • This code uses import parameter IV_EVTID (Event GUID) to get data from tables /CORS/DM_EVT_REL, /CORS/DM_OBJ_IDS, /CORS/DM_OBJ_HRY, /CORS/DM_OBJ_IDS - you can find other useful tables with /CORS/* mask.
  • This code will be executed for every packing event. If you will need to process data in bunches of events, you will need to create a workaround, for example, create some DB table to log events.

 

Thank you for taking your time to read this article, I hope you found it helpful. Good luck with your developments!