How to recover deleted ABAP Program?

Tip - Retrieve Deleted ABAP Program



Problem

Sometimes crazy things happen. People just delete the program assuming it is no longer required. Few days later, the same program is needed. But it doesn’t exist anymore. 

Solution

Create a program with the same name as it existed before. Once the program is created, we can go into version history. The previous program version still exists. So we can retrieve that version, activate and transport using a new transport request.

How to modify the Standard PF-Status of ALV Grid?

Last Monday, I was on-leave at work as I need to go to the hospital for my last session for my upper back therapy. My wife accompanied all day which turned to be a date at the hospital. How sweet we are. :) Upon my return yesterday to work, I was loaded with so many tasks. All the developments I have finished last week were being tested by my counterpart. As expected, bugs are found. So all of them want to finish it as deadline is fast approaching. I was under pressure yesterday as everyone says, "this object is critical. We need to deliver on time!" 

Really??? Anyways, I have done all those stuff within the day. :) But I was exhausted with all those bugs and enhancement requested. Lesson learned: "Dont take a leave while project is going on. Otherwise, you will be under pressure upon return".  Hehehe...

Never mind that crazy day. Everyone of us once feel that pressure. Once you experience it, just smile and stay focus. So today, I would like to share with you this simple tutorial on how to modify the standard PF-Status of an ALV Report using the FM ALV_GRID_DISPLAY.

To have an idea about this topic today, take a look with this sample screenshot below. The button "download" is added into a standard ALV PF-Status.

Basically, some functional consultants request to have this kind of design since it is beneficial to the users from further processing like for instance, in this case, download to excel file. 

So lets start this tutorial now with the following steps. But before I forget, I assumed you have already created your own custom report program. In this tutorial, I will not discuss further on how to create your first report program. But in case, you haven't created yet, you can view this simple tutorial in ALV Report using ALV_GRID_DISPLAY.

Steps:

1. Since what we would like to achieve is to modify the standard PF-Status, our first step is to copy the GUI Status from the standard program. So, go to tcode SE80 and enter the following program name: SAPLKBBL. This is the program that handles all the standard PF-Status related to ALV display.


Once you displayed the objects of the program SAPLKBBL, scroll down and look for the GUI Status folder (refer screenshot above). From the list of statuses, locate this standard one below, "STANDARD_FULLSCREEN". Actually, you can select your own too from the lists. As for this tutorial, I selected the standard_fullscreen status which I commonly used.


2. Once I selected the status that I prefer, right click your mouse to open the context menu. From the menu, select "Copy". This popup window appears afterwards. Enter here the name of your custom program on the parameter in yellow. Then click the copy button.



3. Go back to your custom report program. Open it in SE38. Display all object list. As you will notice, the standard PF-Status is added automatically into your custom program. Look this sample screenshot below. Dont worry will all the functionality of the PF-Status. It is the same with the standard as long as you will not change the function codes. 


4. Open the copied standard status like the normal way you do. Then from the list of function codes, add your own custom code. Well, In this tutorial, I added the function code "Download" which will appear in the ALV status. After you added, save and activate it.



5. In your ABAP code, set your customized status this way:
1
2
3
4
5
6
7
8
*&--------------------------------------------------------------*
*&      Form  pf_status
*&--------------------------------------------------------------*
*       Set the PF Status of the ALV Grid
*---------------------------------------------------------------*
FORM pf_status USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS  'STANDARD_FULLSCREEN'.
ENDFORM.     

Then in your Function Module, REUSE_ALV_GRID_DISPLAY, call the name of your form, PF_STATUS. Once you execute your report, your newly added button besides from the standard buttons will be displayed into your ALV Grid.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'PF_STATUS'
      i_callback_user_command  = 'F_USER_COMMAND'
      is_layout                = gwa_layout
      it_fieldcat              = gi_fieldcatalog[]
      it_sort                  = gi_sort
    TABLES
      t_outtab                 = gi_output
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
  IF sy-subrc = 0.                                          "#EC NEEDED
    "do nothing
  ENDIF.


