Skip to Content

Introduction:

Dear Friends,

The purpose of this blog is to reveal few interesting facts and techniques in ABAP that I came across during my ABAP career. While I am pretty sure that most of you are aware of these things I still wanted to publish these things for the benefit of my other ABAP programmer friends who are not aware.

I also urge you to get involved in this blog and share any such interesting facts or techniques in ABAP that can be of any use.

1. The first interesting technique which I am sharing is the use of a Case statement as an alternative for if..else chain

Suppose you have several radio buttons in your selection screen and you need to check which of them has been selected by the user and write logic accordingly. A conventional way of achieving this is by way of using if else statement like below:

If p_rad1 EQ ‘X’.

      <do something>

elseif p_rad2 EQ ‘X’.

      <do something>

elseif p_rad3 EQ ‘X’.

      <do something>

elseif p_rad4 EQ ‘X’.

      <do something>

endif.

Now here comes the shortcut. Instead of writing ‘N’ number of if else conditions you can write the code like:

CASE ‘X’.

     WHEN p_rad1.

          <do something>

     WHEN p_rad2.

          <do something>

     WHEN p_rad3.

          <do something>

     WHEN p_rad4.

          <do something>

      WHEN OTHERS.

ENDCASE.

The control will automatically branch to the selected radio button πŸ™‚

2. You can set an icon before the selection text for radio buttons, check boxes and input fields.

Just go to the selection text and put the icon code before the selection text like given below. Save and activate. The icon code can be found in the type pool ICON for each icon.

icon 1.jpg

Once you Run the program, you can see that the icon has been placed accordingly before the selection text.

icon 2.jpg

3. Domain with fixed values ‘X’ and ‘ ‘ is automatically converted as a checkbox.

For example, if you create a PARAMETER in your selection screen which refers to a dictionary domain with fixed values ‘X’ and ‘ ‘ only the system automatically converts it to a checkbox even if you don’t give the keyword CHECKBOX in your PARAMETER statement.

checkbox 1.jpg

Here the data element WDY_BOOLEAN refers to a data domain WDY_BOOLEAN which has only two fixed values: ‘X’ and ‘ ‘ .

checkbox 2.jpg

You can see that the parameter is automatically converted to a checkbox even if there was no AS CHECKBOX addition mentioned in the program.

checkbox 3.jpg

This is true not only in case of selection screen but also in normal dialog screens. If you have created any database table and you have any of your table field referring to a data domain like mentioned above, while generating the Table maintenance generatorthe system automatically converts those fields to checkboxes, provided there are only two domain values existing for that domain i.e. ‘X’ and space. Thus we don’t have to do any additional effort for checkboxes while creating a table maintenance dialog.

4. We can send a pop-up to another SAPGUI user using the function module TH_POPUP

popup 1.jpg

The pop-up opens up at the receiver’s SAPGUI if the user is already logged in.

5. Function codes of a program can be entered directly in the command field (without using /n command)

The function codes of a program in the PF-STATUS can be triggered by entering the function key in the command field. This is similar to triggering the function code by clicking on a button.

A few examples:

Go to SE38, select any program name and enter STRT in the command field (without the prefix /n). You can see that the program has started executing. Now if you check the PF-STATUS for the program you can see that STRT is the function key for Execute button.

Time constraint 9.jpg

Similarly, Go to transaction code CMOD. Now in the command field put the transaction code PRFB (without the prefix /n) and hit enter. You can see that the transaction for field exit is opened in the same window. Likewise we use JDBG in SM37 to debug background jobs.

6. Domain value checks are not considered when a table record is updated from program.

Many people think that restricting data domain with values will still be checked if we update the database table through an INSERT, UPDATE or MODIFY statement. However this is not true. Only foreign key checks are considered in database updates.

Domain checks are only enforced in screens where they are placed. That means if user input data in a screen field which refers to a data domain, the check is done. But if you are inserting records to any database table from a program directly, this check is not enforced thereby resulting in updating records with invalid domain values. So we have to be sure that the data we update is correct.

7. ABAP editor shortcuts

In his blog http://scn.sap.com/community/abap/blog/2013/09/17/shortcuts-that-can-make-an-abaper-life-easier, Yuvaraj S has mentioned numerous keyboard shortcuts that indeed make an ABAP’ers life easier πŸ™‚ Hats off to you Yuvaraj !!!

8. CL_GUI_FRONTEND_SERVICES, a powerful class

Ever wondered how we could get OS level information from SAP system such as Operating System Platform, Computer Name, IP Address etc or perform file operations like File execute (open a file), File Copy, File delete etc. or calculate how much drive space is free in the drive. Well this is a powerful class which gives you all of these. You get almost every detail by calling the static methods of this class.

/wp-content/uploads/2013/10/cl_gui_291203.jpg

9. Using lines keyword instead of DESCRIBE TABLE.

To get the number of records in an internal table, we generally use the statement DESCRIBE TABLE itab LINES count.

We can also get the number of records as follows:

count = lines( itab ).

10. Creating tables with time dependency.

Did you know that we could create a database table with de-limit option ? Well if you want to create such a table with time dependent entries do the follow steps:

i) First create your table with required fields. The table/view must also contain two fields for the begin and end dates. One of these fields must be in the key, the other must be the first non-key field in the table/view.  Here I have made ENDDA as the key and BEGDA as the non key which comes immediately after ENDDA.

(For more information check SAP documentation:  Time-Dependent Table/View – Customizing (BC-CUS) – SAP Library )

Time constraint 1.jpg

ii) Now after generating the table maintenance dialog for your table through table maintenance generator, activate time dependency from Menu-&gt;Environment-&gt;Generate Time-Dep.

Time constraint 2.jpg

