Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
wolfgang_woehrle
Product and Topic Expert
Product and Topic Expert
Dear ABAP CDS community,

this blog informs you how to work with source code templates when developing data models in ABAP Development Tools (ADT) a. k. a. ABAP in Eclipse.

When creating a data definition, do you always add the same parts of DDL source code to your CDS objects? Feel free to get rid of such boring activities and read my blog.

If you prefer watching my video instead of reading, please look here:


Use


You can benefit from SAP's templates or create your own templates when creating a data model. Templates help you to reduce effort from repeating work.

You can use templates providing the whole structure of a CDS object (respective the CDS entity) or  source code patterns that are frequently used.

In ADT, the CDS templates are grouped in the following contexts in accordance to the type of CDS entity for which they can be created and their use case:

  1. Templates which contain the whole structure of the CDS entity are labeled with the suffix "(creation)". When creating a CDS object, only these templates will be proposed in the creation wizard.

  2. Creation templates in the "deprecated" context can still be used. If you have created your own creation templates in this context, please reassign them.

  3. The remaining templates can contain one or more source code pattern(s).



Note: SAP's default templates are always provided for the latest back-end version. This means that some of the newest templates may not be handled from your back-end if you use an older version.

From Where Can I Create Data Definitions?


You can create a data definition from the ...

  • Menu bar: From the menu bar File > New > Other > Core Data Services > Data Definition

  • Project Explorer: From the context menu of the Core Data Services object group in your Favorite package

  • Project Explorer: From the context menu of a referenced object, for example, a database table or another data definition that is used as data source.

  • Source code editor: You want to rework an existing data definition on base of a code template.



Entry from the context menu to trigger creation of a data definition



From Where Can I Get Code Templates?


Preference Page


The Templates preferences page for data definitions can be opened from the Window > Preferences > ABAP Development > Editors > Source Code Editors > Data Definition Templates menu.


Preference to create and edit templates


You want to adapt an existing code template.

  1. In the TOC, select the type of the CDS object for which you want to add changes.

  2. From the Templates section, choose the relevant button.
    The relevant dialog will be opened.

  3. Start editing.
    You can use the same features (create, edit, and delete/remove) as they are provided in the
    Templates view.

  4. If you want to redo your changes, choose the Restore Defaults button from the bottom line in the Templates section.


Note: You can only create, edit, and delete templates in the Templates view and the Preference page.

Template View


You can open the Templates view from the Window > Show View > Templates menu.

Note: Before you can display the data definition templates, you have to open a data definition in the DDL editor. The templates will then be displayed as follows:


Sample for the representation of templates in the Template view


The displayed list contains the available templates.

From the toolbar, you can create, edit, and delete templates as well as open the according preference page. See the how-to steps below.

Note: You can only create, edit, and delete templates in the Templates view and the Preference page.

Creation Wizard


In the creation wizard, you can only select the templates from the Templates page.


Sample for a wizard page to insert a template


The template page provides the following possibilities:

  1. "Use the selected template" checkbox: This checkbox is selected by default. If you want to create a data definition from scratch without any template, deselect it.

  2. Select the template of your choice from the list of the available templates.

  3. Get some further information of the selected template from the short description.

  4. Get an overview from preview of the source code of the selected template.


Note

  1. You can create a data definition from the context menu of another data definition or a database table (from a so-called referenced object). In this case, the templates will be available in accordance to the position from where you have triggered creation.  For more information, see here.

  2. When you select a template and create a data definition the next time, the creation wizard will automatically propose the same template which you have used previously. In this case, you can finish the creation process already on the Transport page.


How to Work With Templates?


Note: In the context of ABAP Platform classic, the use of CDS annotations from data definitions depends on the ABAP language version. This means, some CDS entities might not be available in your back-end. Consequently, you might need to create different versions for a template.

Inserting a Template in a Data Definition


You want to add a template into your data model.

  1. Open the data definition.

  2. Navigate to the relevant position where you want to add the code template or remove the existing source code.

  3. Start typing the name of the template and use code completion (Ctrl + Space).
    The content assist dialog is opened and displays the possible ABAP CDS keywords and templates.

  4. Select the template.
    The template will be added.

  5. Use tab key to navigate to the name placeholders and adapt the source code in your data definition.


Creating a Template


You want to create your own template for a pattern that reflects your requirements in the DDL source code.

  1. Open a data definition.

  2. Open the Template view.

  3. Select the node where you want to add your template.

  4. Choose the Create icon from the toolbar.


    Create icon from the toolbar


    The New Template dialog is opened.

  5. Enter the Name of your template.

  6. Select the Context to classify your template.
    Note: When you create a template for the whole structure of a CDS entity, choose the "(creation)" context. For the templates containing only source code patterns, use the context without any additional suffix.

  7. Enter a Description to provide a short text.

  8. Enter your customer-specific Pattern. At the relevant position, choose Ctrl + Space and select the relevant variable from the code completion list. Note that this pattern already contains  placeholders for possible variables.


Sample of the configuration from a new template in the New Template dialog


     7. To save your changes, choose OK.

The template will be added in the Templates view.



Editing a Template


In the Template view, select the template to be edited and choose the Edit icon from the toolbar.


Edit button from the toolbar


In the Edit dialog, add your changes as described above.

To confirm your changes, choose OK.

Delete a Template


In the Template view, select the template to be deleted and choose the Delete icon from the toolbar.


Delete button from the toolbar


To confirm your changes, choose OK.

Sharing a Template


You import and export templates to share them with your development team.

In the Templates preference page, select the relevant templates and choose the Import... or Export... button.


Buttons to import and export templates from the template preference page


In the dialog of your file explorer, select the relevant file location and confirm with OK.

Using Templates for ABAP Source Code


While developing ABAP source code, you can also use SAP's default code templates or create your own ones as source code patterns. In contrast to data definition templates, templates do not provide the whole structure of a development object as a template. Here, you will use source patterns, such as an IF block, CASE block, function module parameter block, and so on.


Sample for the Template view of an ABAP class where a template is selected


You can here also create, edit and delete the templates for the patterns.

Further Information


In this context, you can find the following help content, provided on the SAP Help Portal