Skip to main content
ABAP

Dev Tip: ABAP Code Completion Template

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

To sign up and get your dev tips delivered to your inbox each week, click here!

Tip of the Week

ABAP Code Completion Template

Like ABAP code patterns, there is another handy tool tucked away in the corner of the ABAP editor that can speed up some aspects of your development. It’s called the ABAP Code Templates. Whenever you get code suggestions while typing keywords like ‘If’, or ‘Loop’ or ‘Case’, this is where it is coming from. When the suggestions appear, on press of the ‘TAB’ button, that template will be applied to your code.

But these templates can be dynamic too, although only to a certain extent. I will demonstrate that by using a very common scenario: tagging of modified code with a modification log or comment. Normally such a tag is to let other developers know when, why and by who has the code been last changed.

So, let’s say, we would want to surround any newer piece of code inside of an older code module with the following information:

  1. User Name of the person responsible
  2. Date and time of the change made
  3. Some change tracking number or some description of the change.
  4. A ‘BEGIN’ and an ‘END’ label.

We would also want to position the cursor in such a way that right after the template is applied you can start writing your code.

STEPS:

  1. Create a new report in SE38. Mine is called ZSS_DEMO_CODE_COMPLETION.
  2. In the right and bottom corner of the screen there is a small yellow button. Click on that.

    ABAP editor status bar with a small yellow button in the bottom right corner

  3. From the list, go to ‘Code Completion’.

    ABAP editor Options dialog with Code Templates selected, showing the template list and Add, Edit, Delete buttons

  4. Click on the Add button and you will see the following popup

    Add Code Template popup with Name: change and Description: code modification tag

  5. Now after giving it an appropriate name (this is the name you’d have to type to invoke the template on your editor), save it.

    Code Templates options with the change template selected and the Insert Tag menu open

  6. After you have saved the template, you will be provided with an area where you can write the content of the template. The ‘Insert Tag…’ button will bring up a menu of all the options that you have to dynamically supply information to the template.
  7. I chose to hardcode my username, use the Date Time and Interactive Tag. I also used the Cursor Position tag to set the cursor right between the BEGIN and the END markers so that I can immediately start coding there. It ends up looking like this:

    Template code: START OF CHANGE with DateTime and Variable tags, a cursor line, then END OF CHANGE

  8. Now save it and go back to the main editor screen. Start typing in ‘change’ and you will see a small tooltip appear above this text with the description that you had used before.

    ABAP editor with 'change' typed and a code modification tag tooltip above it

  9. Press Tab now, and you will see a popup. This is for the Interactive Tag. You can add a description or a tracking number here.

    Enter Template Parameter popup asking for a variable name, with CR123 entered

  10. Insert some value and press OK. The template will be inserted with the cursor focus on line 5.

    Inserted template: START OF CHANGE with date, time and CR123, an empty line 5, then END OF CHANGE

Now you can keep using this whenever you need to modify an existing piece of code.

Share