So that's it. A simple process to modify the standard PF-Status of an ALV Grid. If you have any questions or suggestions, please let me know. Leave your comments below. Thank you once again and I invite everyone to connect with me on my page @ABAPTHINKERS. See you next time. :)

ABAP Code: ALV Report using FM ALV_GRID_DISPLAY

Last week, I received an email from one of our readers requesting a simple report tutorial using ALV GRID. Well, a report using an ALV is a simple one for an experienced ABAPer but for a fresher like him, it might be a complicated task. I understand him as I do became a fresher years back. When I am starting to learn ABAP, I have a senior ABAPer also who guided me as I developed. Thanks a lot to Master Jojit for his kindness and support during my greenhorn days.

For this tutorial our intended readers would be the beginners. To begin with, I summarize three major steps to display an ALV grid which I always do.
1. Inputs from Selection Screen
2. SELECT from Database Tables
3. Display using FM ALV_GRID_DISPLAY.


Input from Selection Screen
When creating a report, the first to consider is the Screen. A screen is where the user inputs data. Everything you see in SAP windows has a screen. There are two types of screens. The first one is the SELECTION-SCREEN and the second one is DIALOG SCREEN. There's a big difference between the two screens. One thing I can say is, in DIALOG SCREEN, you are the one designing the whole screen. There is a layout view in which you can position every component. If you are familiar with Visual Basic 6 or VB.net, this is similar with Forms. You can position the components like radio buttons, labels, textbox, dropdown list etc. anywhere from the screen.

Unlike SELECTION-SCREEN, this is not that case. When you declare components on it, it is vertically positioned unless you will implicitly declared to be aligned horizontally. That's one of the major difference of the two. Components positioning. I will not discuss further all the differences between these two types of screen. But I hope I give you a simple difference so that you have an idea what to use when you will develop your first report program.

Normally, when creating an ALV report, we are using the SELECTION-SCREEN. The PARAMETER and SELECT-OPTIONS are usually used here as input parameters. PARAMETER accepts single values while SELECT-OPTIONS accept multiple and complex values. Checkboxes and radiobuttons can also be declared here even a push button. For more information about selection screen, try this link.

Example on how to define a selection screen:
1
2
3
4
5
6
  SELECTION-SCREEN BEGIN OF BLOCK b01 WITH FRAME TITLE text-001.
  SELECT-OPTIONS carrid FOR sflight-carrid.
  SELECT-OPTIONS connid FOR sflight-connid.
  SELECT-OPTIONS fldate FOR sflight-fldate.
  PARAMETERS: p_maxrow TYPE i DEFAULT 30.
  SELECTION-SCREEN END OF BLOCK b01.

SELECT from Database tables
In this step, you are going to do a retrieval process from tables, custom or standard as long as it is maintained in the data dictionary (SE11). Normally, SQL statements is called at the event START-OF-SELECTION. This event is triggered when the user clicks the execute button.  For more information about events in selection-screen, click this link.

By the way, there are two methods in executing SQL statements. The first one is OPEN SQL and the second is, NATIVE SQL. If you are familiar with other databases like mySQL, MSSQL, ORACLE and other databases in the market, they are using the NATIVE SQL. In SAP, it is also possible to use such SQL statements. However, this is not being recommended as ABAP programs having database-specific SQL statements do not run with different databases. So if your company decides to change from one database to another, there might be a problem in the future with your custom ABAP codes.

So as an ABAP developer, SAP highly recommend to use the OPEN SQL. It is a subset of the standard SQL statements which allows you to access tables regardless of manufacturers. So in the long run, no worries on your ABAP programs.

