Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Step bystep abap_changinga_singlerecord


Published on

Published in: Technology
  • Be the first to comment

Step bystep abap_changinga_singlerecord

  1. 1. Updating Single Record (ABAP)Task: Changing a Single RecordProgram: SAPMZ##_CUSTOMER1Transaction: Z##_CUSTOMER1Copy template: SAPBC414T_CREATE_CUSTOMER_01Model Solution: SAPBC414S_CREATE_CUSTOMER_01Our Program name: Z027393A_CUSTOMER1The exercise makes you copy the template SAPBC414T_CREATE_CUSTOMER_01 toSAPMZ##_CUSTOMER1 where ## is the student number, but here we will createthe complete exercise from scratch. Also, the exercise uses Module Pool to createthe program; here we are creating the program using Function Pool.Information: SCUSTOM is the database table that holds the Customer data. Theapplication is written to save the data to this table. Use Transaction Code SE11 forgetting the record description and field description. And use Transaction Code SE16to see the existing data. After the program is completed and executed, a new rowwill be added to this table and can be viewed using the same transaction codeSE16.Table and Fields:SCUSTOM --> Flight Customer Data MANDT - Client ID - Customer Number NAME - Customer name FORM - Form of address STREET - Street POSTBOX - PO Box POSTCODE - Postal Code CITY - City COUNTRY - Country code REGION - Region TELEPHONE - Telephone number of flight customer CUSTTYPE - Customer type DISCOUNT - Discount rate LANGU - Language KeyLaunch SE80.Ensure that you are on the ‘Repository Browser’.Select Package from the drop down and enter your Package name [ZxxxxxxA]where xxxxxx is your SAP Login ID or Oracle ID, then click on the [Display] button.Create the package if the package does not already exist.Now create a Function Module, by right clicking the ‘Function Group’ item andselecting ‘Create’ as shown. 1 of 31
  2. 2. Updating Single Record (ABAP)Enter the Function Group (name) and Short Text (description.) 2 of 31
  3. 3. Updating Single Record (ABAP)Click on Save.Click on Save Symbol Icon.Click on Continue Icon or hit Enter.The system automatically creates two include functions as shown below. 3 of 31
  4. 4. Updating Single Record (ABAP)Double click on the LZ027393A_CUSTOMER1TOP include file and enter the followingcode.FUNCTION-POOL Z027393A_CUSTOMER1 MESSAGE-ID bc414.DATA: answer, flag.DATA: ok_code LIKE sy-ucomm, save_ok LIKE ok_code.TABLES: scustom. “This command is not obsolete for use in this situationThe command TABLES: scustom creates the SCUSTOM structure that retains thenewly entered (created) customer data. 4 of 31
  5. 5. Updating Single Record (ABAP)The BC414 message class is declared as a program default in the PROGRAMstatement (see syntax MESSAGE-ID bc414 in the above code) and therefore validthroughout the programs.Save the Function Module by clicking on the Save button at the top.Since we need to generate a unique ID for each new customer generated, we needto use SAP functionality that does that. This Number Range Object/Buffer can becreated new or existing one used [Use Transaction Code SNRO to create andmaintain new Number Range Object.] We are using the existing object SBUSPID.SAP also provides us with a function NUMBER_GET_NEXT to get the IDS using thisobject. We will first create a Subroutine to access the function in addition to othersubroutines been created.Create the following subroutines as shown below, by right clicking on the FunctionGroup name and selecting Create > Subroutine. 5 of 31
  6. 6. Updating Single Record (ABAP)Enter the name of the subroutine ‘ASK_LOSS’, SAP suggest a new Include for thissubroutine, select the suggested name ‘LZ027393A_CUSTOMER1F01’ – we will usethe same include to save all the subroutines that we will create shortly. 6 of 31
  7. 7. Updating Single Record (ABAP)Accept the warning that may show as follows: 7 of 31
  8. 8. Updating Single Record (ABAP)FORM ask_loss USING p_answer. CALL FUNCTION POPUP_TO_CONFIRM_LOSS_OF_DATA EXPORTING textline1 = Continue?(004) titel = Create Customer(003) IMPORTING answer = p_answer.ENDFORM. " ASK_LOSSSimilarly create the following subroutines by right clicking on the Function Groupname and selecting Create > Subroutine.ASK_SAVENUMBER_GET_NEXTSAVESAVE_SCUSTOMFORM NUMBER_GET_NEXT USING p_scustom LIKE scustom. DATA: return TYPE inri-returncode.* get next free number in the number range 01* of number objectSBUSPID CALL FUNCTION NUMBER_GET_NEXT EXPORTING nr_range_nr = 01 object = SBUSPID IMPORTING number = p_scustom-id returncode = return EXCEPTIONS OTHERS = 1. CASE sy-subrc. WHEN 0. CASE return. WHEN 1.* number of remaining numbers critical MESSAGE s070. WHEN 2.* last number MESSAGE s071. WHEN 3.* no free number left over MESSAGE a072. ENDCASE. WHEN 1.* internal error MESSAGE a073 WITH sy-subrc. 8 of 31
  9. 9. Updating Single Record (ABAP) ENDCASE.ENDFORM. " NUMBER_GET_NEXTFORM ASK_SAVE USING p_answer.* POPUP_TO_CONFIRM_STEP is obsolete, so using POPUP_TO_CONFIRM below* CALL FUNCTION POPUP_TO_CONFIRM_STEP* EXPORTING* textline1 = Data has been changed.(001)* textline2 = Save before leaving transaction?(002)* titel = Create Customer(003)* IMPORTING* answer = function POPUP_TO_CONFIRMexportingtitlebar = Create Customer(003)text_question = Data has been changed. Save before leaving transaction? text_button_1 = Yesicon_button_1 = ICON_YEStext_button_2 = Noicon_button_2 = ICON_NOdefault_button = 2display_cancel_button = Ximportinganswer = p_answer.ENDFORM. " ASK_SAVEFORM SAVE.* get SCUSTOM-ID from number range object BC_SCUSTOM PERFORM number_get_next USING scustom.* save new customer PERFORM save_scustom.ENDFORM.FORM SAVE_SCUSTOM. INSERT INTO scustom VALUES scustom. IF sy-subrc <> 0.* insertion of dataset in DB-table not possible MESSAGE a048. ELSE.* insertion successfull MESSAGE s015 WITH scustom-id. ENDIF.ENDFORM. 9 of 31
  10. 10. Updating Single Record (ABAP)Create GUI Status (Menu) as shown below. 10 of 31
  11. 11. Updating Single Record (ABAP)Click on the ‘+’ next to the Menu Bar and add the following text and labels.Now double click on the ‘Edit’ text (next to the Customer Data label that wasentered) 11 of 31
  12. 12. Updating Single Record (ABAP)Delete all the following entries using button on the top menu, except theCancel line and add the Code as shownSave the changes. 12 of 31
  13. 13. Updating Single Record (ABAP)Now maximize the Function Keys by clicking on the ‘+’ sign next to it.Enter SAVE, BACK, EXIT and CANCEL function code next (above) the save, back,exit and Cancel icon as shown below under the Standard Toolbar button.Enter the fast text as shown below for these buttons. Leave Function Type as blank(Blank defaults to Application Function) for Save and Back button and E (exit) forExit and Cancel. 13 of 31
  14. 14. Updating Single Record (ABAP) 14 of 31
  15. 15. Updating Single Record (ABAP)Save the GUI Status.Now create GUI title 15 of 31
  16. 16. Updating Single Record (ABAP)Enter the Title Code and Title as shown belowClick on enter to save the GUI title.Save and Activate the program, by clicking on the Activate button.Creating the screen.Now create a Screen, by right clicking the ‘Function Group’ item and selecting‘Create’ and then ‘Screen’ as shown. 16 of 31
  17. 17. Updating Single Record (ABAP)Enter Screen number as shownClick Continue button or hit enter. 17 of 31
  18. 18. Updating Single Record (ABAP)Click on the Layout button (or Control + F7) 18 of 31
  19. 19. Updating Single Record (ABAP)Click on the Dictionary/Program Field Window button or hit F6.In the Table/Field Name field enter ‘SCUSTOM’ and then click on the ‘Get fromDictionary’ button. Important note – by selecting these fields from the databasetables [in repository] all the fields automatically get all the labels/field help/etcdefaulted from their respective fields’ definition in the data dictionary.Select the fields as shown. 19 of 31
  20. 20. Updating Single Record (ABAP)Click on the Continue button.Click on the blank screen somewhere near the top left corner, so that the selectedfields get copied over to the screen. 20 of 31
  21. 21. Updating Single Record (ABAP)Do not convert any fields if requested (in this simple example.)Rearrange the fields as shown. Add 4 boxes around the groupings. 21 of 31
  22. 22. Updating Single Record (ABAP)Save the screen and exit the Screen Painter (Screen > Exit)Go to the ‘Element List’ tab of the Screen (this tab is between the ‘Attributes’ and‘Flow Logic’ tab. On the ‘General Attributes’ sub tab enter the Frame01, Frame02,Frame03 and Frame04 for the four unnamed boxes created. Also enter the‘OK_CODE’ name for the field name that has the ‘Type of screen element’ OK (thisis automatically created by the screen when the screen is created – we provide aname.) During run time this field OK_CODE will have the name of the function codeof the button or key that was clicked by user. 22 of 31
  23. 23. Updating Single Record (ABAP)On the ‘Flow Logic’ tab enter the following code for the Process Before Output(PBO) and Process After Input (PAI) logic.PROCESS BEFORE OUTPUT. MODULE STATUS_0100.PROCESS AFTER INPUT. MODULE EXIT AT EXIT-COMMAND. MODULE SAVE_OK_CODE. 23 of 31
  24. 24. Updating Single Record (ABAP) FIELD: scustom-name MODULE mark_changed ON REQUEST. MODULE USER_COMMAND_0100.Save the changes.Create the modules STATUS_0100 and USER_COMMAND_0100 by forwardnavigation (i.e., double click on any module name(s), since these modules do notexist currently, the system will prompt us, to create these.) These modules arecreated in the appropriate INCLUDE file.Double click on STATUS_0100 in the above ABAP editor. At the following promptselect ‘Yes’.Select the recommended new include file that the system shows.Note that the alphabet ‘O’ in the name (3rd character from last) stands for OUTPUTof PROCESS BEFORE OUTPUT (PBO) modules and all the output modules may becreated here. 24 of 31
  25. 25. Updating Single Record (ABAP)Select Continue button or hit Enter.Modify the module definition as shown below.*----------------------------------------------------------------------****INCLUDE LZ027393A_CUSTOMER1O01 .*----------------------------------------------------------------------*MODULE STATUS_0100 OUTPUT. SET PF-STATUS DYN_0100. SET TITLEBAR DYN_0100.ENDMODULE. " STATUS_0100 OUTPUTSave and hit the Back button (F3) to go back to the Flow Logic of the screen.Now double click on the USER_COMMAND_0100 name.Select Yes. 25 of 31
  26. 26. Updating Single Record (ABAP)Accept the new INCLUDE file name for this new INPUT (PAI – Process after Input)module.Click on Continue button or hit Enter.Enter the following code in this INCLUDE function.*----------------------------------------------------------------------****INCLUDE LZ027393A_CUSTOMER1I01 .*----------------------------------------------------------------------**&---------------------------------------------------------------------**& Module EXIT INPUT*&---------------------------------------------------------------------*MODULE exit INPUT. CASE ok_code. WHEN EXIT. IF sy-datar IS INITIAL AND flag IS INITIAL.* no changes on screen 100 LEAVE PROGRAM. ELSE. 26 of 31
  27. 27. Updating Single Record (ABAP) PERFORM ask_save USING answer.* CASE answer.* WHEN J.* ok_code = SAVE&EXIT.* WHEN N.* LEAVE PROGRAM.* WHEN A.* CLEAR ok_code.* SET SCREEN 100.* ENDCASE. CASE answer. WHEN 1. ok_code = SAVE&EXIT. WHEN 2. LEAVE PROGRAM. WHEN A. CLEAR ok_code. SET SCREEN 100. ENDCASE. ENDIF. WHEN CANCEL. IF sy-datar IS INITIAL AND flag IS INITIAL.* no changes on screen 100 LEAVE TO SCREEN 0. ELSE. PERFORM ask_loss USING answer. CASE answer. WHEN J. LEAVE TO SCREEN 0. WHEN N. CLEAR ok_code. SET SCREEN 100. ENDCASE. ENDIF. ENDCASE.ENDMODULE. " EXIT INPUT*&---------------------------------------------------------------------**& Module SAVE_OK_CODE INPUT*&---------------------------------------------------------------------*MODULE save_ok_code INPUT. save_ok = ok_code. CLEAR ok_code.ENDMODULE. " SAVE_OK_CODE INPUT*&---------------------------------------------------------------------**& Module USER_COMMAND_0100 INPUT*&---------------------------------------------------------------------* 27 of 31
  28. 28. Updating Single Record (ABAP)MODULE user_command_0100 INPUT. CASE save_ok. WHEN SAVE&EXIT. PERFORM save. LEAVE PROGRAM. WHEN SAVE. IF flag IS INITIAL. SET SCREEN 100. ELSE. PERFORM save. SET SCREEN 0. ENDIF. WHEN BACK. IF flag IS INITIAL. SET SCREEN 0. ELSE. PERFORM ask_save USING answer. CASE answer. WHEN J. PERFORM save. SET SCREEN 0. WHEN N. SET SCREEN 0. WHEN A. SET SCREEN 100. ENDCASE. ENDIF. ENDCASE.ENDMODULE. " USER_COMMAND_0100 INPUT*&---------------------------------------------------------------------**& Module MARK_CHANGED INPUT*&---------------------------------------------------------------------*MODULE mark_changed INPUT.* set flag to mark changes were made on screen 100 flag = X.ENDMODULE. " MARK_CHANGED INPUTSave and Activate the program (including the screens, etc)Create a transaction code to execute this program. 28 of 31
  29. 29. Updating Single Record (ABAP)Enter the program name and description.Click on Continue button or hit Enter. 29 of 31
  30. 30. Updating Single Record (ABAP)Save - choose the appropriate package and workbench request. 30 of 31
  31. 31. Updating Single Record (ABAP)Run the transaction. 31 of 31