Vous êtes sur la page 1sur 28

ABAP Tutor Page 1

User-Defined table Employee Master


ABAP Tutor Page 2

Internal table Control Break Statement

REPORT ZANAND3 .

DATA: BEGIN OF ITAB OCCURS 0,


DEPTNO TYPE ZEMP99-DEPTNO,
EMPNO TYPE ZEMP99-EMPNO,
EMPNAME TYPE ZEMP99-EMPNAME,
SALARY TYPE P DECIMALS 2,
END OF ITAB.

SELECT * INTO CORRESPONDING FIELDS OF TABLE ITAB FROM ZEMP99 WHERE EMPNO
> 450 .
* Control Break statement

*step 1: sort by field name.


SORT ITAB BY DEPTNO.

*step 2 : loop internal table.


LOOP AT ITAB.
AT FIRST.
WRITE :/5 SY-ULINE(70).
WRITE :/5 'Dept-no',15 'Employee No' ,30 'Name',60 'salary'.
WRITE :/5 SY-ULINE(70).
ENDAT.
AT NEW DEPTNO.
WRITE :/5 ITAB-DEPTNO.
ENDAT.

WRITE :/15 ITAB-EMPNO,30 ITAB-EMPNAME,60 ITAB-SALARY.

AT END OF DEPTNO.
ABAP Tutor Page 3

SUM.
WRITE :/5 SY-ULINE(70).
WRITE :/5 'Total salary for this dept is :' ,60 ITAB-SALARY.
WRITE :/5 SY-ULINE(70).
ENDAT.
AT LAST.
SUM.
WRITE :/5 SY-ULINE(70).
WRITE :/5 'Total salary for this dept is :' ,60 ITAB-SALARY.
WRITE :/5 SY-ULINE(70).

ENDAT.
ENDLOOP.

Classical Report Programming

************************************************************************
* Program Name : zanand4.
* Created on : 24-03-02
* Purpose : Demo on Classical Report Programming
* Author : Anand
************************************************************************
REPORT ZANAND4 LINE-COUNT 25(3)
line-size 100
NO STANDARD PAGE HEADING.
************************************************************************
INITIALIZATION.

* Declaration of Internal Table


ABAP Tutor Page 4

DATA: BEGIN OF ITAB OCCURS 0,


EMPNO TYPE ZEMP99-EMPNO,
EMPNAME TYPE ZEMP99-EMPNAME,
DEPTNO TYPE ZEMP99-DEPTNO,
SALARY TYPE ZEMP99-SALARY,
END OF ITAB.
DATA : TOTALSALARY TYPE ZEMP99-SALARY VALUE '0.00'.
************************************************************************
TOP-OF-PAGE.
WRITE :/5 SY-ULINE(85).
WRITE :/5 SY-VLINE,'EmpNo',
20 SY-VLINE,'Name',
55 SY-VLINE,'Deptno',
65 SY-VLINE,'Salary',
89 SY-VLINE.
WRITE :/5 SY-ULINE(85).
************************************************************************
END-OF-PAGE.
WRITE :/5 SY-VLINE,5 SY-ULINE(85).
WRITE :/5 SY-VLINE,
35 'Total for page ',sy-pagno left-justified,
65 SY-VLINE,TOTALSALARY DECIMALS 2,
89 SY-VLINE.
WRITE :/5 SY-VLINE,5 SY-ULINE(85).
************************************************************************
START-OF-SELECTION.
* Populating Internal Table.
SELECT * FROM ZEMP99 INTO TABLE ITAB where empno > 450.

* Displaying Internal Table.


LOOP AT ITAB.
WRITE :/5 SY-VLINE,ITAB-EMPNO LEFT-JUSTIFIED,
20 SY-VLINE,ITAB-EMPNAME,
55 SY-VLINE,ITAB-DEPTNO LEFT-JUSTIFIED,
65 SY-VLINE,ITAB-SALARY DECIMALS 2,
89 SY-VLINE.
TOTALSALARY = TOTALSALARY + ITAB-SALARY.
ENDLOOP.

perform chk_endofpage.
************************************************************************
END-OF-SELECTION.
* ULINE.
* WRITE :/20 ' Have a Nice Day'.
* ULINE.
************************************************************************

form chk_endofpage.
data : lin type i.
lin = sy-linct - sy-linno - 3.
do lin times.
WRITE :/5 SY-VLINE,
20 SY-VLINE,
55 SY-VLINE,
65 SY-VLINE,
89 SY-VLINE.
ENDDO.
endform.
ABAP Tutor Page 5

Interactive Reports
First Screen List of Departments
ABAP Tutor Page 6

Second Screen : List of employees working under selected department