Example of and OPEN SQL statement:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 SELECT * INTO CORRESPONDING FIELDS OF TABLE gi_sflight FROM
  ( sflight LEFT JOIN scarr
  ON sflight~carrid = scarr~carrid )
  UP TO p_maxrow ROWS
  WHERE sflight~carrid IN carrid
  AND   sflight~connid IN connid
  AND   sflight~fldate IN fldate.
  IF sy-subrc = 0.
    "do nothing
  ENDIF.



Display using FM ALV_GRID_DISPLAY
This is the last step before you can display your ALV Grid. But beforehand, you need to do the following steps first. 

a. Build the Fieldcatalog - In ABAP lingo, FieldCatalog are simply the columns. If you compare it to an Excel sheet, these are the fields with letters, "A", "B", "C", etc. In building your fieldcatalog, you will be declaring all the fields to be visible in your ALV grid which depends on the design of the functional consultant. Also, you can control which columns comes next to each other. Likewise, you can also add colors to columns, set columns to fix and many more functionality. 

b. Create the Layout - in this procedure, you are just declaring how your ALV grid looks like. In this step, you will be declaring if rows appears like zebra. Also, you can optimize also the columns width.

c. Create the PF_STATUS - in ABAP language, "PF_Status" simply means collections of events to perform  an action. So all buttons you see in your SAP screen like the Save button, Exit button, and other buttons you can click with your mouse has a corresponding PF_Status code.

d. Call FM ALV_GRID_DISPLAY - the last step before you can display your ALV is calling this function module, ALV_GRID_DISPLAY. Actually, there are two ways to display a grid type of ALV. Besides using this function module, you can also use the OOP way. But in this tutorial, I will not tackle the OOP method but I will discuss it in a separate tutorial

OOP method to display ALV Grid
1
 CALL METHOD go_grid->set_table_for_first_display



So I hope, I have given an insights all ABAP freshers an introductory on how to create their first report using an FM ALV_GRID_DISPLAY. Thank you once again, and you may connect me in my page @ABAPTHINKERS. Enjoy everyone.

Complete Sample Code:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
************************************************************************
* Copyright 2013, ABAP Thinkers
* All Rights Reserved
************************************************************************
* Program ID     : ZLEVI
* Program Name   : ALV Report using ALV_GRID_DISPLAY
* Create Date    : 11.14.2013
* Created by     : ABAP Thinkers
* Request No.    : local
* Description    : This program creates an ALV report using the FM
*                  ALV_GRID_DISPLAY
************************************************************************

REPORT  zlevi.
************************************************************************
********************START OF DATA DECLARATIONS**************************
************************************************************************
*----------------------------------------------------------------------*
*TYPE-POOLS                                                            *
*----------------------------------------------------------------------*
TYPE-POOLS: abap,slis.

*----------------------------------------------------------------------*
* TABLES                                                               *
*----------------------------------------------------------------------*
TABLES:sflight.

*----------------------------------------------------------------------*
* TYPES                                                                *
*----------------------------------------------------------------------*
TYPES: BEGIN OF ty_sflight.
        INCLUDE STRUCTURE sflight.
TYPES:    add1,
          carrname LIKE scarr-carrname,
          box,
          lights.
TYPES: END OF ty_sflight.

*----------------------------------------------------------------------*
*INTERNAL TABLES                                                       *
*----------------------------------------------------------------------*
DATA:
   gi_sflight               TYPE STANDARD TABLE OF ty_sflight,
   gi_fieldcatalog          TYPE slis_t_fieldcat_alv."

*----------------------------------------------------------------------*
*WORK AREAS                                                            *
*----------------------------------------------------------------------*
DATA:
   gwa_sflight              TYPE ty_sflight,
   gwa_layout               TYPE slis_layout_alv,
   gwa_fieldcatalog         TYPE slis_fieldcat_alv.

************************************************************************
**********************END OF DATA  DECLARATIONS*************************
************************************************************************

