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

Wednesday, December 8, 2010

OOPS ALV

The ALV Grid Control (ALV = SAP List Viewer) is a flexible tool for displaying lists. The tool provides common list operations as generic functions and can be enhanced by self-defined options. This allows you to use the ALV Grid Control in a large range of application programs.

Those Who want the basic knowledge can access the SAP Help file here.

First Steps 
This section describes the easiest way to display a list with selected data in the ALV Grid Control. To do this, you must:

  1. Create an instance of the ALV Grid Control and integrate it into a screen.
  2. Select the data to be displayed and pass it together with a description of the fields to the instance.


Note
See also sample report BCALV_GRID_DEMO in development class SLIS .
Creating an ALV Grid Control You instantiate an ALV Grid Control in the same way as other controls:

  1. Declare reference variables for the ALV Grid Control and the container. In addition, declare an internal table that you fill with selected data later on: DATA: grid TYPE REF TO cl_gui_alv_grid,
          g_custom_container TYPE REF TO cl_gui_custom_container
          gt_sflight TYPE TABLE OF sflight.
    Note
    In order to integrate a control into a screen, you can use four different Structure link container controls (in this example, we use the custom container control).
  2. Create a standard screen and mark an area for the custom container control in the graphical Screen Painter (icon identified by letter 'C'). Assign name CCCONTAINER to this area. Note
    Structure link Exercise 1: Reserving an Area for a Control of the Controls Tutorial explains how to mark an area in the alphanumerical Screen Painter version.
  3. In the PBO module of the screen, you must now instantiate the container control and the ALV Grid Control. By doing this, you create a link between the container control and the screen, using the container created in the Screen Painter. Using parameter parent , you define the container control as the parent of the ALV Grid Control:
IF g_custom_container IS INITIAL.
    CREATE OBJECT g_custom_container
       EXPORTING
          CONTAINER_NAME = 'CCCONTAINER'.
    CREATE OBJECT GRID1
       EXPORTING
         I_PARENT = g_custom_container.
ENDIF.



Note
The IF query of reference variable g_custom_container ensures that the instances are only generated when the PBO is processed for the first time.
Caution
Normally, you must use method cl_gui_cfw=>flush to pass the methods called to the frontend. However, since the Control Framework performs an automatic flush at the end of the PBO module, this step is not required here.
When you start the program, although the two instances (the container control and the ALV Grid Control) are generated, they are not visible. Displaying a List in the ALV Grid Control Once you have created the ALV Grid Control and integrated it into a screen using a container control, you must pass the data and its structure to the ALV Grid Control:

  1. Fill the internal table with data: SELECT * FROM sflight INTO TABLE gt_sflight.
  2. Pass the output table and the structure data to the ALV Grid Control. Again, ensure to call this method only once after the ALV Grid Control is created:
CALL METHOD grid->set_table_for_first_display
             EXPORTING I_STRUCTURE_NAME = 'SFLIGHT'
             CHANGING IT_OUTTAB = gt_sflight.



Note
In this case, the structure data is provided through the Data Dictionary. The ALV Grid Control gets the field information from table SFLIGHT and displays all fields of the table.


Title for ALV Using Commentary Writer
Click here to view the Code extract which will help in creating the title.


Add Button to ALV Toolbar with REUSE_ALV_LIST_DISPLAY
How to add button to ALV toolbar using REUSE_ALV_LIST_DISPLAY?
In the program which calls ALV using REUSE_ALV_LIST_DISPLAY,
There are some demo program like BCALV_GRID_08, which is written using ABAP-Controls.
In that example, the button is added using TOOLBAR event of cl_gui_alv_grid.
Copy a standard GUI Status from Standard
got to SE80 --> Function group --> SALV
or 
SE90 -->Programming SubObjects--> Gui Status.
Copy STANDARD GUI Status
you should copy the 'STANDARD' GUI status from program SALV or SAPLKKBL .
Enter your Z program name and the name you what for this status - you can keep it as 'STANDARD' to be simple.
Then you can edit the new status to add or delete buttons. This will also bring in the standard SAP ALV functionality such as sorting/subtotaling etc...
When you call 'REUSE_ALV_GRID_DISPLAY' make sure you pass it the new status name.
an example of one of mine:
*&---------------------------------------------------------------------*

