Wednesday 24 March 2021

Benefit from Using Code Templates When Creating Data Models

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?

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, templates are grouped in 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)”.

2. The remaining templates can contain one or more source code pattern(s). They do not have any additional label.

ABAP Development, ABAP CDS Tools, SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

Note that 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 datasource

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

ABAP Development, ABAP CDS Tools, SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides
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.

ABAP Development, ABAP CDS Tools, SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides
Preference to create and edit templates

You want to adapt an existing code template.

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

There are code templates for access controls, annotation definitions, metadata definitions, and so on.

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.

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

Note:

1. Some templates are deprecated and no more updated by SAP. They are labeled with the suffix “- deprecated”.

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

ABAP Development, ABAP CDS Tools, SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides
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:

1. Some templates are deprecated and no more updated by SAP. They are labeled with the suffix “- deprecated”.
2. 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.

ABAP Development, ABAP CDS Tools, SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides
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. 

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.

ABAP Development, ABAP CDS Tools, SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

4. Select the template.

The template is 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.

ABAP Development, ABAP CDS Tools, SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

4. Choose the Create icon from the toolbar.

ABAP Development, ABAP CDS Tools, SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides
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. To directly insert the pattern in the DDL editor using Ctrl + Space, choose the Automatically insert checkbox.

8. Enter a Description to provide a short text.

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

ABAP Development, ABAP CDS Tools, SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides
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.

ABAP Development, ABAP CDS Tools, SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

Editing a Template

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

ABAP Development, ABAP CDS Tools, SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides
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.

ABAP Development, ABAP CDS Tools, SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides
Delete button from the toolbar

To confirm your changes, choose OK.

Using Templates for ABAP Core Development


ABAP core development focuses on use cases when creating, editing, testing, debugging, and profiling ABAP development objects in an Eclipse-based IDE. The relevant development objects are, for example, ABAP classes, ABAP programs, function modules, and so on.

In this context, you can also use SAP’s default code templates or create your own ones as source code patterns. This means, in contrast to data definition templates, you 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.

ABAP Development, ABAP CDS Tools, SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides
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.

No comments:

Post a Comment