Thursday, 18 March 2021

Dynamic scripts in ABAP? Where and How

Where

Even if you don’t familiar with JavaScript templating engines which of the following statements is more straight forward and clear?

SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Exam Prep, SAP ABAP Preparation
JavaScript language in templating engine & LiveCycle Designer

In the left part of the image is easy to find IF statement. On the right part we have to “drilldown” to the source code and then try to refresh our memory JS syntax (or even FormCalc). Maybe in SAP world instead of these scripting languages ABAPScript would be more suitable?

How


Perhaps in reports already prepared data will be the best choice for templates (or views in MVC terminology).

Imagine we already have the structure like that

SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Exam Prep, SAP ABAP Preparation

and just want to add a.m. or p.m. texts for the TIME field. We can easily create new “TIME_AMPM TYPE STRING” field to a model.

But a view already has necessary the TIME field. So could we change responsibility from a model to a view?

In the other words is it possible to move ABAP code to a Pdf or Excel template itself?

DATA(result) = COND #( WHEN value-time GE '120000'
                       THEN 'post meridiem'
                       ELSE 'ante meridiem' ).
 
Do you think a view like that will be more readable than FormCalc script. Don’t you?

SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Exam Prep, SAP ABAP Preparation
No FormCalc nor JavaScript

SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Exam Prep, SAP ABAP Preparation
No VBA. Just familiar COND #( )

In dynamic script the special “value” filed refers to the whole structure. 

Let’s take a look to the R-T table. Usually there was a special field in ts_rand_data structure for numeration. Something like that “index TYPE sytabix,”

      " Random table data
      BEGIN OF ts_rand_data,
        group   TYPE string,
        caption TYPE string,
        date    TYPE d,
        sum1    TYPE bf_rbetr, " P with sign
        sum2    TYPE bf_rbetr, " P with sign
      END OF ts_rand_data,
      tt_rand_data TYPE STANDARD TABLE OF ts_rand_data WITH DEFAULT KEY,

      " Document structure
      BEGIN OF ts_root,
        title    TYPE string,
        t        TYPE tt_rand_data,      " internal flat table ( In template {R-T} )
        date     TYPE d,                 " 8
        time     TYPE t,                 " 6
        datetime TYPE cpet_reftimestamp, " 14 = date(8) + time(6)
      END OF ts_root.

Now we can omit the entire condition with WHEN part and use sy-tabix system filed.

SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Exam Prep, SAP ABAP Preparation
You can freely use any ABAP expression that returns a result.

Technical implementation


As you already guessed under the hood for every structure with cond expression the XTT library calls GENERATE SUBROUTINE to create SUBROUTINEs and then PERFORM them.

◉ a maximum of 36 temporary subroutine pools can be created. That’s why within the R structure you can use up to 36 nested tables & structures with cond expression.

◉ If template script doesn’t have leading WHEN statement it will be executed as is. VALUE #( ) REDUCE #( ) etc are not prohibited

◉ For sake of  backwards compatibility COND statement converted to IF-ELSE(ELSEIF)-ENDIF. “&&” converted to CONCATENATE (string templates |{…}| remain as is)

Catching errors


What happens if the expression throws an exception?
For example, should line_exists() be checked before table [key]?

SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Exam Prep, SAP ABAP Preparation

SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Exam Prep, SAP ABAP Preparation

Exceptions in COND are caught. And an empty value of a certain type will be returned.
In DEV & QAS, a warning window will pop up
In PROD, the report will behave “quietly”

SAP ABAP Tutorial and Material, SAP ABAP Certification, SAP ABAP Exam Prep, SAP ABAP Preparation

* If you have syntax error in COND the log will display “red” errors.

Source: sap.com

No comments:

Post a Comment