Background
This morning me and my colleague nikhil.maradi were checking odd behavior in a productive ABAP code, infact we were working on an incident. The problem was related to a SY-SUBRC field value being set as 0 in one system and 4 in another system for the same statement, sounds interesting right read on. This small blog will talk about this weird SY-SUBRC behavior and a very important learning for us.
Problem statement
Our production code is using CREATE DATA statement to create types based on variable.The work area has value of KNKK in it. As can be seen the SY-SUBRC value is being returned 4 in one SAP instance and 0 in another SAP instance.
Our Analysis and Solution
So the first step in finding the root cause of the issue was to check ABAP help does CREATE DATA statement sets any SY-SUBRC value if it fails. As always on searching the help here i could not find any note about the same also shown in screen shot.
On looking at the example as shown below we could see SAP example code is neither checking the SY-SUBRC after the create statement nor catching any exceptions also. We missed checking the executable example at this point else would have found something which we found later so read on!
So for the time being we assumed that somehow we don’t need to check SY-SUBRC neither catch any exceptions and our code will work. But still deep down our heart we were not convinced with the line of reasoning. We decided to dig more into ABAP help and read around as shown below.
This is where we found a meaningful note we found which actually helped in finding the justifications for our assumptions. In the parent help it was clearly mentioned that CREATE DATA will not set any SY-SUBRC
In addition we shall catch the exceptions if we want to trouble shoot.
After understanding the detail the modified sample code looks something like below and same concept is incorporated in the productive code after removing the SY-SUBRC check.
DATA:dref TYPE REF TO data,
tablename TYPE tabname16 VALUE 'ZKNKK'.
TRY.
CREATE DATA dref TYPE (tablename).
CATCH cx_sy_create_data_error.
cl_demo_output=>display( |Error creating { tablename }| ).
ENDTRY.
In case a type does not exist the error is also displayed.
Learning for all ABAPers
As an ABAPer we tend to put SY-SUBRC check in most of the places by assuming that this field will be set, which is not true as seen above. So takeaway for us is as follow
- DO NOT ASSUME, refer ABAP help documentation and exmaple in detail.
- A way must exist determine whether the statement worked or not via SY-SUBRC,Catching Exceptions etc.
It will be great if our fellow community members can share some of such crazy leanrings in the comments below.