**----------------------------------------------------------------------*
**INITIALIZATION                                                        *
**----------------------------------------------------------------------*
INITIALIZATION.
  PERFORM f_initialize.

*----------------------------------------------------------------------*
*SELECTION SCREENS                                                     *
*----------------------------------------------------------------------*
  SELECTION-SCREEN BEGIN OF BLOCK b01 WITH FRAME TITLE text-001.
  SELECT-OPTIONS carrid FOR sflight-carrid.
  SELECT-OPTIONS connid FOR sflight-connid.
  SELECT-OPTIONS fldate FOR sflight-fldate.
  PARAMETERS: p_maxrow TYPE i DEFAULT 30.
  SELECTION-SCREEN END OF BLOCK b01.

*----------------------------------------------------------------------*
*START OF SELECTION                                                    *
*----------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM f_selection.
  PERFORM f_display_alv_report.

*&---------------------------------------------------------------------*
*&      Form  f_selection
*&---------------------------------------------------------------------*
*       Select Statement
*----------------------------------------------------------------------*
FORM f_selection.

  SELECT * INTO CORRESPONDING FIELDS OF TABLE gi_sflight FROM
  ( sflight LEFT JOIN scarr
  ON sflight~carrid = scarr~carrid )
  UP TO p_maxrow ROWS
  WHERE sflight~carrid IN carrid
  AND   sflight~connid IN connid
  AND   sflight~fldate IN fldate.
  IF sy-subrc = 0.
    "do nothing
  ENDIF.

ENDFORM.                    "SELECTION

*&--------------------------------------------------------------*
*&      Form  f_create_layout
*&--------------------------------------------------------------*
*       Create Layout of the ALV Grid
*---------------------------------------------------------------*
FORM f_create_layout USING p_gd_layout TYPE slis_layout_alv.

  p_gd_layout-zebra             = abap_true.
  p_gd_layout-colwidth_optimize = abap_true.

ENDFORM. " f_create_layout

*&--------------------------------------------------------------*
*&      Form  pf_status
*&--------------------------------------------------------------*
*       Set the PF Status of the ALV Grid
*---------------------------------------------------------------*
FORM pf_status USING rt_extab TYPE slis_t_extab.."    "#EC CALLED
  SET PF-STATUS  'STANDARD_FULLSCREEN'  OF PROGRAM 'SAPLKKBL'.
ENDFORM.                    "pfstatus

*&--------------------------------------------------------------*
*&      Form  f_display_alv_report
*&--------------------------------------------------------------*
*       Display ALV Report
*---------------------------------------------------------------*
FORM f_display_alv_report.

  PERFORM f_create_layout USING gwa_layout.
  PERFORM f_build_fieldcatalog.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'PF_STATUS'
      is_layout                = gwa_layout
      it_fieldcat              = gi_fieldcatalog[]
    TABLES
      t_outtab                 = gi_sflight[]
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
  IF sy-subrc = 0.                                          "#EC NEEDED
    "do nothing
  ENDIF.

ENDFORM.                    " F_DISPLAY_ALV_REPORT

*&--------------------------------------------------------------*
*&      Form  f_build_fieldcatalog
*&--------------------------------------------------------------*
*       Generate the Field Catalogs of the ALV Grid
*----------------------------------------------------------------*
FORM f_build_fieldcatalog.

  REFRESH: gi_fieldcatalog.
  CLEAR: gwa_fieldcatalog.

  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      i_program_name         = sy-cprog
      i_structure_name       = 'SFLIGHT'
    CHANGING
      ct_fieldcat            = gi_fieldcatalog
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.
  IF sy-subrc = 0.
    "do nothing
  ENDIF.

ENDFORM.                    "f_build_fieldcatalog

*&---------------------------------------------------------------------*
*&      Form  F_INITIALIZE
*&---------------------------------------------------------------------*
*       Initialize variables
*----------------------------------------------------------------------*
FORM f_initialize .

  REFRESH:
     gi_sflight,
     gi_fieldcatalog.

  CLEAR:
     gwa_sflight,
     gwa_layout,
     gwa_fieldcatalog.