iii) Now go to SM30 and click on Maintain button, you can see that a “DELIMIT” button has been automatically generated by the system.

Time constraint 4.jpg

iv) Now if I enter a record which overlaps with the existing BEGDA – ENDDA dates system will give an error like this:

Time constraint 6.jpg

v) Now press enter and save the records. Now open the table in SE16 and you can see that the system has de-limited the entries according to the new record.

Time constraint 8.jpg

However if you open SM30 again with the same table, you can see that the system will show only those valid records which are valid as on current date.

Time constraint 7.jpg

You can also use the delimit button to delimit entries likewise.

11. Displaying ALV without creating field catalog

We usually create the field catalog for an ALV before calling the ALV output through function modules likes REUSE_ALV_GRID_DISPLAY, REUSE_ALV_GRID_DISPLAY_LVC etc. The main purpose of the field catalog is to set field information like field position in output, column text, output length and various other properties. But what if you don’t want to mention these properties explicitly. You want it based on your data internal table only.

To be more precise, in cases where I want the field position or other field properties to be derived from my internal table which contains the data, I can use the ALV object model to create an ALV output without creating the field catalog. The field properties are automatically derived from your internal table which contains the data. The field position comes from the field position in the internal table and likewise other properties like column text will come from the data element to which your internal table field refers to.

Well most of you are aware of this ALV object model and how it is called. For those of you who are not aware, this is all you have to do. Check the example below. All you have to do is call two methods, that’s it !!!

1. Call method FACTORY to create the ALV object.

2. To display the ALV output on the screen, call up the method DISPLAY afterwards.

Sample Code:

data: gt_outtab type table of SFLIGHT.
data: gr_table type ref to cl_salv_table.

*… Select Data
   select * from SFLIGHT into corresponding fields of table gt_outtab.

*… Create Instance
   call method cl_salv_table=&gt;factory
      IMPORTING
         R_SALV_TABLE = gr_table
      changing
         t_table = gt_outtab.

*… Display Table
   gr_table-&gt;display( ).

factory ALV.jpg

Similarly you can call a Hierarchical ALV, Tree ALV etc. Please check the SAP help for more information:

http://help.sap.com/saphelp_nw04/helpdata/en/f9/1ab54099de3726e10000000a1550b0/content.htm

Finally as I said, these were few things which I had come across during my programming days. I am sure that you will also have something to share.

Lets collaborate, share and learn in SCN !!!

Thanks and Regards,

Ajay Bose

To report this post you need to login first.

126 Comments

You must be Logged on to comment or reply to a post.

  1. Satish Kumar Balasubramanian

    Hi Ajay,

    Very useful Information, Thank you so much, Hearty congragulations for your effort. πŸ™‚

    I have a small concern with the 10th point, in creating table with delimit option.

    you have mentioned as

    ” BEGDA also will be a part of your key field(s)”

    but when you are creating the table you have made ENDDA as the key field, is there anything wrong or did i understand wrongly?

    Please clear this doubt for me πŸ™‚

    Thanks once again πŸ™‚

    Regards,

    Satish

    (0) 
    1. Ajay Bose S Post author

      Hi Satish,

      It was a mistake. Its ENDDA only which has to be a part of the key. I have updated this. Thanks for correcting.

      Thanks,

      Ajay Bose

      (0) 
  2. sampath kumar

    Hi ajay,

             i think performance wise  ‘case’ statement is better than ‘if’.

         your 11 points is very useful to me… Hats of to you sir…

    Regards,

    Sampath kumar

    (0) 
  3. Raymond Giuseppi

    Just a remark on table dependency, BEGDA and ENDDA are not the only options, look at online help.sap.com, also the key field can be the start or the end date, and the other date must be the next field in table definition (first non key field)

    Regards,

    Raymond

    (0) 
  4. Krishna Chaitanya

    Hi Ajay,

    Nicely illustrated.  Very good info.  Really it will be helpful to all of us. 

    Appreciate your efforts to share this blog.  πŸ™‚

    Regards,

    Krishna Chaitanya.

    (0) 
  5. Hai Wang

    Thanks for your sharing, and Would be nice, if you wish to explain the last part, that regards to 11. Displaying ALV without creating field catalog

    (0) 
    1. Ajay Bose S Post author

      Hi Hai Wang,

      This point explains the process of calling an ALV report without creating a field catalog. We usually invoke an ALV report by calling function modules REUSE_ALV_GRID_DISPLAY, REUSE_ALV_LIST_DISPLAY etc. When we call these function modules the mandatory parameter FIELDCATALOG is required to give information about column position, field label etc. With this factory ALV this parameter is no longer required and all the information is taken directly from the internal table which holds the actual data.

      Please refer the below link for more info:

      ALV Output Display (SAP Library – ALV Object Model)

      (0) 
  6. Alejandro Bindi

    Good compilation. But an item should be corrected:

    Many people think that restricting data domain with values will still be checked if we update the database table through an INSERT, UPDATE or MODIFY statement. However this is not true. Only foreign key checks are considered in database updates.

    Foreign keys aren’t considered neither, only in dialog, that is screen input. You can DELETE an entry referenced in a foreign key without problems. Referential integrity is not checked in ABAP (that’s why you must be extra careful when updating tables).

    Refer Use of foreign key check in ABAP reports

    (0) 
  7. Marius Greeff

    This was really help full. Also got me thinking what I can share.

    In the new Abap editor pressing Ctrl+Space will bring up code completion. When typing a table name and ctrl+space will list tables starting with the name and also works with field names and function modules.

    With single click on the function module you would like you can then press shift+enter that will complete the function module pattern and include field descriptions.

    With so many comments this may be lost. I haven’t read all the comments so I would not know if somebody as already mentioned this. If so, sorry for duplicating.

    (0) 

Leave a Reply