Tuesday, April 1, 2014

Create transaction for SAP-Query

Create transaction for SAP-Query


1)     Open transaction SE93
 
2)     Put object name  ,sort text for your transaction
3)     Choose radio button 5. Transaction with parameter.  
4)     Default value for transaction ‘START_REPORT’
5)     In bellow default value section insert default value
          SREPOVARI-REPORTTYPE      = AQ      "Parameter indicating Abap Query
          D_SREPOVARI-REPORT           = ZY_CS_ALLSLS   "Query User group
          D_SREPOVARI-EXTDREPORT= ZY_SLS_MST_001   "Query name  

   

6)     Save the transaction.  
8) Execute the transaction. 
 

Create Selection Screen for SAP Table Maintenance View (SM30)

Usually a SAP Table Maintenance View (SM30) with single screen displays all the records in the table as shown below.
What if we want to display only some records in SAP Table Maintenance View (SM30) based on the user selection? Just follow the below steps.
  • Create a report program and design the selection screen for the table/view as per your requirement.
  • Use the SAP function module VIEW_RANGETAB_TO_SELLIST to build the selection criteria for Table Maintenance View
  • Use the SAP function module VIEW_MAINTENANCE_CALL to call the Table Maintenance View (SM30)
TABLES: zemployee.
CONSTANTS: c_view  TYPE   char30  VALUE 'ZEMPLOYEE',
          c_u     TYPE   char1   VALUE 'U',
          c_and   TYPE   char3   VALUE 'AND'.
DATA: gt_seltab    TYPE STANDARD TABLE OF vimsellist.
DATA: g_fieldname  TYPE vimsellist-viewfield.
DATA: gt_exclude   TYPE TABLE OF vimexclfun,
     gwa_exclude  TYPE vimexclfun.

SELECT-OPTIONS: s_id    FOR zemployee-id,
               s_name  FOR zemployee-name,
               s_place FOR zemployee-place.

*Add ID column to selection criteria of Table maintenanace view
g_fieldname = 'ID'.

CALL FUNCTION 'VIEW_RANGETAB_TO_SELLIST'
 EXPORTING
   fieldname          = g_fieldname
   append_conjunction = c_and
 TABLES
   sellist            = gt_seltab
   rangetab           = s_id.

*Add Name column to selection criteria of Table maintenanace view
g_fieldname = 'NAME'.

CALL FUNCTION 'VIEW_RANGETAB_TO_SELLIST'
 EXPORTING
   fieldname          = g_fieldname
   append_conjunction = c_and
 TABLES
   sellist            = gt_seltab
   rangetab           = s_name.

*Add Place column to selection criteria of Table maintenanace view
g_fieldname = 'PLACE'.

CALL FUNCTION 'VIEW_RANGETAB_TO_SELLIST'
 EXPORTING
   fieldname          = g_fieldname
   append_conjunction = c_and
 TABLES
   sellist            = gt_seltab
   rangetab           = s_place.

* Call to the 'VIEW_MAINTENANCE_CALL' function module
CALL FUNCTION 'VIEW_MAINTENANCE_CALL'
 EXPORTING
   action         = c_u
   view_name      = c_view
 TABLES
   dba_sellist    = gt_seltab.
Selection Screen : Display all the entries in SM30 where place is LONDON
Table Maintenance View (SM30)
Use the ACTION parameter in FM VIEW_MAINTENANCE_CALL to call the Table Maintenance in Change or Display mode.
GUI function codes (Buttons) in the Table Maintenance can be deactivated using the Table parameter  EXCL_CUA_FUNCT of FM VIEW_MAINTENANCE_CALL.
Below program deactivate New Entries, Copy and Delete buttons in SM30.
TABLES: zemployee.
CONSTANTS: c_view  TYPE   char30  VALUE 'ZEMPLOYEE',
          c_u     TYPE   char1   VALUE 'U',
          c_and   TYPE   char3   VALUE 'AND'.
DATA: gt_seltab    TYPE STANDARD TABLE OF vimsellist.
DATA: g_fieldname  TYPE vimsellist-viewfield.
DATA: gt_exclude   TYPE TABLE OF vimexclfun,
     gwa_exclude  TYPE vimexclfun.

SELECT-OPTIONS: s_id    FOR zemployee-id,
               s_name  FOR zemployee-name,
               s_place FOR zemployee-place.

*Add ID column to selection criteria of Table maintenanace view
g_fieldname = 'ID'.

CALL FUNCTION 'VIEW_RANGETAB_TO_SELLIST'
 EXPORTING
   fieldname          = g_fieldname
   append_conjunction = c_and
 TABLES
   sellist            = gt_seltab
   rangetab           = s_id.

*Add Name column to selection criteria of Table maintenanace view
g_fieldname = 'NAME'.

CALL FUNCTION 'VIEW_RANGETAB_TO_SELLIST'
 EXPORTING
   fieldname          = g_fieldname
   append_conjunction = c_and
 TABLES
   sellist            = gt_seltab
   rangetab           = s_name.

*Add Place column to selection criteria of Table maintenanace view
g_fieldname = 'PLACE'.

CALL FUNCTION 'VIEW_RANGETAB_TO_SELLIST'
 EXPORTING
   fieldname          = g_fieldname
   append_conjunction = c_and
 TABLES
   sellist            = gt_seltab
   rangetab           = s_place.

