Skip to main content
ABAP

Dev Tip: Learn how to use dynamic patterns to automatically generate replicated code in the ABAP editor

Shinjan Sengupta Shinjan Sengupta, SAP/UX Solution Architect 3 min read

In case you’ve missed our dev tips in past weeks, we will be posting them in the blog moving forward. To sign up and get your dev tips delivered to your inbox each week, click here!

Tip of the Week: Learn how to use dynamic patterns to automatically generate replicated code in the ABAP editor

Learn how to use dynamic patterns to automatically generate replicated code in the ABAP editor.

There are times when we want to reuse a snippet of code or some text in our ABAP programs. A very common case would be to add a program header to every program module. Instead of copying and pasting the program header from an older program and then editing it, we can use dynamic patterns to automatically generate the program header pre-filled with pertinent information. We can write more complex patterns involving multiple input screens as well.

Example usage:

  1. Generating program header
  2. Generating full program templates following company naming conventions and standards
  3. Generate get and set methods based on DDIC structures for a local class

We will generate a simple one line pattern with the user ID dynamically populated.

Step 1: Create a Pattern

Go to SE38 and create a program.

ABAP Editor initial screen with ZTEST_PATTERN entered in the Program field

Go to Utilities –> More Utilities –> Edit Pattern –> Create Pattern

Utilities menu open to More Utilities, Edit Pattern, Create Pattern in the ABAP Editor

Give it a name (this name will be used later)

Create Pattern dialog with ZPROGRAM_HEADER entered as the pattern name

Inside the pattern editor, go to Pattern –> Dynamic Pattern

Pattern menu in the pattern editor with the Dynamic Pattern option

Replace the default text (*$&$EXIT) to *$&$MUSTER (this is to create your own Function Module)

Editing Pattern ZPROGRAM_HEADER screen with *$&$MUSTER on line 1

Step 2: Create a Function Module

First create a function group if there isn’t already one that you can use.

Goto menu in Function Builder open to Function Groups, Create Group

Create Function Group dialog for ZTEST_PATTERNS, short text Editor Pattern Demo

Create a function module with the following naming rule<PATTERN_NAME>_EDITOR_EXIT

Function Builder initial screen with ZPROGRAM_HEADER_EDITOR_EXIT in the Function Module field

Create Function Module dialog for ZPROGRAM_HEADER_EDITOR_EXIT in function group ZTEST_PATTERNS

There should be only one TABLES parameter of type RSWSOURCET and name BUFFER. It is a table of type string.

Function module Tables tab with one parameter, BUFFER, TYPE RSWSOURCET

Step 3: Implement the Function Module

Write your code and populate the BUFFER table with the text. Anything that BUFFER contains will be the output of the pattern. I have used SY-UNAME to get the current user’s ID and appended one sentence to the BUFFER.

Source code of ZPROGRAM_HEADER_EDITOR_EXIT appending a line with sy-uname to buffer

Step 4: Use the Pattern

Go to your target report where you want to use the pattern, and click on the Pattern button from the toolbar to bring up the following dialog. In the Other Pattern field, put the name of the pattern that you have created.

Insert Statement dialog with Other Pattern selected and ZPROGRAM_HEADER entered

The pattern will now be placed in your code with the dynamic value (user ID in this case) populated.

Report ZTEST_PATTERN with the inserted line * This is a test pattern by SSENGUPTA

If you have an interest in viewing similar content, visit our blog, here.

View our LinkedIn, here.

Share