************************************************************************
* Program Name : zanand2.
* Program Type : Report.
* SubType : Interactive Report
* Author : Anand
* Created On : 28-03-02
* Purpose : Demo on Interactive Report.
************************************************************************
REPORT ZANAND2 NO STANDARD PAGE HEADING
LINE-COUNT 25(3)
LINE-SIZE 120.

************************************************************************
* Initialization Event
************************************************************************
INITIALIZATION.

DATA: BEGIN OF DEPT OCCURS 0,


DEPTNO TYPE ZDEPT99-DEPTNO,
DEPTNAME TYPE ZDEPT99-DEPTNAME,
LOCATION TYPE ZDEPT99-LOCATION,
TOTAL TYPE ZEMP99-SALARY,
END OF DEPT.

DATA: BEGIN OF EMP OCCURS 0,


EMPNO TYPE ZEMP99-EMPNO,
EMPNAME TYPE ZEMP99-EMPNAME,
DEPTNO TYPE ZEMP99-DEPTNO,
SALARY TYPE ZEMP99-SALARY,
END OF EMP.
ABAP Tutor Page 7

DATA : DEPT_PAGE_TOTAL TYPE ZEMP99-SALARY ,


EMP_PAGE_TOTAL TYPE ZEMP99-SALARY.

DATA : DEPT_GRAND_TOTAL TYPE ZEMP99-SALARY,


EMP_GRAND_TOTAL TYPE ZEMP99-SALARY.

DATA : FLAG1 TYPE I VALUE 0,


FLAG2 TYPE I VALUE 0.

************************************************************************
* Top of Page Event.
************************************************************************
TOP-OF-PAGE.
IF FLAG1 = 0.
PERFORM DISPLAY_DEPT_HEADER.
ENDIF.

TOP-OF-PAGE DURING LINE-SELECTION.


IF FLAG2 = 0.
PERFORM DISPLAY_EMP_HEADER.
ENDIF.

************************************************************************
* End of Page Event
************************************************************************
END-OF-PAGE.
IF SY-LSIND = 0.
PERFORM DISPLAY_DEPT_FOOTER.
ELSE.
PERFORM DISPLAY_EMP_FOOTER.
ENDIF.

************************************************************************
* Start of Selection Event
************************************************************************
START-OF-SELECTION.
SELECT * FROM ZDEPT99 INTO CORRESPONDING FIELDS OF TABLE DEPT.
SELECT * FROM ZEMP99 INTO CORRESPONDING FIELDS OF TABLE EMP.
PERFORM UPDATE_DEPT.
PERFORM DISPLAY_DEPT_DETAIL.
************************************************************************
* End of Selection Event.
************************************************************************
END-OF-SELECTION.

************************************************************************
* At Line Selection Event
************************************************************************
AT LINE-SELECTION.
FLAG2 = 0.
PERFORM DISPLAY_EMP_DETAIL USING DEPT-DEPTNO.

************************************************************************
* User defined Sub-routines.
************************************************************************

* Department Details
FORM DISPLAY_DEPT_DETAIL.
LOOP AT DEPT.
WRITE :/ SY-VLINE,DEPT-DEPTNO,
ABAP Tutor Page 8

15 SY-VLINE,DEPT-DEPTNAME,
40 SY-VLINE,DEPT-LOCATION,
65 SY-VLINE,DEPT-TOTAL,
95 SY-VLINE.
HIDE DEPT-DEPTNO.
DEPT_PAGE_TOTAL = DEPT_PAGE_TOTAL + DEPT-TOTAL.
ENDLOOP.
PERFORM REACH_END_OF_PAGE.
ENDFORM.

*Employee Details.
FORM DISPLAY_EMP_DETAIL USING DEPTNO1.
LOOP AT EMP WHERE DEPTNO = DEPTNO1.
WRITE :/ SY-VLINE,EMP-EMPNO,
15 SY-VLINE,EMP-EMPNAME,
55 SY-VLINE,EMP-SALARY,
95 SY-VLINE.
EMP_PAGE_TOTAL = EMP_PAGE_TOTAL + EMP-SALARY.
ENDLOOP.
PERFORM REACH_END_OF_PAGE.
ENDFORM.

*Update the Department internal table with total salary .


FORM UPDATE_DEPT.
LOOP AT EMP.
READ TABLE DEPT WITH KEY DEPTNO = EMP-DEPTNO.
IF SY-SUBRC = 0.
DEPT-TOTAL = DEPT-TOTAL + EMP-SALARY.
MODIFY DEPT INDEX SY-TABIX.
ENDIF.
ENDLOOP.
ENDFORM.

* Employee Header
FORM DISPLAY_EMP_HEADER.
WRITE :/ SY-ULINE(95).
WRITE :/ SY-VLINE,'EMPLOYEE NO',
15 SY-VLINE,'NAME',
55 SY-VLINE,'SALARY',
95 SY-VLINE.
WRITE :/ SY-ULINE(95).
ENDFORM.

* Department Header
FORM DISPLAY_DEPT_HEADER.
WRITE :/ SY-ULINE(95).
WRITE :/ SY-VLINE,'DEPT. NO',
15 SY-VLINE, 'NAME',
40 SY-VLINE,'LOCATION',
65 SY-VLINE,'TOTAL SALARY',
95 SY-VLINE.
WRITE :/ SY-ULINE(95).

ENDFORM.
* Employee Footer
FORM DISPLAY_EMP_FOOTER.
WRITE :/ SY-ULINE(95).
WRITE :/ SY-VLINE,
30 'Total Amount for Page :',SY-PAGNO LEFT-JUSTIFIED,
55 SY-VLINE, EMP_PAGE_TOTAL,
95 SY-VLINE.
ABAP Tutor Page 9

WRITE :/ SY-ULINE(95).
EMP_GRAND_TOTAL = EMP_GRAND_TOTAL + EMP_PAGE_TOTAL.
EMP_PAGE_TOTAL = 0.
ENDFORM.

* Department Footer
FORM DISPLAY_DEPT_FOOTER.
WRITE :/ SY-ULINE(95).
WRITE :/ SY-VLINE,
30 'Total Amount for Page :',SY-PAGNO LEFT-JUSTIFIED,
65 SY-VLINE, DEPT_PAGE_TOTAL,
95 SY-VLINE.
WRITE :/ SY-ULINE(95).
DEPT_GRAND_TOTAL = DEPT_GRAND_TOTAL + DEPT_PAGE_TOTAL.
DEPT_PAGE_TOTAL = 0.
ENDFORM.

* Displays Department Grand Total


FORM DISPLAY_DEPT_GRANDTOTAL.
NEW-PAGE.
SKIP TO LINE 5.
WRITE :/ SY-ULINE(95).
WRITE : /20 'Grand Total FOR ALL EMPLOYEES:' ,DEPT_GRAND_TOTAL.
WRITE :/ SY-ULINE(95).
DEPT_GRAND_TOTAL = 0.
ENDFORM.

* Displays Employee Grand Total


FORM DISPLAY_EMP_GRANDTOTAL.
NEW-PAGE.
SKIP TO LINE 5.
WRITE :/ SY-ULINE(95).
WRITE : /20 'Grand Total :' ,EMP_GRAND_TOTAL.
WRITE :/ SY-ULINE(95).
EMP_GRAND_TOTAL = 0.
ENDFORM.

* To Trigger End of Page Event.


FORM REACH_END_OF_PAGE.
DATA : VSKIP TYPE I.
VSKIP = SY-LINCT - SY-LINNO - 3.

DO VSKIP TIMES.
IF SY-LSIND = 0.
WRITE :/ SY-VLINE,15 SY-VLINE,40 SY-VLINE,65 SY-VLINE,95 SY-VLINE.
ELSE.
WRITE :/ SY-VLINE,15 SY-VLINE,55 SY-VLINE,95 SY-VLINE.
ENDIF.
ENDDO.

IF SY-LSIND = 0.
FLAG1 = 1.
PERFORM DISPLAY_DEPT_GRANDTOTAL.
ELSE.
FLAG2 = 1.
PERFORM DISPLAY_EMP_GRANDTOTAL.
ENDIF.
ENDFORM.
ABAP Tutor Page 10

Dialog Programming - Simple

Flow Logic
PROCESS BEFORE OUTPUT.
* MODULE STATUS_1000.
*
PROCESS AFTER INPUT.
module exit at exit-command.
module user_command_1000.
* MODULE USER_COMMAND_1000.

Module pool program

PROGRAM ZANAND6 .

DATA: BEGIN OF EMP,


EMPNO TYPE ZEMP99-EMPNO,
EMPNAME TYPE ZEMP99-EMPNAME,
DEPTNO TYPE ZEMP99-DEPTNO,
SALARY TYPE ZEMP99-SALARY,
END OF EMP.

*&---------------------------------------------------------------------*
*& Module exit INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
ABAP Tutor Page 11

MODULE exit INPUT.


leave to screen 0.
ENDMODULE. " exit INPUT
*&---------------------------------------------------------------------*
*& Module user_command_1000 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_command_1000 INPUT.
case sy-ucomm.
WHEN 'SAVE'.
modify zemp99 from emp.
if sy-subrc = 0.
message I002(zz).
else.
MESSAGE E003(zz).
endif.
when 'DEL'.
delete from zemp99 where empno = emp-empno.
if sy-subrc = 0.
message I004(zz) with emp-empno.
else.
MESSAGE E005(zz) with emp-empno.
endif.
when others.
message E006(zz).

endcase.
ENDMODULE. " user_command_1000 INPUT

Advanced Dialog Programming in ABAP - by Anand

Dialog programming is also called as DYNPRo. There exit atleast one


Module pool program for the screen. Screens contain the controls and
programming logic is written in Module pool program.

Events in Dialog Programming


~~~~~~~~~~~~~~~~~~~~~~~~~~~~

All the events are placed in Flowlogic of screen editor.


1. Process before output (PBO).
2. Process after input (PAI).
3. Process on Value request (POV).
4. process on Help Request (POH).
ABAP Tutor Page 12

Flow in Dialog programming


~~~~~~~~~~~~~~~~~~~~~~~~~~
PAI
------>
Screeen(se51) Module Pool Programs (se38)
<------
PBO

Transaction
~~~~~~~~~~~
se38 Abap Editor
se51 Screen Painter
se93 Transaction Creator

system variables
~~~~~~~~~~~~~~~~
sy-rows = Number of rows
sy-cols = number of columns
sy-loopc = Number of visible rows in table
sy-index = loop index
sy-tabix = Table Index
sy-subrc = Function call success / failure
sy-ucomm. = User command

Steps for Step Loop.


~~~~~~~~~~~~~~~~~~~
1. Transaction SE38 - Create a Module Pool Program
2. Declare all data common to screens and program
3. Declare Cursor Variable - used by system as
data : cur1 type i.
4. Go to Transaction se51 - Create required screens with number (10,20...)
5. Go to Flow logic Tab sheet
6. Write PBO Event
a. Module initialise.
b. loop at <itab> cursor <cur1>.
module read_from_pgm.
endloop.
7. Write PAI Event
a. module <exit> at exit-command.
b. loop at <itab>.
module send_to_pgm.
endloop.
c. module user_command.
8. Go to Layout
a. Get all the required fields from Program and dictionary.
ABAP Tutor Page 13

b. arrange it straightly and then select all the fields.


goto edit--> grouping -->steploops --> define.
c. Assign the Label for each control and name it.
d. put buttons (for user command) and give Functional code as (save,delete,,)
e. put button for Exit (i.e Functional type = Exit command)
9. write code in all respective module.
10. Save after syntax check and Activate the Module pool program.
11. Save after syntax check and activate the Screen.
12. Go to Transaction se93
13. Create a new transaction by giving
a. Module pool program name
b. Screen number
c. Save the Transaction.
14. Run the Transaction from Command Prompt.

-----------------------------------------------------

Steps for Table Control


~~~~~~~~~~~~~~~~~~~
1. Transaction SE38 - Create a Module Pool Program
2. Declare all data common to screens and program
3. Declare control for Table control as
controls <tc1> type tableview using screen <screen_no>.
4. Declare Cursor Variable - used by system as
data : cur1 type i.
5. Go to Transaction se51 - Create required screens with number (10,20...)
6. Go to Flow logic Tab sheet
7. Write PBO Event
a. Module initialise.
b. loop at <itab> with control <tc1> cursor <cur1>.
module read_from_pgm.
endloop.
8. Write PAI Event
a. module <exit> at exit-command.
b. loop at <itab>.
module send_to_pgm.
endloop.
c. module user_command.
9. Go to Layout
a. Get all the required fields from Program and dictionary.
b. put it in Table control
c. Assign the Label for each control and name it.
d. put buttons (for user command) and give Functional code as (save,delete,,)
e. put button for Exit (i.e Functional type = Exit command)
10. write code in all respective module.
11. Save after syntax check and Activate the Module pool program.
ABAP Tutor Page 14

12. Save after syntax check and activate the Screen.


13. Go to Transaction se93
14. Create a new transaction by giving
a. Module pool program name
b. Screen number
c. Save the Transaction.
15. Run the Transaction from Command Prompt.

Dialog Programming Advanced (Table Control )

Flow Logic

* PBO EVENT
PROCESS BEFORE OUTPUT.
MODULE STATUS_0010.
LOOP AT EMP WITH CONTROL TC1 CURSOR cur1.
MODULE READ_FROM_PGM.
ENDLOOP.
*PAI EVENT
PROCESS AFTER INPUT.
MODULE QUIT AT EXIT-COMMAND.
LOOP AT EMP.
MODULE SEND_TO_PGM.
ENDLOOP.
MODULE USER_COMMAND_0010.
ABAP Tutor Page 15

Module pool Program

PROGRAM ZANAND7 .

data: begin of emp occurs 0,