ENDFORM.                    " F_INITIALIZE

How to implement a Lock object in ABAP?

Its about a week passed since super typhoon Yolanda hit the country. As seen from the news around the world, the City of Tacloban and nearby towns were badly damaged due to this natural disaster. The Philippine government with the aid of the international community join forces together as fast as possible to rescue the victims, to give reliefs to the survivors and most importantly to lift their spirits and hopes.

Filipinos are known of our resiliency. No matter how many trials come along the way, we stand firm that we will overcome with God's help. We are fortunate that we were spared with that super typhoon. If we were in that place, can we survive the wrath of mother nature? If we survived then, what is our life right now? So we must be thankful we didn't experience the same as our fellow Filipinos are experiencing today. I appeal to the whole world please join me in donating anything you can. Please contact your national Red Cross to get more details. I am now extending my full gratitude to everyone.

So today, I will discuss with you about Lock Objects and how to create and implement it into your ABAP programs. 


source: http://www.freeimages.co.uk

What is a Lock Object Concept?
Lock objects is implemented in SAP to avoid the inconsistency during update into the database. It is needed to maintain data integrity. This simply means that no user can edit the same record at the same time. For example, user A is editing Delivery No. 80001055. When user B access the same Delivery No in VL02N, the system will says, "it is being locked by user A". 


What are the Types of Lock Objects?
SAP provides three(3) types of Lock objects:

a. Read Lock - protects read access to an object. The read lock allows other transactions read access but not write access to the locked area of the table

b. Write Lock - protects write access to an object. The write lock allows other transactions neither read nor write access to  the locked area of the table.

c. Enhanced write lock - works like a write lock except that the enhanced write lock also protects from further accesses from the same transaction.


How to create a Lock Object?
1. Go to tcode SE11. Then select the option Lock Object. Remember always that the start of the name of the lock object is "E"
and followed by ‘Z’ or ‘Y’.  For instance, "EZLIKP". Then click the Create button.



2. In this screen below, select the Tables tab. Give a meaningful short description of your lock object. Then on the primary table, enter the name of the table which will be locked. In my sample, I used table LIKP. Please note that it can be a standard table or a customized table. Then on the Lock Mode selection, select from the types of lock as discussed above. I choose "Write Lock" so that no one will be able to edit a particular Delivery No simultaneously. Save and activate.



As can be seen from the Lock Parameter tab, the primary keys (MANDT & VBELN) of the LIKP table are the one listed as the lock parameters.

So in this two steps, you were able to create a lock object. Remember always, the name of the lock object must begin with "E".


How to implement a Lock Object in ABAP?
 Once a lock object is created, two function modules were automatically created by the system. These are:

a. ENQUEUE_[Lockobject name] - To lock the object. ex: ENQUEUE_EZLIKP.
b. DEQUEUE_[Lockobject name] - To unlock the object. ex: DEQUEUE_EZLIKP.

Consider this case. I created a dialog program which change the picking date of a Delivery No. So in my first screen, I asked the user to input the Delivery No. just like this one below.

Once the user click the execute button, you will need to lock now that delivery no. so that no one would be able to edit it while you are doing the updates. Regardless you update it or not, you need to lock the object to maintain data consistency. Since I implemented this program using a dialog screen, I will call the FM ENQUEUE_EZLIKP into the PAI. But if you are using a SELECTION-SCREEN, call this FM at the event, START-OF-SELECTION. Pass the delivery no. as the exporting parameter of the Function Module.  Take this sample code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
CALL FUNCTION 'ENQUEUE_EZLIKP'
    EXPORTING
      mode_likp      = 'E'
      mandt          = sy-mandt
      vbeln          = p_vbeln "Delivery No
    EXCEPTIONS
      foreign_lock   = 1
      system_failure = 2
      OTHERS         = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE c_mode_s NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 DISPLAY LIKE c_mode_e.
  ELSE.
    "if locking is successful, it will call screen 100 
    CALL SCREEN 100.
  ENDIF.


