ABAP Workbench Tricks #2: Dynamic Patterns

DevWorkbench    Wednesday August 6th, 2014   

In yesterday’s post about patterns, I discussed the basic usage of the pattern feature and showed you how to create your own patterns. Today, I’ll take it further and demonstrate how you can add dynamic elements to your patterns, making your development process even more streamlined.

Dynamic patterns explained

So, what is this about? In regular (static) patterns, there is just ABAP code and possibly some comments. Useful, but not terribly sophisticated. With dynamic patterns however, you can emulate the behaviour of the built-in patterns: Take an argument (e.g. a structure name), do some processing, produce output based on the input.

As an example, we’ll create a dynamic pattern that lists all the fields of a given structure below each other in order to be filled with values. Of course, since you can execute code and pass parameters to your pattern, you can do pretty much anything that you can think of.

Creating a dynamic pattern

To create a dynamic pattern in SAP ERP, just perform the steps I discussed in yesterday’s post about patterns to create one: Select Utilities > More Utilities > Edit Pattern > Create Pattern and enter a name for your pattern. It will contain only the following line of code, nothing else:


Wait, that’s it? Of course, this doesn’t do anything yet. We’ve just told the pattern to call a specific function module, which I’ll discuss now.

Content of a Dynamic Pattern

Content of a Dynamic Pattern

Creating the function module for the dynamic pattern

This pattern will, when inserted, call a function module that is dependent on the pattern name:


You need to create this function module. For my test pattern, the name is ZTESTDYNAMIC_EDITOR_EXIT accordingly. You will receive a notification that the name is reserved for SAP, but you can continue anyway. Create the function module with one TABLES parameter of type RSWSOURCET with the name BUFFER (attention: in ECC6, it’s a CHANGING instead of a TABLES parameter).

Function Module for Dynamic Pattern

Function Module for Dynamic Pattern

In this function module, you can now do whatever you want to do with your dynamic pattern. You have all the possibilities of normal dialog programming. You could for example ask the user for input with a popup, as I’m doing in this example (find my example coding below).

Here, I’m asking the user for some input by using the function module POPUP_GET_VALUES. This is what appears after the pattern has been inserted via the dialog you already know.

Popup Sent from Dynamic Pattern

Popup Sent from Dynamic Pattern

After entering a structure name, my coding appends a line to the BUFFER table for each element of the given structure. The output looks like this.

Output Created by Dynamic Pattern

Output Created by Dynamic Pattern

Isn’t that cool? And that’s the end of this quick tutorial! Enjoy using dynamic patterns, as they can really get your ABAP development moving once you know how to use them effectively.

Example coding for function module

Here’s the coding I put into my function module to generate a list of structure fields to assign values.

 FUNCTION ztestdynamic_editor_exit.
 *"*"Local Interface:
   DATA ls_sval TYPE sval.
   DATA ls_dd02l TYPE dd02l.
   DATA ls_dd03l TYPE dd03l.
   DATA lt_dd03l TYPE STANDARD TABLE OF dd03l.
   DATA lv_strucname LIKE rsrd1-ddtype_val.
 * Get name of structure from user
   ls_sval-tabname 'RSRD1'.
   ls_sval-fieldname 'DDTYPE_VAL'.
   ls_sval-field_attr  '00'.
   APPEND ls_sval TO lt_sval.
       popup_title     'Enter Structure Name'
       fields          lt_sval
       error_in_fields 1
       OTHERS          2.
 * Get the given structure name
   READ TABLE lt_sval INTO ls_sval INDEX 1.
   lv_strucname ls_sval-value.
 * Check if the structure exists
     FROM dd02l
     INTO ls_dd02l
     WHERE tabname lv_strucname
       AND tabclass 'INTTAB'.
   IF sy-subrc <> 0.
 * Read table fields
   SELECT FROM dd03l
     INTO TABLE lt_dd03l
     WHERE tabname lv_strucname
     ORDER BY position ASCENDING.
 * Add a comment to the buffer
   CONCATENATE '* Set structure fields for variable lv_' lv_strucname INTO buffer.
   APPEND buffer.
   CONCATENATE 'DATA lv_' lv_strucname ' TYPE ' lv_strucname '.' INTO buffer.
   APPEND buffer.
 * Loop at fields and generate the output for each field
   LOOP AT lt_dd03l INTO ls_dd03l.
     CONCATENATE 'lv_' lv_strucname '-' ls_dd03l-fieldname ' =  .' INTO buffer.
     APPEND buffer.