*Deactivate New Entries
gwa_exclude-function = 'NEWL'.  " Function Code for New Entries
APPEND gwa_exclude TO gt_exclude.

*Deactivate Copy
gwa_exclude-function = 'KOPE'.  " Function Code for Copy
APPEND gwa_exclude TO gt_exclude.

*Deactivate Delete
gwa_exclude-function = 'DELE'.  " Function Code for Delete
APPEND gwa_exclude TO gt_exclude.

* Call to the 'VIEW_MAINTENANCE_CALL' function module
CALL FUNCTION 'VIEW_MAINTENANCE_CALL'
 EXPORTING
   action         = c_u
   view_name      = c_view
 TABLES
   dba_sellist    = gt_seltab
   excl_cua_funct = gt_exclude.
Output : Displaying all entrie
s


Events in Table Maintenance

Events in Table Maintenance


Scenario: We have a following custom table which contains the fields “Date on which record was created” and “Name of the person who created the object”. We would like to have these to be filled up with SY-DATUM and SY-UNAME respectively.   
Go to Table Maintenance Generator:
Enter the details as shown below:
Now click on Environment -> Modification -> Events
Following screen is displayed.


Click on F4. Following entries are displayed:
Here you can observe that there are different types of events available like before saving the data, after saving the data, before deleting, after deleting and others. Let us go with “Creating a new entry”.
Click on the button in the Editor column and enter the following code: (Please note that you should also code FORM and ENDFORM as well). 
 
Save and activate the table.
Testing the scenario:
Go to SM30 and try creating new entries. Do not enter the values for “Created on” and “Created by”.
Check your entries in the database table. You can observe that the date and user name are automatically filled-in. See the screenshot below:



Some more Details

Modify your SAP database table maintenance events, screen, interface and code

Once you have created your basic SAP database tableand then added a standard table maintenence to it you may find that you want to change the functionality of it slightly. I.e. you may want to display a message when the data is saved or prevent users from saving certain combinations of data.



Step 1 – Modification options
SAP provides standard functionality for this purpose and is within the table maintenance create/change screen. Just to re-cap how to get to this, execute transaction SE11, enter your table name and press the change button. Within the next SE11 screen goto the menu option 'Utilities->Table Maintenance Generator'. The modifiction options are then within the menu option 'Environment->Modification'. Here you will see that you can modify the screens, interface, events and source code. For this example I am going to show you how to modify and event, the SAVE event to be specific. So when the user presses the save button with t-code SM30 for this table it will process this additional functionality automatically.



Step 2 – Access the event modification screen
Select the menu option Environment->Modification->Events, you will see the following screen which will show the list of events that have been modified. It should be blank as this is the first time in.



Step 3 – Create event modification
Click on the New entries button and use the F4 dropdown help on the first field to select the event you want to modify. You will see that there is quite a few that you can use such as before save, after save, before delete, when creating an entry etc... Select 01 (Before saving the data in the database).


Enter 'SAVE_DATA' as the form name for your event modification and press enter. Note you can give this any name you want.


You should also notice that you get an editor button in the third column, click this. You will then be asked to choose an existing include or create a new one for your code. Just keep the default as 'New Include' and press the green tick. Also click ok on the warning screen about an include statement being inserted.



Step 4 – Add code to event modification
You will now be presented with a blank include page where you can add the code for your event.


Its now time to add your ABAP code but remember you also need to add the FORM...ENDFORM code to match the form you endered on the modification screen i.e. SAVE_DATA


In this example I am simply going to display a message with some text and the value stored in field1 using the following ABAP code. I have tried to use a message class and number that should be in your system but if you have any problems use a message class you know exists in your system.


FORM SAVE_DATA.
 message i899(H1) with 'Data has been saved' ztest_table-field1.
ENDFORM.



Step 5 – Save and Activate
Save and activate your include, then use the back button to return to the event modification screen and save that.



Step 6 – Execute

Now go to the table maintenace via SM30, change an entry in the table and press save. You should see you new message appear.

Scheduling background jobs by triggering events

Scheduling background jobs by triggering events



Step1: Create event from transaction SM62.
Give event name and description and press save button
Step2: Create a program that triggers this event by calling the FM ‘BP_EVENT_RAISE’.
*&---------------------------------------------------------------------*
*& Report  Z_TRIGGER_EVENT                                             *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*
REPORT  Z_TRIGGER_EVENT                         .
CALL FUNCTION 'BP_EVENT_RAISE'
 EXPORTING
   eventid                      = 'Z_TRIGGER_JOB'
EXCEPTIONS
  BAD_EVENTID                  = 1
  EVENTID_DOES_NOT_EXIST       = 2
  EVENTID_MISSING              = 3
  RAISE_FAILED                 = 4
  OTHERS                       = 5
         .
IF sy-subrc <> 0.
Write: 'Event failed to trigger'.
else.
Write: 'Event triggered'.
ENDIF.
Step3: Configure the background job from transaction SM36.
In the initial screen give job name and job class and press “Start condition” button.
In the popup screen press “After event” button and give the event name and then press save button.


Now go back to the initial screen and press “Step” button
Provide program and variant name and after providing all the values press save button.
In the initial screen press save button.
Step4: Now execute the program to trigger the event and as well check the background job.
Run transaction SM37
Check the status of job defined above
Now check the spool to see the generated list