empno type zemp99-empno,
empname type zemp99-empname,
deptno type zemp99-deptno,
salary type zemp99-salary,
end of emp.

controls tc1 type tableview using screen 10.

data : cur1 type i.

*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0010 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module USER_COMMAND_0010 input.
CASE SY-UCOMM.
WHEN 'READDB'.
select * from zemp99 into table emp.
WHEN 'SAVE'.
modify zemp99 from table emp.
ENDCASE.

endmodule. " USER_COMMAND_0010 INPUT


*&---------------------------------------------------------------------*
*& Module QUIT INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module QUIT input.
LEAVE TO SCREEN 0.
endmodule. " QUIT INPUT

*&---------------------------------------------------------------------*
*& Module SEND_TO_PGM INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module SEND_TO_PGM input.
data : wa like emp.
wa = emp.
read table emp with key empno = emp-empno.
modify emp from wa index sy-tabix.

endmodule. " SEND_TO_PGM INPUT


*&---------------------------------------------------------------------*
*& Module READ_FROM_PGM OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module READ_FROM_PGM output.

endmodule. " READ_FROM_PGM OUTPUT


*&---------------------------------------------------------------------*
*& Module STATUS_0010 OUTPUT
ABAP Tutor Page 16

*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module STATUS_0010 output.
data : v_lines type i.
describe table emp lines v_lines.
tc1-lines = v_lines.
endmodule. " STATUS_0010 OUTPUT

Dialog Programming Advanced (Step Control )

Flowlogic
* PBO Event
PROCESS BEFORE OUTPUT.
MODULE STATUS_0010.

loop at emp1 cursor cur1.


module read_frm_pgm.
endloop.

* PAI Event
PROCESS AFTER INPUT.
module exit at exit-command.
loop at emp1.
module send_to_pgm.
endloop.
MODULE USER_COMMAND_0010.
ABAP Tutor Page 17

Module pool Program

PROGRAM ZANAND8 .

data: begin of emp1 occurs 0,


empno type zemp99-empno,
empname type zemp99-empname,
deptno type zemp99-deptno,
salary type zemp99-salary,
end of emp1.

data : cur1 type i.


*&---------------------------------------------------------------------*
*& Module exit INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module exit input.
leave to screen 0.
endmodule. " exit INPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0010 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module USER_COMMAND_0010 input.
case sy-ucomm.
when 'READ'.
select * from zemp99 into table emp1.
when 'SAVE'.
modify zemp99 from table emp1.
endcase.
endmodule. " USER_COMMAND_0010 INPUT
*&---------------------------------------------------------------------*
*& Module send_to_pgm INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module send_to_pgm input.
data : wa like emp1.
wa = emp1.
read table emp1 with key empno = emp1-empno.
modify emp1 from wa index sy-tabix.
endmodule. " send_to_pgm INPUT
*&---------------------------------------------------------------------*
*& Module STATUS_0010 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module STATUS_0010 output.

endmodule. " STATUS_0010 OUTPUT


*&---------------------------------------------------------------------*
*& Module read_frm_pgm OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module read_frm_pgm output.

endmodule. " read_frm_pgm OUTPUT


ABAP Tutor Page 18

BDC
Recorded Screen for transaction zmypgm using transaction shdb
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Recorded Value
~~~~~~~~~~~~~~~
ZANAND6 1000 X
0000 BDC_OKCODE =SAVE
0000 BDC_CURSOR EMP-SALARY
0000 EMP-EMPNO 589
0000 EMP-EMPNAME KUMAR NANDA
0000 EMP-DEPTNO 10
0000 EMP-SALARY 17000
ZANAND6 1000 X
0000 BDC_OKCODE /EEXIT
0000 BDC_CURSOR EMP-EMPNO
ABAP Tutor Page 19

Flat File Content


~~~~~~~~~~~~~~~
900 Anand 2 15000
902 gowri shankar 2 17000.25
903 uma 3 2500
904 Swami 3 16000.75

BDC - Call Transaction Method

Steps
1. Record the Transaction using Transaction Code SHDB
2. Enter the required fields one by one and navigate thru screens / press button.
3. Save the Recording .
4. go the Transaction SE 38
5. Declare BDC Tab and BDCMsgCOLL internal table for hoding the BDC Data
and BDC Messages respectively.
6. Declare an Equivalent structured internal table for file format (I_file)
7. Upload the File Content to the Internal Table (I_file)
ABAP Tutor Page 20

8. Loop at internal table (I_file)


a. After Populating Call the Transaction
Call transaction <zmypgm> using <I_bdctab> mode <A/E/N>
Update <A/S> messages into <I_msgtab>
b. Handle the Error Message and Error Records in separate internal table.
9. Display the Statistical Report of success records details and error records details.
Code :

