Overview
In the last few posts, I demonstrated you some new features of the 7.4 Release in detail: the different Inline Declarations (DATA or FIELD-SYMBOL), the VALUE and NEW Constructor Expressions, or the Table Expressions.
Today, I want to do a bit different, than before. For this, I have collected the best improvements of our ABAP language in the Support Package 02, and I would like to share it with you in a single post.
Operand position in CALL FUNCTION ... EXPORTING
First, let's talk about an improvement of the Function Module calls. Before 7.4, we cannot pass any function or expression as actual parameter, instead, first we evaluated the expression and saved its result in a variable, and then we passed this variable as actual parameter of the Function Module.
For example, the GUI_UPLOAD function module needs a string value for its filename parameter, but what if we have the filename with the type, rlgrap-filename (that actually is CHAR 128)? We define a string variable, pass the filename to the string variable, and then pass the string variable as actual parameter of the function module.
... filename TYPE rlgrap-filename.
DATA filename_as_string TYPE string.
filename_as_string = filename.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = filename_as_string
has_field_separator = 'X'
...
From 7.4, our task is much easier, since we have Constructor Expressions (like NEW, VALUE, CONV and so on), and we are able to pass functions and expression as actual parameters!
So now, we can simply define a conversion expression with the help of CONV operator in order to convert our CHAR 128 filename to string, and pass it as an actual parameter, in one single line!
... filename TYPE rlgrap-filename.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = CONV string( filename )
has_field_separator = 'X'
...
Note
- we cannot specify substring expressions in case of string and xstring types after EXPORTING,
- we cannot specify types generally with #, since the types of the parameters are not determined until runtime (unlike in method calls, here the CONV #( filename ) doesn't work).
Parameter Interface of Functional Methods
Before 7.4, we could only specify importing parameters besides the returning parameter in a functional method. Thanks to 7.4, we are able to define functional methods that can have any number of other formal parameter (EXPORTING, IMPORTING, and CHANGING), besides the RETURNING.
Note
I recommend to use this feature, if you really need it (most of the cases, you won't need it), and try to keep your interfaces as thin as you can! "SRP - Single Responsibility Principle" and "KISS - Keep it simple, stupid" are your friends! :)
...
CLASS cl_employee_repository DEFINITION.
PUBLIC SECTION.
METHODS create IMPORTING employee TYPE REF TO cl_employee
EXPORTING employee_code TYPE empcode
RETURNING value(succeeded) TYPE abap_bool.
ENDCLASS.
...
Interfaces in Test Classes
The next feature is very interesting for TDD developers, or who does unit testing. Before 7.4, creating test doubles (like mock, spy, or stub) for heavy interfaces was a nightmare, since you had two choices: implement all methods of the interface or having many empty method implementation in your test double.
In 7.4, if you don't want to implement all the methods of an interface, then simply place the addition, PARTIALLY IMPLEMENTED at end of the INTERFACES statement, and from now on you only have to implement the methods that you really need.
...
CLASS cl_mock_service_client DEFINITION FOR TESTING FINAL.
PUBLIC SECTION.
INTERFACES if_service_client PARTIALLY IMPLEMENTED.
ENDCLASS.
...
Identity Transformation Support for JSON
Today, RESTful web services are very popular. If you have to consume a web service today, I'm pretty sure you have to deal with some RESTful APIs, and you get your data often in JSON format. Before 7.4, we could parse a JSON many way, I preferred my favorite, using Simple Transformation. In 7.4, SAP goes further and give us the Indentity Transformation that is actually an XSLT transformation delivered by SAP, and can make our life much easier.
Let's say we have consumed a REST service, and got the following JSON back that we want to parse into ABAP data (for example internal table):
...
DATA(json) =
'{' &&
'"FLIGHTS":' &&
'[' &&
'{' &&
'"MANDT":"800",' &&
'"CARRID":"AA",' &&
'"CONNID":"0017",' &&
'"FLDATE":"2009-02-11",' &&
'"PRICE":422.94,' &&
'"CURRENCY":"USD",' &&
'"PLANETYPE":"747-400",' &&
'"SEATSMAX":385,' &&
'"SEATSOCC":370,' &&
'"PAYMENTSUM":192057.12,' &&
'"SEATSMAX_B":31,' &&
'"SEATSOCC_B":31,' &&
'"SEATSMAX_F":21,' &&
'"SEATSOCC_F":19' &&
'},' &&
'{' &&
'"MANDT":"800",' &&
'"CARRID":"AA",' &&
'"CONNID":"0017",' &&
'"FLDATE":"2009-02-12",' &&
'"PRICE":422.94,' &&
'"CURRENCY":"USD",' &&
'"PLANETYPE":"747-400",' &&
'"SEATSMAX":385,' &&
'"SEATSOCC":370,' &&
'"PAYMENTSUM":192057.12,' &&
'"SEATSMAX_B":31,' &&
'"SEATSOCC_B":31,' &&
'"SEATSMAX_F":21,' &&
'"SEATSOCC_F":19' &&
'}' &&
']' &&
'}'.
...
There is nothing to do, than getting a target object (for example the parsed_flights internal table) that fits to the source JSON, and calling the XSLT transformation, called id with the source JSON data, and the target ABAP object (in our case an internal table). That's it! No need to create any XSLT or ST transformations! That's a really handy feature, I love it!
...
DATA parsed_flights TYPE STANDARD TABLE OF sflight.
CALL TRANSFORMATION id
SOURCE XML json
RESULT flights = parsed_flights.
...
Authorization Check in CALL TRANSACTION
The last feature in SP02 is about the CALL TRANSACTION statement. From 7.4, SAP provides us two new additions for this statement: WITH AUTHORITY-CHECK and the WITHOUT AUTHORITY-CHECK.
In the case of the WITH AUTHORITY-CHECK, the system checks the authorization of the current user to execute the called transaction, using the S_TCODE and any other authorization object entered in SE93. If the currect user doesn't have authorization for the given transaction, an exception of the class CX_SY_AUTHORIZATION_ERROR is raised.
In the case of the WITHOUT AUTHORITY-CHECK, there is no authorization check.
So with these additions, SAP has merged the CALL TRANSACTION and AUTHORITY-CHECK statements together.
...
SET PARAMETER ID: 'XUS' FIELD sy-uname.
TRY.
CALL TRANSACTION 'SU01' WITH AUTHORITY-CHECK.
CATCH cx_sy_authorization_error.
" handling errors
ENDTRY.
...
Summary
Although these new features seem small improvements, but there are cases when they can help you a lot! My favorites are the Identity Transformations for JSONs, and the partially implemented interfaces for Test Doubles! Of course there are more of them (check Release Notes), but I found these the most important that I should share with you!
Do you have any experiences with these new features? If yes, then share with us below!
Now, go and try it out! If you don't have any access to any 7.4 based SAP system, then I recommend to watch my colleague's setup guide.
Stay tuned, keep reading! If you want to get notification about the newest posts, follow me or subscribe to our newsletter!
If you liked it, please share it! Thanks!