Monday, September 7, 2015

Database Table Update Delete


Module pool program is an ABAP/4 program which consists a set of modules with different screens. In each and every screen a user can give input by entering data or clicking any button or check boxes. Several screens of module pool program must have a screen flow logic by which a user can interact with the program. It can be called screen programming or dialog programming also. Module pool program cannot be executed directly by pressing F8. We need to create Transaction code for a module pool program. Now we can execute the program by entering that t-code similar like SE38, SE11 etc.

A module pool program can have any number of screens. All those screens must have separate attribute, screen elements and flow logic. Since ABAP is event driven language, screens must have the events also. There are four types of events in module pool programming.
1.       PBO (Process Before Output) – When it triggers then it initializes the screen attribute, screen elements, PF status with all buttons of the screen and initial value of any fields if exists.
2.       PAI (Process After Input) – After displaying the screen when user enters any value or clicks any button on that screen then this event is triggered. After triggering this event it moves to the flow logic of the next screen. Hence preparing the next screen output. After doing this the system again triggers the PBO for next screen and then the next screen appears.
3.       POV (Process on Value request) – When user press the F4 button then it triggers this event and it displays the possible set of data records which need to be entered into that field.
4.       POH (Process on Help request) – When user press the F1 button then it triggers this event and it displays the help documentation (if exists) with a pop up screen.

We design the screen by using Screen Painter (SE51) and PF status or menu bar by using Menu Painter (SE41). In module pool program we can directly go to screen painter inside the screen by clicking the LAYOUT button.

We can display data records from database table into the screen by using module pool program. We also can modify data records by module pool program also. Here we have created a requirement where we have insert new data, update existing data and delete data by using module pool screen. We have a custom database table ‘zcustomers’. Now it contains fields of customer id, name and address.




Now we already have some records in this table as follows.



Now we have created this module pool program which has the first screen like this.



Here we already have entered the customer number ‘2’ and click the display button.



On the second screen we can see the customer details. The address is not updated there. We can check it from the database table also and we can confirm that the address has not been updated there. Now we shall enter an address on the address field and click on update button.



After updating the address of the customer we are generating a success message at message bar.



Now we can check the database again if the address has been updated or not.



We can also enter a new customer by using this program. We are going to the first screen and entering a new customer ID like ‘6’.  There is a validation whether the ID is already existing or not. To do this we have to click the display button after entering the customer number. If it is not in database then the program will prompt an Information message like this.



Now we click on the create button and the program will move to the next screen.



The customer number which we have entered into the first screen, is carry forwarded to the second screen. And now we need to fill up the details of this customer.



After filling up the data we click on update button to update this new customer into the database. We shall again get a success message for that. Now we shall see the database table and it has been newly updated as well.



In this way if we delete any customer at the first screen level then it will be removed as well.

Step – 1:
Now we are coming to the coding part of this program. At the time of creation of this program we have four include programs as follows. We just need to create all of them and save those.

INCLUDE zsr_top                                 .  " global Data
INCLUDE zsr_o01                                 .  " PBO-Modules
INCLUDE zsr_i01                                 .  " PAI-Modules
INCLUDE zsr_f01                                 .  " FORM-Routines

Firstly we have to declare the data types, tables, variables etc in the global data declaration.

PROGRAM  zsr_mod.

TABLES: zcustomers.

TYPES: BEGIN OF ty_cust,
        id      TYPE zcustomers-id,
        name    TYPE zcustomers-name,
        address TYPE zcustomers-address,
       END OF ty_cust.

DATA: wa_cust TYPE zcustomers,

      ok_code1 TYPE sy-ucomm,
      ok_code2 TYPE sy-ucomm.

Step – 2:
Now we shall create the first screen 9001 and maintain the flow logic as follows.

PROCESS BEFORE OUTPUT.
  MODULE status_9001.

PROCESS AFTER INPUT.
  MODULE user_command_9001.

Step – 3:
Now we shall create the PBO – Module status_9001 as follows.

MODULE status_9001 OUTPUT.
  SET PF-STATUS 'PF_CUST_INPUT'.
  SET TITLEBAR 'CUST_INPUT'.

ENDMODULE.                 " STATUS_9001  OUTPUT

Step – 4:
After that we have to create the pf status and title bar by double clicking on there.




Step – 5:
After that we shall create the PAI module user_command_9001 as follows:

MODULE user_command_9001 INPUT.

  CASE ok_code1.
    WHEN 'DISP'.
      PERFORM get_customer.
    WHEN 'CRT'.
      PERFORM create_customer.
    WHEN 'CANC'.
      CLEAR: zcustomers-id.
      LEAVE LIST-PROCESSING.

    WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
      LEAVE PROGRAM.
  ENDCASE.

ENDMODULE.                 " USER_COMMAND_9001  INPUT

Step – 6:
Next we shall create all those subroutines as follows:

FORM get_customer .

  IF zcustomers-id IS NOT INITIAL.
    SELECT SINGLE *
      FROM zcustomers INTO wa_cust
      WHERE id = zcustomers-id.

    IF sy-subrc = 0.
      CALL SCREEN 9002.
    ELSE.
      MESSAGE 'Customer doesn''t exist' TYPE 'I'.
    ENDIF.
  ELSE.
    MESSAGE 'Enter a Customer Number' TYPE 'I'.
  ENDIF.

ENDFORM.                    " get_customer