So if your ABAP program passes into this code above, it will create a lock entry in SM12 as seen below. Anything listed here must be unlock first before anyone can use it again. The report below shows the lock mode and the lock argument of the lock object.

So if someone of your company try to access the same object which is currently locked, it will prompt to his screen this message below indicating it is being processed by a specific user . Hence, data integrity prevails.

As I said above, lock objects remains in the lock entry list until you logoff your SAP system or call  the FM DEQUEUE. As a best practice, take the second method always, call the FM DEQUEUE in order to unlock the object and other users may view and update it too. To call the FM, consider this snippet:

1
2
3
4
5
CALL FUNCTION 'DEQUEUE_EZLIKP'
    EXPORTING
      mode_likp = 'E'
      mandt     = sy-mandt
      vbeln     = p_vbeln.



SUMMARY and POINTERS:
1. Lock Object starts with "E" and followed by "Z" or "Y";
2. There are 3 types of lock: Read, Write and Enhanced Write;
3. There are 2 FM automatically created and used in implementing locks: ENQUEUE_<name_of_lock_object> and DEQUEUE_<name_of_lock_object>.
4. Always call the FM DEQUEUE once done with the processing on your ABAP code;


Thank you once again to everyone for reading this simple blog. Please join me in my page at Facebook, ABAPTHINKERS to get more updates time to time.

How to download the data structure of tables in SE11?

Good morning people around the world. Its me again doing some blogs. Sharing to you some tips and tricks in the SAP world. Well, before I start my topic today, just want you to know that our country was hit by one of the strongest typhoon on earth this year named "Yolanda". Thanks God and it did not hit directly Manila, the capital of the country where millions of people are living.  Just recently, Bohol in the Visayan island was hit another 7.2 magnitude earthquake which causes more than 200 people died. Anyway, lets pray together to all our brethren who were in the midst of this natural calamity. 

So to begin with my discussion today, I will share to you a great tutorial which I know it will be beneficial to all. Many SAP systems have customized developments. In relation to it, there are custom tables also that were created by the ABAP developers. Now, the question of many developers is, how can they back up their tables into their laptops or computers? Just like in my previous blog which I tackled on how to download your ABAP code into PDF file, now what we want to download into our own computer is the structure of our custom table that we created?

Why do we need to download the structure of our custom tables? Basically, we need to have a copy of it as for our future reference. Actually, in other databases like MySQL, there is that functionality of IMPORT/EXPORT which you can download the whole database into your computer. But in SAP, this feature is not supported. What SAP offers is the transport system from one system to another but not into your own computer.

So in this tutorial, I will discuss to you a simple way to back-up your custom tables into your local drive. Just follow the steps below:

Steps:

1. First open your custom table using SE11.  What I used as an example here is EKKO which is a standard table.



2. In the file menu, click Utilities->Database Object->Display.



3. A new screen like this one below appears showing the structure of the table. Please take note that the data elements were replaced with elementary data types. 



4. Normally, when we download, we go to System ->List->Save. But as you can see, all options are disabled.



5. In order to download it, put your mouse cursor anywhere in the table then right click it. From the context menu, select View Source.



5. A notepad then appears displaying the source code of the table. This is an html format. 



6. Click the Save in the file menu. This dialog appears. Then save your file anywhere now into your local drive. Remember, since this is an html file, save this way, your filename must end with .html, so example, table_ekko.html. And last reminder is, change the "Save as type" into "All files" (Please refer to screenshot below).



7. Congratulations. you have now a copy into your local drive the custom table you created.  Go to the directory where you save your file. Open it and it will look the same as the one in SAP.

Sample Result: