Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member215344
Contributor
9,778

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.

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

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.

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

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

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

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.

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.

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 )

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.

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

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

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.

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.

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( ).

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

126 Comments
raymond_giuseppi
Active Contributor
0 Kudos

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

Former Member
0 Kudos

Dear Ajay,

Simple but very useful informations.

former_member185177
Contributor
0 Kudos

Hi Ajay,

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

Appreciate your efforts to share this blog.  :smile:

Regards,

Krishna Chaitanya.

former_member215344
Contributor
0 Kudos

Thanks for the info Raymond. I will update the blog accordingly.

Thanks,

Ajay Bose

Former Member
0 Kudos

Good Sharing Bro.....Nice

harshsisodia31
Participant
0 Kudos

Thanks Ravi. Really helpful for daily coding.

thangam_perumal
Contributor
0 Kudos

Really Good.. More Helpful for me..

former_member186413
Participant
0 Kudos

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

Former Member
0 Kudos

Useful info. thanks for sharing..

former_member215344
Contributor
0 Kudos

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)

Former Member
0 Kudos

This was indeed an interesting read! Thank you

NagaPrakashT
Contributor
0 Kudos

Good information... Thank you ajay.

sandroramos
Active Participant
0 Kudos

Great Job!!!

10th point is new to me. :oops:

Thanks for sharing.

former_member183824
Participant
0 Kudos

Thanks for sharing. Very gud & helpful.

Jitendra_Kansal
Product and Topic Expert
Product and Topic Expert
0 Kudos

nice information. Thanks for sharing !!!

alejandro_bindi
Active Contributor
0 Kudos

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

Former Member
0 Kudos

Thank you Ajay, everything is very interesting.

Congratss

arnab_das5
Explorer
0 Kudos

Excellent :smile:

jorge_andrade
Explorer
0 Kudos

Excellent.

Regards.

Jorge Andrade

Former Member
0 Kudos

Hello Eric,

Your reply reminds me of a blog from horst.keller (though not exactly a related topic but very useful nevertheless )

With new release 7.4 we have inline declaration feature, which will make our life simple :smile: .

DESCRIBE TABLE itab LINES DATA(count).

IF count = 0.

     MESSAGE 'No Data' TYPE 'I'.

ENDIF.

or

DATA(count) = lines(itab).


Exciting feature isn't it ?


those who have not read it yet, have look.


ABAP News for Release 7.40 - Inline Declarations

Former Member
0 Kudos

Nice, I have learned some programming tips that I don't know before read this post.

wol
Active Participant
0 Kudos

Thank you for Number 2!

marius_greeff
Active Participant
0 Kudos

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.

former_member215344
Contributor
0 Kudos

Hi Marius,

Thanks for sharing this useful information. Great job !!!

Ajay.

0 Kudos

Thanks a lot

former_member184367
Active Participant
0 Kudos

Thanks for sharing. This tips are really very helpful while coding.

Labels in this area