*&      Form  DISP_ALV
*&---------------------------------------------------------------------*
FORM disp_alv .

 CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
   EXPORTING
     i_callback_program       = sy-repid
     i_callback_pf_status_set = 'SET_PF_STATUS'
     i_callback_user_command  = 'USER_COMMAND'
     is_layout                = gfl_layout
     it_fieldcat              = git_fieldcat
*      i_save                   = 'A'
   TABLES
     t_outtab                 = git_final                         
   EXCEPTIONS
     program_error            = 1
     OTHERS                   = 2.

 IF sy-subrc NE 0.
*    MESSAGE e398(00) WITH 'Can not generate ALV list'.
 ENDIF.

ENDFORM.                    " DISP_ALV
Create a form with tes same name you specified in the FM
In this case :
i_callback_pf_status_set = 'SET_PF_STATUS'
*------------------------------------------------------------------*
*       FORM SET_PF_STATUS                                         *
*------------------------------------------------------------------*
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
 "Copy of 'STANDARD' pf_status from fgroup SALV
 SET PF-STATUS 'ZSTANDARD'.
ENDFORM.                    "set_pf_status

Note : While editing the copied Status
Give your function code with ‘&’ sign then only it will appear in the tool bar other wise it will be available when you right click the application tool bar
for example
&GENB       Generate Excise Invoice
USER COMMAND - to capture activities happening in ALV
*&---------------------------------------------------------------------*
*&      Form  user_command
*&---------------------------------------------------------------------*
FORM user_command USING ucomm TYPE sy-ucomm
                                                         field TYPE slis_selfield.
 DATA ref1 TYPE REF TO cl_gui_alv_grid.
 CASE ucomm.
   WHEN '&GENB'.
******************Getting the changes from ALV. like the checked line items ***************
     CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
       IMPORTING
         e_grid = ref1.
     CALL METHOD ref1->check_changed_data.
  
  ENDCASE.
ENDFORM.                    "USER_COMM
To refresh...
rs_selfield-refresh = 'X'. "structure slis_selfield
How to display ALV in Custom Container.

Code Extract
data : alv_container  type ref to cl_gui_docking_container.
data : alv_grid    type ref to cl_gui_alv_grid.

data : layout type lvc_s_layo.

data : variant type  disvariant.

check alv_container is initial.

 CREATE OBJECT alv_container
EXPORTING
* parent                   =
  repid                    = sy-repid
  dynnr                    = sy-dynnr
  side                     = alv_container->dock_at_left
*   extension                = 1550
* style                    =
* lifetime                 = lifetime_default
* caption                  =
* metric                   = 0
* ratio                    = '95'
* no_autodef_progid_dynnr =
* name                     =
EXCEPTIONS
  cntl_error               = 1
  cntl_system_error        = 2
  create_error             = 3
  lifetime_error           = 4
  lifetime_dynpro_dynpro_link = 5
  OTHERS                   = 6
  .
 IF sy-subrc  0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
           WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
 ENDIF.

*- Make the docking container as parent to the grid

 CREATE OBJECT alv_grid
EXPORTING
  i_parent       = alv_container
EXCEPTIONS
  error_cntl_create = 1
  error_cntl_init   = 2
  error_cntl_link   = 3
  error_dp_create   = 4
  OTHERS         = 5.
 IF sy-subrc  0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
           WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
 ENDIF.

*  layout-zebra = 'X'.
 PERFORM build_fieldcat.
 variant-report = sy-repid.

*- Call grid for display

 CALL METHOD alv_grid->set_table_for_first_display
EXPORTING
  i_structure_name            = 'WT_TEST'
  is_variant                 = variant
  i_save                      = 'A'
*   is_layout                   = layout
CHANGING
  it_outtab                   = WT_TEST
  it_fieldcatalog             = wt_fcat_log
EXCEPTIONS
  invalid_parameter_combination = 1
  program_error              = 2
  too_many_lines             = 3
  OTHERS                     = 4
      .
 IF sy-subrc  0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
           WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
 ENDIF.