************************************************************************
* Program : ZANAND9 *
* Authod : Anand *
* Created on : 22-Mar-02 *
* Description : This is simple demo program for BDC using call *
* Transaction Method. *
* Program Requirements : *
* Transaction zmypgm - Created in SE93 *
* Transaction Recording - SHDB
* Flat File as given below. *
* Function Used : *
* WS_UPLOAD - for uploading file *
* FORMAT_Message - Formatting error message *
* *
************************************************************************
*Flat file format (c:\employee.txt)
*----------------
*900 Anand 2 15000
*902 gowri shankar 2 17000.25
*903 uma 3 2500
*904 Swami AA 16000.75
*
************************************************************************
*Transaction Recording (using Transaction SHDB)
*ZANAND6 1000 X
* BDC_OKCODE =SAVE
* BDC_CURSOR EMP-SALARY
* EMP-EMPNO 589
* EMP-EMPNAME KUMAR NANDA
* EMP-DEPTNO 10
* EMP-SALARY 17000
*ZANAND6 1000 X
* BDC_OKCODE /EEXIT
* BDC_CURSOR EMP-EMPNO
************************************************************************

REPORT ZANAND9 .

* To Hold content form file


data: begin of emp occurs 0,
empno(10) ,
empname(20),
deptno(10),
salary(10),
end of emp.
* To store error regards with error message
data: begin of emp_error occurs 0,
empno(10) ,
empname(20),
deptno(10),
salary(10),
ABAP Tutor Page 21

v_msg(100),
end of emp_error.
* BDC Table and BDC message table
data : i_bdctab like bdcdata occurs 0 with header line,
i_bdcmsg like bdcmsgcoll occurs 0 with header line.

* Upload the file


Perform Read_file.

Write :/ 'List of Success Records :' color col_heading.


* Display the Sucess records
loop at emp.
perform populate_bdctab. " Populate the BDC Table
* Call the transaction
call transaction 'ZMYPGM' using i_bdctab
mode 'N'
messages into i_bdcmsg.
* Check for error
if sy-subrc <> 0.
perform handle_message. " Handle Error Condition
else.
write :/ emp. " Display Sucees record detail
endif.

refresh i_bdctab.
clear i_bdctab.
endloop.

* Display the list of failed records


write :/ 'List of Error Message :' color col_heading.
loop at emp_error.
write :/ emp_error.
clear emp_error.
endloop.

*-- User Defined SubRoutines


*&---------------------------------------------------------------------*
*& Form Read_file
*&---------------------------------------------------------------------*
* Upload file content to Internal Table
*----------------------------------------------------------------------*
form Read_file.
call function 'WS_UPLOAD'
EXPORTING
* CODEPAGE =''
FILENAME = 'c:\employee.txt '
* FILETYPE = 'ASC'
* HEADLEN =''
* LINE_EXIT =''
* TRUNCLEN =''
* USER_FORM =''
* USER_PROG =''
* DAT_D_FORMAT =''
* IMPORTING
* FILELENGTH =
tables
data_tab = emp
* EXCEPTIONS
* CONVERSION_ERROR =1
* FILE_OPEN_ERROR =2
* FILE_READ_ERROR =3
ABAP Tutor Page 22

* INVALID_TYPE =4
* NO_BATCH =5
* UNKNOWN_ERROR =6
* INVALID_TABLE_WIDTH =7
* GUI_REFUSE_FILETRANSFER =8
* CUSTOMER_ERROR =9
* OTHERS = 10
.
if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.

endform. " Read_file


*&---------------------------------------------------------------------*
*& Form populate_bdctab
*&---------------------------------------------------------------------*
* Fills the Employee Number, Name ,salary and department number
* Automatically and press Save and then exit button.
*----------------------------------------------------------------------*
form populate_bdctab.

* Transaction screen details


i_bdctab-program = 'zanand6'.
i_bdctab-dynpro = '1000'.
i_bdctab-dynbegin = 'X'.
append i_bdctab.
clear i_bdctab.
* Employee Number
i_bdctab-fnam = 'EMP-EMPNO'.
i_bdctab-fval = EMP-empno.
append i_bdctab.
clear i_bdctab.
* Employee Name
i_bdctab-fnam = 'EMP-EMPNAME'.
i_bdctab-fval = EMP-empname.
append i_bdctab.
clear i_bdctab.
* Employee Department number
i_bdctab-fnam = 'EMP-deptNO'.
i_bdctab-fval = EMP-deptno.
append i_bdctab.
clear i_bdctab.
* Employee Salary
i_bdctab-fnam = 'EMP-salary'.
i_bdctab-fval = EMP-salary.
append i_bdctab.
clear i_bdctab.
* Press Save Button
i_bdctab-fnam = 'BDC_OKCODE'.
i_bdctab-fval = '=SAVE'.
append i_bdctab.
clear i_bdctab.
* Screen Details
i_bdctab-program = 'zanand6'.
i_bdctab-dynpro = '1000'.
i_bdctab-dynbegin = 'X'.
append i_bdctab.
clear i_bdctab.
* Press Exit Button
i_bdctab-fnam = 'BDC_OKCODE'.
ABAP Tutor Page 23

