Saturday 15 March 2014

SAP LUW & DB LUW

Scenario: This post describes how to use UPDATE FMs to achieve SAP LUW. This involves two data base tables. We want to achieve the below functionality. Either the two tables will be updated together or none of them.

Step1. Create a header table with below fields.



















Step2. Create a item table with below fields.


















CASE-1
Step3. Write a Report program. Fill the Header work area and item table. For the example here for a single header line we have multiple  line items. The report doesn't contain any Commit Work statement.
After the program execution and database update Commit Work happens implicitly( By Default).



























Or we can slightly change the report, so that we have COMMIT  WORK statement for every database update statements to explicitly.

_________________________________________________________________________________

Step4.
_________________________________________________________________________________
REPORT  zupdate_details.
** The requirement is the header and the item table should be updtaed **
** together. If either updated fails then all other updated should be **
** reverted back *************************************************
DATA : ls_emp_hdr TYPE zemp_header,
              lt_emp_item TYPE TABLE OF zemp_item_det,
              ls_emp_item TYPE zemp_item_det.

START-OF-SELECTION.
******fill emp header *****
  ls_emp_hdr-emp_id = '00001'.
  ls_emp_hdr-emp_name = 'Rajat Shankar'.
  ls_emp_hdr-emp_desig = 'Senior Softare Engg'.
  ls_emp_hdr-emp_mobile_no = '9090909090'.
  ls_emp_hdr-emp_email_id = 'raja@gmail.com'.
******fill emp item*****
  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = 'HSC'.
  ls_emp_item-yr_pass = '2010'.
  ls_emp_item-divison = '1'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = '12TH'.
  ls_emp_item-yr_pass = '2012'.
  ls_emp_item-divison = '2'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  INSERT INTO zemp_header VALUES ls_emp_hdr.
  INSERT  zemp_item_det FROM TABLE lt_emp_item.
  WRITE :/ 'Employee :', ls_emp_hdr-emp_id , 'details is updated'.

_________________________________________________________________________________
Step4. Explicitly Specifying COMMIT WORK for each SQL Insert Statement.
_________________________________________________________________________________
REPORT  zupdate_details.
** The requirement is the header and the item table should be updtaed **
** together. If either updated fails then all other updated should be **
** reverted back ************************************************
DATA : ls_emp_hdr TYPE zemp_header,
              lt_emp_item TYPE TABLE OF zemp_item_det,
               ls_emp_item TYPE zemp_item_det.

START-OF-SELECTION.
******fill emp header *****
  ls_emp_hdr-emp_id = '00001'.
  ls_emp_hdr-emp_name = 'Rajat Shankar'.
  ls_emp_hdr-emp_desig = 'Senior Softare Engg'.
  ls_emp_hdr-emp_mobile_no = '9090909090'.
  ls_emp_hdr-emp_email_id = 'raja@gmail.com'.
******fill emp item*****
  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = 'HSC'.
  ls_emp_item-yr_pass = '2010'.
  ls_emp_item-divison = '1'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = '12TH'.
  ls_emp_item-yr_pass = '2012'.
  ls_emp_item-divison = '2'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  INSERT INTO zemp_header VALUES ls_emp_hdr.
  IF sy-subrc = 0 .
    COMMIT WORK.
  ENDIF.
  INSERT  zemp_item_det FROM TABLE lt_emp_item.
  IF sy-subrc = 0 .
    COMMIT WORK.
  ENDIF.
  WRITE :/ 'Employee :', ls_emp_hdr-emp_id , 'details is updated'.
_________________________________________________________________________________
Step5. Execute the report and the below output appears.










Step6. The header table is updated with one record.











Step7. The item table is updated with 2 dependent records.











In Case-1 both the data base tables are updated properly. So there is no data inconsistency.
_________________________________________________________________________________


CASE-2
Before testing the CASE-2 delete all entries from the two data base tables, so that no need to change the data in the program.
Step8. Slightly change the report. Put a rollback statement for the ITEM table update. This rollback we are doing manually to test our scenario but it might happen automatically . Execute the report.


























