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

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

Contact us
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.

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.

Go to Utilities –> More Utilities –> Edit Pattern –> Create Pattern
Give it a name (this name will be used later)
Inside the pattern editor, go to Pattern –> Dynamic Pattern
Replace the default text (*$&$EXIT) to *$&$MUSTER (this is to create your own Function Module)
Step 2: Create a Function Module
First create a function group if there isn’t already one that you can use.
Create a function module with the following naming rule<PATTERN_NAME>_EDITOR_EXIT
There should be only one TABLES parameter of type RSWSOURCET and name BUFFER. It is a table of type string.
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.
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.
The pattern will now be placed in your code with the dynamic value (user ID in this case) populated.
Shinjan Sengupta

Shinjan Sengupta

Shinjan has been in the SAP space for over 10 years. He has worked on all areas of SAP ABAP for various clients in Europe and in the US. He considers himself to be a fast learner who puts enormous importance towards looking at the bigger picture while simultaneously being details oriented. Coming from a computer science background, he has always been a technology enthusiast dabbling in various programming languages along with pushing the limits of SAP ABAP. Before joining Mindset Consulting, he was the solution architect for Microsoft’s Business Process Monitoring engagement. Now as a development lead at Mindset, he has been heading challenging projects and is focused on delivering solutions that reach beyond the expectations of the clients.

Let’s make your SAP better, together.

  • This field is for validation purposes and should be left unchanged.