1. Updating Single Record (ABAP)
Task: Changing a Single Record
Program: SAPMZ##_CUSTOMER1
Transaction: Z##_CUSTOMER1
Copy template: SAPBC414T_CREATE_CUSTOMER_01
Model Solution: SAPBC414S_CREATE_CUSTOMER_01
Our Program name: Z027393A_CUSTOMER1
The exercise makes you copy the template SAPBC414T_CREATE_CUSTOMER_01 to
SAPMZ##_CUSTOMER1 where ## is the student number, but here we will create
the complete exercise from scratch. Also, the exercise uses Module Pool to create
the program; here we are creating the program using Function Pool.
Information: SCUSTOM is the database table that holds the Customer data. The
application is written to save the data to this table. Use Transaction Code SE11 for
getting the record description and field description. And use Transaction Code SE16
to see the existing data. After the program is completed and executed, a new row
will be added to this table and can be viewed using the same transaction code
SE16.
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 Key
Launch 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 and
selecting ‘Create’ as shown.
1 of 31
2. Updating Single Record (ABAP)
Enter the Function Group (name) and Short Text (description.)
2 of 31
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. Updating Single Record (ABAP)
Double click on the LZ027393A_CUSTOMER1TOP include file and enter the following
code.
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 situation
The command TABLES: scustom creates the SCUSTOM structure that retains the
newly entered (created) customer data.
4 of 31
5. Updating Single Record (ABAP)
The BC414 message class is declared as a program default in the PROGRAM
statement (see syntax MESSAGE-ID bc414 in the above code) and therefore valid
throughout 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 need
to use SAP functionality that does that. This Number Range Object/Buffer can be
created new or existing one used [Use Transaction Code SNRO to create and
maintain 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 this
object. We will first create a Subroutine to access the function in addition to other
subroutines been created.
Create the following subroutines as shown below, by right clicking on the Function
Group name and selecting Create > Subroutine.
5 of 31
6. Updating Single Record (ABAP)
Enter the name of the subroutine ‘ASK_LOSS’, SAP suggest a new Include for this
subroutine, select the suggested name ‘LZ027393A_CUSTOMER1F01’ – we will use
the same include to save all the subroutines that we will create shortly.
6 of 31
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_LOSS
Similarly create the following subroutines by right clicking on the Function Group
name and selecting Create > Subroutine.
ASK_SAVE
NUMBER_GET_NEXT
SAVE
SAVE_SCUSTOM
FORM NUMBER_GET_NEXT USING p_scustom LIKE scustom.
DATA: return TYPE inri-returncode.
* get next free number in the number range '01'
* of number object'SBUSPID'
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. Updating Single Record (ABAP)
ENDCASE.
ENDFORM. " NUMBER_GET_NEXT
FORM 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 = p_answer.
call function 'POPUP_TO_CONFIRM'
exporting
titlebar = 'Create Customer'(003)
text_question = 'Data has been changed. Save before leaving transaction? '
text_button_1 = 'Yes'
icon_button_1 = 'ICON_YES'
text_button_2 = 'No'
icon_button_2 = 'ICON_NO'
default_button = '2'
display_cancel_button = 'X'
importing
answer = p_answer.
ENDFORM. " ASK_SAVE
FORM 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
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 was
entered)
11 of 31
12. Updating Single Record (ABAP)
Delete all the following entries using button on the top menu, except the
Cancel line and add the Code as shown
Save the changes.
12 of 31
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) for
Exit and Cancel.
13 of 31
16. Updating Single Record (ABAP)
Enter the Title Code and Title as shown below
Click 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. Updating Single Record (ABAP)
Enter Screen number as shown
Click Continue button or hit enter.
17 of 31
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 from
Dictionary’ button. Important note – by selecting these fields from the database
tables [in repository] all the fields automatically get all the labels/field help/etc
defaulted from their respective fields’ definition in the data dictionary.
Select the fields as shown.
19 of 31
20. Updating Single Record (ABAP)
Click on the Continue button.
Click on the blank screen somewhere near the top left corner, so that the selected
fields get copied over to the screen.
20 of 31
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. 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 (this
is automatically created by the screen when the screen is created – we provide a
name.) During run time this field OK_CODE will have the name of the function code
of the button or key that was clicked by user.
22 of 31
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. 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 forward
navigation (i.e., double click on any module name(s), since these modules do not
exist currently, the system will prompt us, to create these.) These modules are
created in the appropriate INCLUDE file.
Double click on STATUS_0100 in the above ABAP editor. At the following prompt
select ‘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 OUTPUT
of PROCESS BEFORE OUTPUT (PBO) modules and all the output modules may be
created here.
24 of 31
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 OUTPUT
Save 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. 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. 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. 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 INPUT
Save and Activate the program (including the screens, etc)
Create a transaction code to execute this program.
28 of 31
29. Updating Single Record (ABAP)
Enter the program name and description.
Click on Continue button or hit Enter.
29 of 31
30. Updating Single Record (ABAP)
Save - choose the appropriate package and workbench request.
30 of 31