Step9.
_________________________________________________________________________________

REPORT  zupdate_details.
** The requirement is the header and the item table should be updtaed **
** together. If either updated fails then all other updated should be **
** reverted back *************************************************
DATA : ls_emp_hdr TYPE zemp_header,
              lt_emp_item TYPE TABLE OF zemp_item_det,
              ls_emp_item TYPE zemp_item_det.

START-OF-SELECTION.

******fill emp header *****
  ls_emp_hdr-emp_id = '00001'.
  ls_emp_hdr-emp_name = 'Rajat Shankar'.
  ls_emp_hdr-emp_desig = 'Senior Softare Engg'.
  ls_emp_hdr-emp_mobile_no = '9090909090'.
  ls_emp_hdr-emp_email_id = 'raja@gmail.com'.
******fill emp item*****
  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = 'HSC'.
  ls_emp_item-yr_pass = '2010'.
  ls_emp_item-divison = '1'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  ls_emp_item-emp_id = '00001'.

  ls_emp_item-edu_type = '12TH'.
  ls_emp_item-yr_pass = '2012'.
  ls_emp_item-divison = '2'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  INSERT INTO zemp_header VALUES ls_emp_hdr.

  IF sy-subrc = 0 .
    COMMIT WORK.
  ENDIF.
  INSERT  zemp_item_det FROM TABLE lt_emp_item.
  IF sy-subrc = 0 .
**manually rollback it to test our requirement but **
**this thing may happen automatically in some certain cases **
    ROLLBACK WORK.
  ENDIF.
  WRITE :/ 'Employee :', ls_emp_hdr-emp_id , 'details is updated'.

_________________________________________________________________________________
Step10.











Step11. The Header table is updated properly.











Step12. But the ITEM table is not updated with any records leading to data inconsistencies.










CASE-3
Before testing the CASE-3 delete all entries from the two data base tables, so that no need to change the data in the program.
Step13. In this Case, Put a Rollback for the Header table and execute the report.






 Step14.
________________________________________________________________________________
REPORT  zupdate_details.
** The requirement is the header and the item table should be updtaed **
** together. If either updated fails then all other updated should be **
** reverted back *************************************************
DATA : ls_emp_hdr TYPE zemp_header,
             lt_emp_item TYPE TABLE OF zemp_item_det,
             ls_emp_item TYPE zemp_item_det.

START-OF-SELECTION.
******fill emp header *****
  ls_emp_hdr-emp_id = '00001'.
  ls_emp_hdr-emp_name = 'Rajat Shankar'.
  ls_emp_hdr-emp_desig = 'Senior Softare Engg'.
  ls_emp_hdr-emp_mobile_no = '9090909090'.
  ls_emp_hdr-emp_email_id = 'raja@gmail.com'.
******fill emp item*****
  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = 'HSC'.
  ls_emp_item-yr_pass = '2010'.
  ls_emp_item-divison = '1'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = '12TH'.
  ls_emp_item-yr_pass = '2012'.
  ls_emp_item-divison = '2'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  INSERT INTO zemp_header VALUES ls_emp_hdr.
  IF sy-subrc = 0 .
**manually rollback it to test our requirement but **
**this thing may happen automatically in some certain cases **
    ROLLBACK WORK.
  ENDIF.
  INSERT  zemp_item_det FROM TABLE lt_emp_item.
  IF sy-subrc = 0 .
    COMMIT WORK.
  ENDIF.
  WRITE :/ 'Employee :', ls_emp_hdr-emp_id , 'details is updated'.
_________________________________________________________________________________

Step15.









Step16. The header table is not updated due to the rollback.












Step17. The ITEM table is updated properly.











CASE-4
Before testing the CASE-4 delete all entries from the two data base tables, so that no need to change the data in the program.
Step18. Put ROLLBACK for both Header and Item.

Step19.
_________________________________________________________________________________

REPORT  zupdate_details.
** The requirement is the header and the item table should be updtaed **
** together. If either updated fails then all other updated should be **
** reverted back *************************************************
DATA : ls_emp_hdr TYPE zemp_header,
              lt_emp_item TYPE TABLE OF zemp_item_det,
              ls_emp_item TYPE zemp_item_det.

START-OF-SELECTION.
******fill emp header *****
  ls_emp_hdr-emp_id = '00001'.
  ls_emp_hdr-emp_name = 'Rajat Shankar'.
  ls_emp_hdr-emp_desig = 'Senior Softare Engg'.
  ls_emp_hdr-emp_mobile_no = '9090909090'.
  ls_emp_hdr-emp_email_id = 'raja@gmail.com'.
******fill emp item*****
  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = 'HSC'.
  ls_emp_item-yr_pass = '2010'.
  ls_emp_item-divison = '1'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = '12TH'.
  ls_emp_item-yr_pass = '2012'.
  ls_emp_item-divison = '2'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  INSERT INTO zemp_header VALUES ls_emp_hdr.
  IF sy-subrc = 0 .
**manually rollback it to test our requirement but **
**this thing may happen automatically in some certain cases **
    ROLLBACK WORK.
  ENDIF.
  INSERT  zemp_item_det FROM TABLE lt_emp_item.
  IF sy-subrc = 0 .
**manually rollback it to test our requirement but **
**this thing may happen automatically in some certain cases **
    ROLLBACK WORK.
  ENDIF.
  WRITE :/ 'Employee :', ls_emp_hdr-emp_id , 'details is updated'.
_________________________________________________________________________________
Step20.










Step21. No records are updated in the Header table.

Step22. No records are updated in the ITEM tale.

_________________________________________________________________________________
In this Above 4 cases, things are looking fine in CASE-1 & in CASE-4 .But in CASE-2 & CASE-3
there are some data inconsistencies like header contains the record but the dependent item doesn't have any or the Item table contains the records and the header is empty.
To over come this situation, lets use UPDATE FUNCTION MODULE to apply SAP LUW technique.
_________________________________________________________________________________

Step23. Create a Function group and create a FM.


















Step24. Provide the importing parameters.












Step25. Provide below source code in the FM.














Step26. Create another FM for the ITEM table update.

















Step27. Provide the tables.











Step28. Provide below source code in the FM.
















CASE-5
Before testing the CASE-5 delete all entries from the two data base tables, so that no need to change the data in the program.

Step29. Call the above UPDATE function modules and execute the report


























Step30.
_________________________________________________________________________________
REPORT  zupdate_details.
** The requirement is the header and the item table should be updtaed **
** together. If either updated fails then all other updated should be **
** reverted back *************************************************
DATA : ls_emp_hdr TYPE zemp_header,
              lt_emp_item TYPE TABLE OF zemp_item_det,
              ls_emp_item TYPE zemp_item_det.

START-OF-SELECTION.
******fill emp header *****
  ls_emp_hdr-emp_id = '00001'.
  ls_emp_hdr-emp_name = 'Rajat Shankar'.
  ls_emp_hdr-emp_desig = 'Senior Softare Engg'.
  ls_emp_hdr-emp_mobile_no = '9090909090'.
  ls_emp_hdr-emp_email_id = 'raja@gmail.com'.
******fill emp item*****
  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = 'HSC'.
  ls_emp_item-yr_pass = '2010'.
  ls_emp_item-divison = '1'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = '12TH'.
  ls_emp_item-yr_pass = '2012'.
  ls_emp_item-divison = '2'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  CALL FUNCTION 'UPDATE_EMP_HEADER' IN UPDATE TASK
    EXPORTING
      i_emp_hdr = ls_emp_hdr.

  CALL FUNCTION 'UPDATE_EMP_ITEM' IN UPDATE TASK

    TABLES
      lt_emp_item = lt_emp_item.

  WRITE :/ 'Employee :', ls_emp_hdr-emp_id , 'details is updated'.

_________________________________________________________________________________

Step31.












Step32. No records updated in the header table.










Step33. No record updated in the ITEM TABLE.










________________________________________________________________________________
In the above CASE-5, the program executed successfully and call to the UPDATE FMs are made but the FM codes are not executed. The Update FMs source code executes when it found any COMMIT WORK statement in the program explicitly. Though Implicit Commit Work happens at the end of the program execution but UPDATE FM execution starts only when explicit commit work is mentioned in the program.
________________________________________________________________________________
CASE-6
Before testing the CASE-6 delete all entries from the two data base tables, so that no need to change the data in the program.
Step34. This time put a explicit COMMIT WORK statement in the Program and also put DEBUG POINT at Commit Work statement.



























Step35.
_________________________________________________________________________________
REPORT  zupdate_details.
** The requirement is the header and the item table should be updtaed **
** together. If either updated fails then all other updated should be **
** reverted back *************************************************
DATA : ls_emp_hdr TYPE zemp_header,
              lt_emp_item TYPE TABLE OF zemp_item_det,
             ls_emp_item TYPE zemp_item_det.

START-OF-SELECTION.
******fill emp header *****
  ls_emp_hdr-emp_id = '00001'.
  ls_emp_hdr-emp_name = 'Rajat Shankar'.
  ls_emp_hdr-emp_desig = 'Senior Softare Engg'.
  ls_emp_hdr-emp_mobile_no = '9090909090'.
  ls_emp_hdr-emp_email_id = 'raja@gmail.com'.
******fill emp item*****
  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = 'HSC'.
  ls_emp_item-yr_pass = '2010'.
  ls_emp_item-divison = '1'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = '12TH'.
  ls_emp_item-yr_pass = '2012'.
  ls_emp_item-divison = '2'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  CALL FUNCTION 'UPDATE_EMP_HEADER' IN UPDATE TASK
    EXPORTING
      i_emp_hdr = ls_emp_hdr.
  CALL FUNCTION 'UPDATE_EMP_ITEM' IN UPDATE TASK
    TABLES
      lt_emp_item = lt_emp_item.

  commit work . " Calls the execution of Update function modules in update Workprocess

  WRITE :/ 'Employee :', ls_emp_hdr-emp_id , 'details is updated'.


_________________________________________________________________________________

Step36. The debugger stops at this point. There are some DB tables that holds UPDATE FM call, data for temporary period of time.  The tables are VBHDR, VBMOD & VBDATA.


















Step37. At this point header table is not updated.









Step38. Also ITEM table is not updtaed.










Step39. Go to VBHDR table and Provide Your Report name that uses Update FM calls.































Step40. The contest are displayed. It contains VBKEY and the transaction name from where it is called (in this case -SE38).









Step41. Go to the VBMOD table in SE11 and provide the update FM names .














Step42. It also displays the VBKEY and the FM names .












Step43. Go to VBDATA table and provide VBKEY values that we obtain from VBHDR or  from VBMOD table.

















Step44. It displays the data lengths.















Step45. Press F5 button to execute the commit work statement.




















Step46. The Header table is updated successfully.












Step47. The ITEM table is also updated successfully.











CASE-7
Before testing the CASE-7 delete all entries from the two data base tables, so that no need to change the data in the program.
Step48.  Go to the ITEM update FM and put some extra code. Put a Message type 'A' to fail the DATABASE update.
















_______________________________________________________________________________

REPORT  zupdate_details.
** The requirement is the header and the item table should be updtaed **
** together. If either updated fails then all other updated should be **
** reverted back *************************************************
DATA : ls_emp_hdr TYPE zemp_header,
              lt_emp_item TYPE TABLE OF zemp_item_det,
              ls_emp_item TYPE zemp_item_det.

START-OF-SELECTION.
******fill emp header *****
  ls_emp_hdr-emp_id = '00001'.
  ls_emp_hdr-emp_name = 'Rajat Shankar'.
  ls_emp_hdr-emp_desig = 'Senior Softare Engg'.
  ls_emp_hdr-emp_mobile_no = '9090909090'.
  ls_emp_hdr-emp_email_id = 'raja@gmail.com'.
