ABAP Editor/Workbench Custom Dynamic Patterns

In my last post ABAP Editor dynamic patterns, We saw how we can use the patterns to our advantage.

Today, I wanted to expand upon further features of dynamic patterns, which is the ability to write and add your own dynamic patterns.

Let's say you want to define a template for the generation of DB validation code and provide your table name as input.

Creating the Pattern.

From the ABAP editor,
Select Utilities > More Utilities > Edit Pattern > Create Pattern and enter a name for your pattern.


ABAP Editor Create new Custom Pattern

Type name for the pattern:

ABAP Editor Create new custom pattern Name

You will be presented with a text editor screen, Add the following code:
*$&$MUSTER
The above code tells the runtime to call a function module with the naming convention:
<pattern_name>_editor_exit. >ABAP Editor Create new custom pattern Code

The next obvious step is to create the FM with the above-discussed name.
The FM has a defined interface of a TABLES parameter BUFFER of type RSWSOURCET.
The table is used to pass back the code to the editor.

ABAP Editor Create new custom pattern FM
Insert Below code in the source code tab.
DATA lt_sval TYPE STANDARD TABLE OF sval.
DATA ls_sval TYPE sval.
DATA:
lv_ret TYPE char1,
lv_buffer TYPE LINE OF rswsourcet.
"Get tablename using Popup.
ls_sval-tabname = 'DD02L'.
ls_sval-fieldname = 'TABNAME'.
ls_sval-field_attr = '00'.
APPEND ls_sval TO lt_sval.
CALL FUNCTION 'POPUP_GET_VALUES_DB_CHECKED'
EXPORTING popup_title = 'Table Name'
IMPORTING returncode = lv_ret
TABLES fields = lt_sval
EXCEPTIONS OTHERS = 2.
IF sy-subrc <> 0.
RETURN.
ENDIF.
READ TABLE lt_sval INTO ls_sval INDEX 1.
IF syst-subrc <> 0.
RETURN.
ENDIF.
"Create COde..
lv_buffer = '"Validate Input against Table:' && ` ` && ls_sval-value.
APPEND lv_buffer TO buffer.
APPEND 'Select COUNT(*)' TO buffer.
APPEND ' UPTO 1 ROWS' TO buffer.
lv_buffer = | FROM { to_upper( ls_sval-value ) } |.
APPEND lv_buffer TO buffer.
APPEND ' WHERE col_name = p_inp.' TO buffer.
APPEND '"Error Handling.' TO buffer.
APPEND 'IF SYST-DBCNT <> 1.' TO buffer.
APPEND ' MESSAGE E000(DB) WITH ''No data fround for input.''.' TO buffer.
APPEND 'ENDIF.' TO buffer.

Call the new pattern:

ABAP Editor Call  custom pattern

Input table name:ABAP Editor Call  custom pattern

Generated Code:

ABAP Editor Call  custom pattern
So there you have it, Your own Dynamic pattern to help you out with writing DB Existence check.

Offcourse, You can expand upon it or create your own pattern with extra functionalities.
As always, Hope you like the post and use the information in your day-to-day work.

Popular posts from this blog

ABAP convert internal table to excel (.xlsx) format and Send email or download

ABAP read excel(.XLSX) file to internal table in ABAP using CL_FDT_XL_SPREADSHEET

Use CL_BCS library to send email with ABAP