i_bdctab-fval = '/EEXIT'.
append i_bdctab.
clear i_bdctab.
endform. " populate_bdctab

*&---------------------------------------------------------------------*
*& Form handle_message
*&---------------------------------------------------------------------*
* Organises the Error Message and give it in readable format
*----------------------------------------------------------------------*
form handle_message.
data : v_msg(100) type c.

call function 'FORMAT_MESSAGE'


EXPORTING
ID = SY-MSGID
* LANG = '-D'
NO = SY-MSGNO
V1 = SY-MSGV1
V2 = SY-MSGV2
V3 = SY-MSGV3
V4 = SY-MSGV4
IMPORTING
MSG = v_msg
* EXCEPTIONS
* NOT_FOUND =1
* OTHERS =2
.

* Append the Error record in error table with error message

loop at i_bdcmsg where msgtyp ='E'.


move-corresponding emp to emp_error.
concatenate 'Error in ' i_bdcmsg-fldname
v_msg into v_msg
separated by space.
move v_msg to emp_error-v_msg.

append emp_error.
clear emp_error.
clear : v_msg,i_bdcmsg.
endloop.

endform. " handle_message

BDC Session Method

Transaction Required :
SHDB Recording the Transaction.
SM35 - To Run the Program Created Session.

Steps :
1. Use Transaction SHDB to Record the Required Transaction.
2. Enter the required data and save the record.
3. Save the Recorded Messages.
4. Go to Transaction SE38 , to write BDC Program
ABAP Tutor Page 24

5. Declare the Internal table to Hold flat file Content


6. Declare the BDCData internal table to store BDC Related information
7. Call the function BDC_OPEN_GROUP
a. Pass Group = <Ses_name>
b. Client = sy-mant
c. User = sy-uname
d. Holddate = <old date>
e. Keep = X
8. Loop the Internal Table (created from file content)
a. Populate the BDCData internal table
b. Call the function BDC_Insert
i. Pass Tcode = <Transaction Name>
ii. Dynprotable = <bdctable>
9. Call the function BDC_Close_group
10. Go to the Transaction SM35 and Run the session.

Code :
************************************************************************
* Program : ZANAND10 *
* Authod : Anand *
* Created on : 22-Mar-02 *
* Description : This is simple demo program for BDC using Session *
* Method. *
* Program Requirements : *
* Transaction zmypgm - Created in SE93 *
* Transaction Recording - SHDB *
* To Run Created Session - SM35
* Flat File as given below. *
* Function Used : *
* WS_UPLOAD - for uploading file *
* FORMAT_Message - Formatting error message *
* *
************************************************************************
*Flat file format (c:\employee.txt)
*----------------
*900 Anand 2 15000
*902 gowri shankar 2 17000.25
*903 uma 3 2500
*904 Swami AA 16000.75
*
************************************************************************
*Transaction Recording (using Transaction SHDB)
*ZANAND6 1000 X
* BDC_OKCODE =SAVE
* BDC_CURSOR EMP-SALARY
* EMP-EMPNO 589
* EMP-EMPNAME KUMAR NANDA
* EMP-DEPTNO 10
* EMP-SALARY 17000
*ZANAND6 1000 X
* BDC_OKCODE /EEXIT
* BDC_CURSOR EMP-EMPNO
************************************************************************

REPORT ZANAND9 .
ABAP Tutor Page 25

* To Hold content form file


data: begin of emp occurs 0,
empno(10) ,
empname(20),
deptno(10),
salary(10),
end of emp.
* BDC Table
data : i_bdctab like bdcdata occurs 0 with header line.

* Upload the file


Perform Read_file.
Perform Open_BDC.

* Display the Employee List


loop at emp.
Write :/ emp.
perform populate_bdctab. " Populate the BDC Table
* Call the Session Insert
perform Insert_bdc.
refresh i_bdctab.
clear i_bdctab.
endloop.