******fill emp item*****
  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = 'HSC'.
  ls_emp_item-yr_pass = '2010'.
  ls_emp_item-divison = '1'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = '12TH'.
  ls_emp_item-yr_pass = '2012'.
  ls_emp_item-divison = '2'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  CALL FUNCTION 'UPDATE_EMP_HEADER' IN UPDATE TASK
    EXPORTING
      i_emp_hdr = ls_emp_hdr.

**Termination in Item details FModule**
  CALL FUNCTION 'UPDATE_EMP_ITEM' IN UPDATE TASK
    TABLES
      lt_emp_item = lt_emp_item.


  commit work . " Calls the execution of Update function modules in update Workprocess

  WRITE :/ 'Employee :', ls_emp_hdr-emp_id , 'details is updated'.
________________________________________________________________________________
Step49. The below output appears. Click on BACK button.













Step50. The below screen appears saying that UPDATE FM Termination. Click on the INBOX button.
















Step51. Click on the first highlighted message in the INBOX section.

















Step52. It shows the details. The Message content that we have mentioned in the ITEM FM appears here.





















Step53. No header records are updated.









Step54. No ITEM records are updated.









In this case the Header Update FM is called first and one record is inserted in the Header table and while calling the ITEM update FM, there is an error, so as both UPDATE FMs are combined into a single SAP LUW and the ITEM FM fails, the DB update made by the Header FM is also reverted back.

Step55. To see the Update FM failure details, Use Tcode- SM13.








Step56. Click on execute.


























Step57. Double click on the first Update Failure record in the ALV display.










Step58. It shows the details. There was error in the ITEM. function module










CASE-8
Before testing the CASE-8 delete all entries from the two data base tables, so that no need to change the data in the program.
Step59. Put a Message type 'A' in the header Update FM.
















Step60. ITEM Update FM.
















________________________________________________________________________________

REPORT  zupdate_details.
** The requirement is the header and the item table should be updtaed **
** together. If either updated fails then all other updated should be **
** reverted back *************************************************
DATA : ls_emp_hdr TYPE zemp_header,
              lt_emp_item TYPE TABLE OF zemp_item_det,
              ls_emp_item TYPE zemp_item_det.

START-OF-SELECTION.
******fill emp header *****
  ls_emp_hdr-emp_id = '00001'.
  ls_emp_hdr-emp_name = 'Rajat Shankar'.
  ls_emp_hdr-emp_desig = 'Senior Softare Engg'.
  ls_emp_hdr-emp_mobile_no = '9090909090'.
  ls_emp_hdr-emp_email_id = 'raja@gmail.com'.
******fill emp item*****
  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = 'HSC'.
  ls_emp_item-yr_pass = '2010'.
  ls_emp_item-divison = '1'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

  ls_emp_item-emp_id = '00001'.
  ls_emp_item-edu_type = '12TH'.
  ls_emp_item-yr_pass = '2012'.
  ls_emp_item-divison = '2'.
  APPEND ls_emp_item TO lt_emp_item.
  CLEAR ls_emp_item.

**Termination in Header FModule**
  CALL FUNCTION 'UPDATE_EMP_HEADER' IN UPDATE TASK
    EXPORTING
      i_emp_hdr = ls_emp_hdr.

  CALL FUNCTION 'UPDATE_EMP_ITEM' IN UPDATE TASK

    TABLES
      lt_emp_item = lt_emp_item.

  commit work . " Calls the execution of Update function modules in update Workprocess
  WRITE :/ 'Employee :', ls_emp_hdr-emp_id , 'details is updated'.

________________________________________________________________________________
Step61. The below output appears. Click on back button.














Step62. the below Update Termination message comes up. Click on the Inbox button.












Step63. Click on the first message in the Inbox.



















Step64. Te details of the error message it shows.






















Step65. Go to SM13 and double click on the first line.










Step66. It show the error in the Header Update FM.











Also in this case, check the two database tables, none of the tables are updated.
_______________________________________________________________________________

No comments:

Comments system

Disqus Shortname