FORM create_customer .

  IF zcustomers-id IS NOT INITIAL.

    "Validation is needed if customer is already present
    SELECT SINGLE *
      FROM zcustomers INTO wa_cust
      WHERE id = zcustomers-id.

    IF sy-subrc = 0. "It means customer is already in database
      MESSAGE 'Customer already exist' TYPE 'I'.

    ELSE. "It means customer is not present in the database
      "Hence it needs to be created

      CLEAR: wa_cust, zcustomers-name, zcustomers-address.
      "Only zcustomers-id needs to be passed to screen 9002

      CALL SCREEN 9002.
    ENDIF.
  ELSE.
    MESSAGE 'Enter a Customer Number' TYPE 'S'.
  ENDIF.

ENDFORM.                    " create_customer

Step – 7:
Here we can see that the program is calling screen 9002 so we have to configure that now. On screen 9002 we have the following.

PROCESS BEFORE OUTPUT.
  MODULE status_9002.
*
PROCESS AFTER INPUT.
  MODULE user_command_9002.

Step – 8:
Similarly we have to create the PBO for 9002 as follows.

MODULE status_9002 OUTPUT.
  SET PF-STATUS 'PF_CUST_OUTPUT'.
  SET TITLEBAR 'CUST_OUTPUT'.

  IF ok_code1 = 'DISP'.
    PERFORM customer_output.
  ENDIF.

ENDMODULE.                 " status_9002  OUTPUT

Step – 9:
Now after creating the pf status and title bar for screen 9002 we need to create the subroutine of customer_output.

FORM customer_output .

  IF wa_cust IS NOT INITIAL.

    "Screen fields       Work area fields
    zcustomers-id      = wa_cust-id.
    zcustomers-name    = wa_cust-name.
    zcustomers-address = wa_cust-address.
  ENDIF.

ENDFORM.                    " customer_output

Step – 10:
Now we need to create the PAI for screen 9002 as follows.

MODULE user_command_9002 INPUT.

  CASE ok_code2.
    WHEN 'UPDT'.
      PERFORM update_customer.
    WHEN 'DEL'.
      PERFORM delete_customer.
    WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
      LEAVE TO SCREEN 9001.
      LEAVE LIST-PROCESSING.
  ENDCASE.

ENDMODULE.                 " user_command_9002  INPUT

Step – 11:
Next we need to create the subroutines to update and delete customer.

FORM update_customer .

  IF zcustomers-id IS NOT INITIAL.

    "Passing the values to the internal work area
    wa_cust-id      = zcustomers-id.
    wa_cust-name    = zcustomers-name.
    wa_cust-address = zcustomers-address.

    IF ok_code1 = 'CRT'.

      "Inserting new Customer into database while creating
      INSERT into zcustomers values wa_cust.

    ELSE.

      "Updating the database by using the work area
      UPDATE zcustomers FROM wa_cust.
    ENDIF.

    IF sy-subrc = 0. "If the update is successful
      MESSAGE 'Customer details updated successfully' TYPE 'S'.

    ELSE. "If the update is not successful
      MESSAGE 'Customer is not updated' TYPE 'E'.
    ENDIF.
  ENDIF.

ENDFORM.                    " update_customer

FORM delete_customer .

  IF zcustomers IS NOT INITIAL.

    "Work area fields     Screen fields
    wa_cust-id          = zcustomers-id.
    wa_cust-name        = zcustomers-name.
    wa_cust-address     = zcustomers-address.

    "Delete data records from database table
    DELETE zcustomers FROM wa_cust.

    IF sy-subrc = 0. "If deletion is successful
      CLEAR: wa_cust, zcustomers.
      MESSAGE 'Data has been removed successfully' TYPE 'S'.
      LEAVE TO SCREEN 9001.

    ELSE. "If deletion is not successful
      MESSAGE 'Data has not been removed' TYPE 'E'.
    ENDIF.
  ENDIF.

ENDFORM.                    " delete_customer

Step – 12:
Since module pool program cannot be executed directly we need to create a transaction code.



Now after doing this we have to activate the whole program and then check the t-code from the system.

13 comments:

Raju Shrestha said...

Very detailed..

Will help beginners!!

Regards,
Raju

mayukh saha said...

really very very helpful...thnk u..:)

Rahul said...

Thanks for sharing this Informative content. Well explained. Got to learn new things from your Blog on SAP SD.
SAP SD

Vivek Patel said...

really helpful this code .. for my understanding
thanking you.

Rahul said...

It was very nice blog to learn about SAP BASIS. Thanks for sharing.SAP basis

Bhavya Kumar said...

Best blog.Got to learn new things.Thanks for this Blog SAP Success Factors Training in Chennai

Shalini said...


Hi, This is shalini from Chennai learned SAP Training in Chennai from mr.karthick. The training really was good and i got selected in leading mnc company as SAP Consultant.

You can contact 8122241286 for Best SAP Training in Chennai or reach www.thecreatingexperts.com

sanayamohit said...

SAP Success Factors Real Time Hands on Training in Chennai...

Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286

Both Classroom/Online Training is Available!!!!!!

Shruti Ramalingam said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

http://thecreatingexperts.com/sap-mm-training-in-chennai/

sushma said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

simashree said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

ragu varan said...

Best SAP HANA Training in Chennai by leading HANA Consultant.
Reach at 9003085882
http://thecreatingexperts.com/category/sap-hana-training-in-chennai/

gilledashivani said...

Great information. the information provided is of great use as i got to learn new things. If you are looking for real time training in SAP ABAP, kindly contact sieve software
Sieve software offers real time training by industry experts with live projects and placement support.
SAP ABAP TRAINING IN HYDERABAD