Perform Close_BDC.
*-- User Defined SubRoutines
*&---------------------------------------------------------------------*
*& Form Read_file
*&---------------------------------------------------------------------*
* Upload file content to Internal Table
*----------------------------------------------------------------------*
form Read_file.
call function 'WS_UPLOAD'
EXPORTING
* CODEPAGE =''
FILENAME = 'c:\employee.txt '
* FILETYPE = 'ASC'
* HEADLEN =''
* LINE_EXIT =''
* TRUNCLEN =''
* USER_FORM =''
* USER_PROG =''
* DAT_D_FORMAT =''
* IMPORTING
* FILELENGTH =
tables
data_tab = emp
* EXCEPTIONS
* CONVERSION_ERROR =1
* FILE_OPEN_ERROR =2
* FILE_READ_ERROR =3
* INVALID_TYPE =4
* NO_BATCH =5
* UNKNOWN_ERROR =6
* INVALID_TABLE_WIDTH =7
* GUI_REFUSE_FILETRANSFER =8
* CUSTOMER_ERROR =9
* OTHERS = 10
.
if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
ABAP Tutor Page 26

* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.


endif.

endform. " Read_file


*&---------------------------------------------------------------------*
*& Form populate_bdctab
*&---------------------------------------------------------------------*
* Fills the Employee Number, Name ,salary and department number
* Automatically and press Save and then exit button.
*----------------------------------------------------------------------*
form populate_bdctab.

* Transaction screen details


i_bdctab-program = 'zanand6'.
i_bdctab-dynpro = '1000'.
i_bdctab-dynbegin = 'X'.
append i_bdctab.
clear i_bdctab.
* Employee Number
i_bdctab-fnam = 'EMP-EMPNO'.
i_bdctab-fval = EMP-empno.
append i_bdctab.
clear i_bdctab.
* Employee Name
i_bdctab-fnam = 'EMP-EMPNAME'.
i_bdctab-fval = EMP-empname.
append i_bdctab.
clear i_bdctab.
* Employee Department number
i_bdctab-fnam = 'EMP-deptNO'.
i_bdctab-fval = EMP-deptno.
append i_bdctab.
clear i_bdctab.
* Employee Salary
i_bdctab-fnam = 'EMP-salary'.
i_bdctab-fval = EMP-salary.
append i_bdctab.
clear i_bdctab.
* Press Save Button
i_bdctab-fnam = 'BDC_OKCODE'.
i_bdctab-fval = '=SAVE'.
append i_bdctab.
clear i_bdctab.
* Screen Details
i_bdctab-program = 'zanand6'.
i_bdctab-dynpro = '1000'.
i_bdctab-dynbegin = 'X'.
append i_bdctab.
clear i_bdctab.
* Press Exit Button
i_bdctab-fnam = 'BDC_OKCODE'.
i_bdctab-fval = '/EEXIT'.
append i_bdctab.
clear i_bdctab.
endform. " populate_bdctab

*&---------------------------------------------------------------------*
*& Form Open_BDC
*&---------------------------------------------------------------------*
* Establish a New Session
*----------------------------------------------------------------------*
ABAP Tutor Page 27

form Open_BDC.
data : v_date type d.
v_date = sy-datum - 1.
call function 'BDC_OPEN_GROUP'
EXPORTING
CLIENT = SY-MANDT
* DEST = FILLER8
GROUP = 'Emp'
HOLDDATE = v_date
KEEP = 'X'
USER = sy-uname
* RECORD = FILLER1
* IMPORTING
* QID =
* EXCEPTIONS
* CLIENT_INVALID =1
* DESTINATION_INVALID =2
* GROUP_INVALID =3
* GROUP_IS_LOCKED =4
* HOLDDATE_INVALID =5
* INTERNAL_ERROR =6
* QUEUE_ERROR =7
* RUNNING =8
* SYSTEM_LOCK_ERROR =9
* USER_INVALID = 10
* OTHERS = 11
.
endform. " Open_BDC

*&---------------------------------------------------------------------*
*& Form Close_BDC
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form Close_BDC.
call function 'BDC_CLOSE_GROUP'
* EXCEPTIONS
* NOT_OPEN =1
* QUEUE_ERROR =2
* OTHERS =3
.
endform. " Close_BDC
*&---------------------------------------------------------------------*
*& Form Insert_bdc
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form Insert_bdc.
call function 'BDC_INSERT'
EXPORTING
TCODE = 'zmypgm'
* POST_LOCAL = NOVBLOCAL
* PRINTING = NOPRINT
tables
dynprotab = i_bdctab
* EXCEPTIONS
* INTERNAL_ERROR =1
* NOT_OPEN =2
* QUEUE_ERROR =3
* TCODE_INVALID =4
* PRINTING_INVALID =5
ABAP Tutor Page 28

* POSTING_INVALID =6
* OTHERS =7
.
endform. " Insert_bdc

Vous aimerez peut-être aussi