Monday, August 29, 2016

TIP: Generate a Unique index for Parameter values in SAP EM


Problem Statement

SAP EM stores attribute values as name value pairs (called Parameters). There are certain instances where you need to hold multiple values for the same attribute / parameter. For example, a shipment event handler can be tracked using multiple tracking Numbers. And at a later stage some of the tracking numbers become obsolete and need to be removed from the event handler. In these scenarios, not knowing the right index to remove the applicable parameters becomes very difficult and inefficient in terms of performance.

Solution

There are several ways to overcome the situation, but based on my experience, I will outline the approach to generate a unique index for a given attribute value --> This works out very well. You can incorporate the below mentioned code to generate a unique index of any given attribute value.

Code

FUNCTION zem_create_unique_index.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(PARAM_VALUE) TYPE  ZKW_CHAR60
*"  EXPORTING
*"     REFERENCE(PARAM_INDEX) TYPE  /SAPTRX/INDEXCOUNTER
*"----------------------------------------------------------------------

  
DATA lv_char            TYPE c,
         lv_string_length   
TYPE i,
         lv_ascii_num
(3)    TYPE n,
         lv_ascii           
TYPE i,
         lv_ascii_offset
(1TYPE n,
         lv_ascii_length    
TYPE i,
         lv_from_digit      
TYPE i,
         lv_no_of_digits    
TYPE i.

  
FIELD-SYMBOLS <fs_hex>  TYPE x.

  lv_no_of_digits 
1.
  lv_string_length 
strlenparam_value ).

*** Parse through each character and get ASCII
  
DO lv_string_length TIMES.

*** Get the Character
    lv_char 
param_value+lv_from_digit(lv_no_of_digits).

*** Increase the Offset
    lv_from_digit 
lv_from_digit + 1.

*** Assign Character to Numeric Casting
    UNASSIGN <fs_hex>
.
    
ASSIGN lv_char TO <fs_hex> CASTING.

*** Get the ASCII Value
    
IF <fs_hex> IS ASSIGNED.
      
MOVE <fs_hex> TO lv_ascii_num.
    
ENDIF.

*** Get the Length Of Ascii
    lv_ascii_length 
strlenlv_ascii_num ).
*** Set Offset to read Last digit
    lv_ascii_offset 
= ( lv_ascii_length ).
*** Ascii SUM
    lv_ascii 
lv_ascii + lv_ascii_num.

    
CLEARlv_char,
           lv_ascii_num
.
  
ENDDO.

  param_index 
lv_ascii.

ENDFUNCTION.


If you have any questions just leave a comment below and I'll get back to you.