Post on 26-Oct-2014
description
transcript
BDC-CALL TRASACTION
Recording: it is a SAP provided option to record the screen and field details of an application (Ex:XD01,XK01, VA01,ME21N,VF01,…………) while it is executing(Running).
This is used to write the code to automate the data upload into SAP
SHDB is the T.code to work with recording
Go to SHDB
Click on New recording icon
Provide the Recording name, Transaction code(Ex:XD01) for which recording is required
Click on ‘Start recording ‘ Tab
Enter the data in fields required in first screen
Click on ENTER
Provide the data in required fields in second screen
Click on SAVE--------Enter-----Enter
Now click on SAVE to save the above recording if required
1) Call transaction MethodGo to se38
Click on Create and provide the description and type
Click on ENTER and click on Local Object Icon
Now write the program
REPORT zbdc_call_demo.
DATA:BEGIN OF wa_kna1, kunnr LIKE kna1-kunnr, ktokd LIKE kna1-ktokd, name1 LIKE kna1-name1, sortl LIKE kna1-sortl, ort01 LIKE kna1-ort01, pstlz LIKE kna1-pstlz, land1 LIKE kna1-land1, spras LIKE kna1-spras, END OF wa_kna1,
it_kna1 LIKE TABLE OF wa_kna1.
*To hold the screen and field details of a customerDATA:wa_bdcdata TYPE bdcdata, it_bdcdata LIKE TABLE OF wa_bdcdata.
*To read the data from text file to presentations layerCALL FUNCTION 'GUI_UPLOAD' EXPORTING filename = 'C:\customer.txt'* FILETYPE = 'ASC' has_field_separator = 'X'* HEADER_LENGTH = 0* IMPORTING* FILELENGTH = FILELENGTH* HEADER = HEADER TABLES data_tab = it_kna1* EXCEPTIONS.
*To process the dataIF NOT it_kna1 IS INITIAL.
LOOP AT it_kna1 INTO wa_kna1.*To get the screen & field detaisl of each customer*First screen details wa_bdcdata-program = 'SAPMF02D'. wa_bdcdata-dynpro = '0100'. wa_bdcdata-dynbegin = 'X'. APPEND wa_bdcdata TO it_bdcdata. CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'BDC_CURSOR'. wa_bdcdata-fval = 'RF02D-KTOKD'. APPEND wa_bdcdata TO it_bdcdata.
CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'BDC_OKCODE'. wa_bdcdata-fval = '/00'. APPEND wa_bdcdata TO it_bdcdata. CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'RF02D-KUNNR'. wa_bdcdata-fval = wa_kna1-kunnr. APPEND wa_bdcdata TO it_bdcdata. CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'RF02D-KTOKD'. wa_bdcdata-fval = wa_kna1-ktokd . APPEND wa_bdcdata TO it_bdcdata. CLEAR wa_bdcdata.
* *2nd screen details wa_bdcdata-program = 'SAPMF02D'. wa_bdcdata-dynpro = '0110'. wa_bdcdata-dynbegin = 'X'. APPEND wa_bdcdata TO it_bdcdata. CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'BDC_CURSOR'. wa_bdcdata-fval = 'KNA1-PSTLZ'. APPEND wa_bdcdata TO it_bdcdata. CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'BDC_OKCODE'. wa_bdcdata-fval = '=UPDA'. APPEND wa_bdcdata TO it_bdcdata. CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'KNA1-NAME1'. wa_bdcdata-fval = wa_kna1-name1. APPEND wa_bdcdata TO it_bdcdata. CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'KNA1-SORTL'. wa_bdcdata-fval = wa_kna1-sortl. APPEND wa_bdcdata TO it_bdcdata. CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'KNA1-ORT01'. wa_bdcdata-fval = wa_kna1-ort01. APPEND wa_bdcdata TO it_bdcdata. CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'KNA1-PSTLZ'. wa_bdcdata-fval = wa_kna1-pstlz. APPEND wa_bdcdata TO it_bdcdata. CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'KNA1-LAND1'. wa_bdcdata-fval = wa_kna1-land1. APPEND wa_bdcdata TO it_bdcdata. CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'KNA1-SPRAS'. wa_bdcdata-fval = wa_kna1-spras. APPEND wa_bdcdata TO it_bdcdata. CLEAR wa_bdcdata.
*to run the Application(XD01) CALL TRANSACTION 'XD01' USING it_bdcdata MODE 'A'. REFRESH it_bdcdata.
ENDLOOP.ELSE. MESSAGE 'No data found to upload' TYPE 'I'.ENDIF.
SAVE----CHECK-----ACTIVATE
To test the program
Create the text file(customer) data with the fields in wa_kna1 with Tab space and place in C drive
Example 2Go to SE38
Now create the program as below
report zbdc_call_demo.
*To hold the data received from local drivedata:begin of wa_cust, kunnr type kunnr, "customer number ktokd type ktokd, "accountgroup name1 type name1, "name sortl type sortl, "search term stras type stras, "STREET land1 type land1, "country spras type spras, "language end of wa_cust,
it_cust like table of wa_cust.
*To hold the screen and field details of the transaction data: wa_bdcdata type bdcdata, it_bdcdata like table of wa_bdcdata.
*To upload the data from local drive
call function 'GUI_UPLOAD' exporting filename = 'C:\TEST.txt'* FILETYPE = 'ASC' has_field_separator = 'X'* HEADER_LENGTH = 0* READ_BY_LINE = 'X'* DAT_MODE = ' '* CODEPAGE = ' '* IGNORE_CERR = ABAP_TRUE* REPLACEMENT = '#'* CHECK_BOM = ' '* VIRUS_SCAN_PROFILE = VIRUS_SCAN_PROFILE* NO_AUTH_CHECK = ' '* IMPORTING* FILELENGTH = FILELENGTH* HEADER = HEADER tables
data_tab = it_cust* EXCEPTIONS* FILE_OPEN_ERROR = 1* FILE_READ_ERROR = 2* NO_BATCH = 3* GUI_REFUSE_FILETRANSFER = 4* INVALID_TYPE = 5* NO_AUTHORITY = 6* UNKNOWN_ERROR = 7* BAD_DATA_FORMAT = 8* HEADER_NOT_ALLOWED = 9* SEPARATOR_NOT_ALLOWED = 10* HEADER_TOO_LONG = 11* UNKNOWN_DP_ERROR = 12* ACCESS_DENIED = 13* DP_OUT_OF_MEMORY = 14* DISK_FULL = 15* DP_TIMEOUT = 16* OTHERS = 17 .if sy-subrc <> 0.* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.endif.
if not it_cust is initial. delete it_cust index 1.*to process the dataloop at it_cust into wa_cust.
*To get the sceen and field details of the transaction* 1st screen details
wa_bdcdata-program = 'SAPMF02D'. wa_bdcdata-dynpro = '0100'. wa_bdcdata-dynbegin = 'X'. wa_bdcdata-fnam = ''. wa_bdcdata-fval = ''. append wa_bdcdata to it_bdcdata. clear wa_bdcdata.
wa_bdcdata-program = ''. wa_bdcdata-dynpro = ''. wa_bdcdata-dynbegin = ''. wa_bdcdata-fnam = 'BDC_CURSOR'. wa_bdcdata-fval = 'RF02D-KTOKD'. append wa_bdcdata to it_bdcdata. clear wa_bdcdata.
wa_bdcdata-program = ''. wa_bdcdata-dynpro = ''. wa_bdcdata-dynbegin = ''. wa_bdcdata-fnam = 'BDC_OKCODE'. wa_bdcdata-fval = '/00'. append wa_bdcdata to it_bdcdata. clear wa_bdcdata.
wa_bdcdata-program = ''. wa_bdcdata-dynpro = ''. wa_bdcdata-dynbegin = ''. wa_bdcdata-fnam = 'RF02D-KUNNR'. wa_bdcdata-fval = wa_cust-kunnr. append wa_bdcdata to it_bdcdata. clear wa_bdcdata.
wa_bdcdata-program = ''. wa_bdcdata-dynpro = ''. wa_bdcdata-dynbegin = ''. wa_bdcdata-fnam = 'RF02D-KTOKD'. wa_bdcdata-fval = wa_cust-ktokd. append wa_bdcdata to it_bdcdata. clear wa_bdcdata.
*2nd screen detailswa_bdcdata-program = 'SAPMF02D'. wa_bdcdata-dynpro = '0110'. wa_bdcdata-dynbegin = 'X'. wa_bdcdata-fnam = ''. wa_bdcdata-fval = ''. append wa_bdcdata to it_bdcdata. clear wa_bdcdata.
wa_bdcdata-program = ''. wa_bdcdata-dynpro = ''. wa_bdcdata-dynbegin = ''. wa_bdcdata-fnam = 'BDC_CURSOR'. wa_bdcdata-fval = 'KNA1-SORTL'. append wa_bdcdata to it_bdcdata. clear wa_bdcdata.
wa_bdcdata-program = ''. wa_bdcdata-dynpro = ''. wa_bdcdata-dynbegin = ''. wa_bdcdata-fnam = 'BDC_OKCODE'. wa_bdcdata-fval = '=UPDA'. append wa_bdcdata to it_bdcdata. clear wa_bdcdata.
wa_bdcdata-program = ''. wa_bdcdata-dynpro = ''. wa_bdcdata-dynbegin = ''. wa_bdcdata-fnam = 'KNA1-NAME1'. wa_bdcdata-fval = wa_cust-name1. append wa_bdcdata to it_bdcdata. clear wa_bdcdata.
wa_bdcdata-program = ''. wa_bdcdata-dynpro = ''. wa_bdcdata-dynbegin = ''. wa_bdcdata-fnam = 'KNA1-SORTL'. wa_bdcdata-fval = wa_cust-sortl. append wa_bdcdata to it_bdcdata. clear wa_bdcdata.
wa_bdcdata-program = ''. wa_bdcdata-dynpro = ''. wa_bdcdata-dynbegin = ''. wa_bdcdata-fnam = 'KNA1-STRAS'. wa_bdcdata-fval = wa_cust-stras. append wa_bdcdata to it_bdcdata. clear wa_bdcdata.
wa_bdcdata-program = ''. wa_bdcdata-dynpro = ''.
wa_bdcdata-dynbegin = ''. wa_bdcdata-fnam = 'KNA1-LAND1'. wa_bdcdata-fval = wa_cust-land1. append wa_bdcdata to it_bdcdata. clear wa_bdcdata.
wa_bdcdata-program = ''. wa_bdcdata-dynpro = ''. wa_bdcdata-dynbegin = ''. wa_bdcdata-fnam = 'KNA1-SPRAS'. wa_bdcdata-fval = wa_cust-spras. append wa_bdcdata to it_bdcdata. clear wa_bdcdata.
*TO call the required transaction XD01
call transaction 'XD01' using it_bdcdata mode 'A'. "(A:All screens(foreground),N:no screens(Back ground),E:error screens) refresh it_bdcdata. endloop. else. message 'No data found from the file' type 'I'. endif.
Example 3Go to SE38
Now create the program as below
REPORT zbdc_call_demo2.TYPE-POOLS truxs.
*To hold the data received from local driveDATA:BEGIN OF wa_cust, kunnr TYPE kunnr, "customer number ktokd TYPE ktokd, "accountgroup name1 TYPE name1, "name sortl TYPE sortl, "search term stras TYPE stras, "STREET land1 TYPE land1, "country spras TYPE spras, "language END OF wa_cust, it_cust LIKE TABLE OF wa_cust.*To hold the screen and field details of the transactionDATA: wa_bdcdata TYPE bdcdata, it_bdcdata LIKE TABLE OF wa_bdcdata.DATA v_notepad TYPE truxs_t_text_data.PARAMETERS p_file TYPE rlgrap-filename.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.*To browse the file CALL FUNCTION 'F4_FILENAME' EXPORTING program_name = syst-cprog dynpro_number = syst-dynnr field_name = ' ' IMPORTING file_name = p_file. IF NOT p_file IS INITIAL.*To convert the data from excel sheet to int table CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP' EXPORTING* I_FIELD_SEPERATOR = I_FIELD_SEPERATOR* I_LINE_HEADER = I_LINE_HEADER i_tab_raw_data = v_notepad i_filename = p_file TABLES i_tab_converted_data = it_cust* EXCEPTIONS* CONVERSION_FAILED = 1* OTHERS = 2 . IF sy-subrc <> 0.* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF.
ELSE. MESSAGE 'File was not found' TYPE 'E'. ENDIF.
IF NOT it_cust IS INITIAL. DELETE it_cust INDEX 1.*to process the data LOOP AT it_cust INTO wa_cust.*to add the leading zerosCALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' EXPORTING input = wa_cust-ktokd IMPORTING OUTPUT = wa_cust-ktokd .
*To get the sceen and field details of the transaction* 1st screen details
PERFORM fill_bdcdata USING 'SAPMF02D' '0100' 'X' '' ''. PERFORM fill_bdcdata USING: '' '' '' 'BDC_CURSOR' 'RF02D-KTOKD', '' '' '' 'BDC_OKCODE' '/00', '' '' '' 'RF02D-KUNNR' wa_cust-kunnr, '' '' '' 'RF02D-KTOKD' wa_cust-ktokd,*2nd screen details 'SAPMF02D' '0110' 'X' '' '', '' '' '' 'BDC_CURSOR' 'KNA1-SORTL',
'' '' '' 'BDC_OKCODE' '=UPDA', '' '' '' 'KNA1-NAME1' wa_cust-name1, '' '' '' 'KNA1-SORTL' wa_cust-sortl, '' '' '' 'KNA1-STRAS' wa_cust-stras, '' '' '' 'KNA1-LAND1' wa_cust-land1, '' '' '' 'KNA1-SPRAS' wa_cust-spras.
*TO call the required transaction XD01
CALL TRANSACTION 'XD01' USING it_bdcdata MODE 'N'.
REFRESH it_bdcdata. ENDLOOP. ELSE. MESSAGE 'No data found from the file' TYPE 'I'. ENDIF.*&---------------------------------------------------------------------**& Form fill_bdcdata*&---------------------------------------------------------------------** text To fill the bdcdata int table*----------------------------------------------------------------------** -->P_0135 text* -->P_0136 text* -->P_0137 text* -->P_0138 text* -->P_0139 text*----------------------------------------------------------------------*FORM fill_bdcdata USING value(p_prog) value(p_dynpro) value(p_dynbgn) value(p_fnam) value(p_fval).
wa_bdcdata-program = p_prog. wa_bdcdata-dynpro = p_dynpro. wa_bdcdata-dynbegin = p_dynbgn. wa_bdcdata-fnam = p_fnam. wa_bdcdata-fval = p_fval. APPEND wa_bdcdata TO it_bdcdata. CLEAR wa_bdcdata.
ENDFORM. " fill_bdcdata
Features of a CALL TRANSACTION method
1) Synchronous processing. The system performs a database commit immediately before and after the CALL TRANSACTION USING statement.
2) Updating the database can be either synchronous or asynchronous. The program specifies the update type.
3) Transfer data for a single transaction.4) Transfers data for a sequence of dialog screens.5) No batch input processing log is generated.6) Error handling is not possible
7) asynchronous processing8) can transfer small amount of data9) processing is faster.10) errors need to be handled explicitly11) data is updated automatically
SESSION METHOD
1) Data is not updated in the database table until the session is processed.
2) No sy-subrc is returned.
3) Error log is created for error records.
4) Updation is always synchronous.
5) Session method supports both small amount of data as well as large amount of data
6) Data processing is asynchronous and data updation is synchronous.
7) It processes multiple applications while performing validations.
8) In session method data will be updated in data base only after processing session only.
9) System provide by default logfile for handling error records.
10) It supports both foreground as well as background process
In session method, there are 3 function modules are used
1) BDC_OPEN_GROUP : this is used to open the session
2) BDC_INSERT: this is used to insert the data
3) BDC_CLOSE_GROUP: this is used to close the session
Steps to work with SESSION METHOD
1) Define the required work areas and int tables
2) Upload the data using GUI_UPLOAD or CONVERT_TEXT_XLS_TO_SAP or any other
3) open the session using BDC_OPEN_GROUP
4) Loop
5) prepare the bdcdata int table data
6) Insert the data using BDC_INSERT
7) ENDLOOP
8) Close the session using BDC_CLOSE_GROUP
9) Now go to SM35 to process the session(Fore ground, Display with errors or Back ground)
Example 1
Go to SE38
Now create the program as below
report zbdc_session_demo.
type-pools truxs.
*To hold the data received from local drive
data:begin of wa_cust,
kunnr type kunnr, "customer number
ktokd type ktokd, "accountgroup
name1 type name1, "name
sortl type sortl, "search term
stras type stras, "STREET
land1 type land1, "country
spras type spras, "language
end of wa_cust,
it_cust like table of wa_cust.
*To hold the screen and field details of the transaction
data: wa_bdcdata type bdcdata,
it_bdcdata like table of wa_bdcdata.
data v_notepad type truxs_t_text_data.
parameters p_file type rlgrap-filename.
at selection-screen on value-request for p_file.
*To browse the file
call function 'F4_FILENAME'
exporting
program_name = syst-cprog
dynpro_number = syst-dynnr
field_name = ' '
importing
file_name = p_file.
*start of selection
start-of-selection.
if not p_file is initial.
*To convert the data from excel sheet to int table
call function 'TEXT_CONVERT_XLS_TO_SAP'
exporting
* I_FIELD_SEPERATOR = I_FIELD_SEPERATOR
* I_LINE_HEADER = I_LINE_HEADER
i_tab_raw_data = v_notepad
i_filename = p_file
tables
i_tab_converted_data = it_cust
* EXCEPTIONS
* CONVERSION_FAILED = 1
* OTHERS = 2
.
else.
message 'File was not found' type 'E'.
endif.
*END OF SELECTION EVENT
end-of-selection.
*to open the session
call function 'BDC_OPEN_GROUP'
exporting
* CLIENT = SY-MANDT
* DEST = FILLER8
group = 'DEMO'
* HOLDDATE = FILLER8
keep = 'X'
user = sy-uname
* RECORD = FILLER1
prog = sy-cprog
* DCPFM = '%'
* DATFM = '%'
* IMPORTING
* QID = 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
.
if not it_cust is initial.
delete it_cust index 1.
*to process the data
loop at it_cust into wa_cust.
*to add the leading zeros
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = wa_cust-ktokd
importing
output = wa_cust-ktokd
.
*To get the sceen and field details of the transaction
* 1st screen details
perform fill_bdcdata using 'SAPMF02D' '0100' 'X' '' ''.
perform fill_bdcdata using: '' '' '' 'BDC_CURSOR' 'RF02D-KTOKD',
'' '' '' 'BDC_OKCODE' '/00',
'' '' '' 'RF02D-KUNNR' wa_cust-kunnr,
'' '' '' 'RF02D-KTOKD' wa_cust-ktokd,
*2nd screen details
'SAPMF02D' '0110' 'X' '' '',
'' '' '' 'BDC_CURSOR' 'KNA1-SORTL',
'' '' '' 'BDC_OKCODE' '=UPDA',
'' '' '' 'KNA1-NAME1' wa_cust-name1,
'' '' '' 'KNA1-SORTL' wa_cust-sortl,
'' '' '' 'KNA1-STRAS' wa_cust-stras,
'' '' '' 'KNA1-LAND1' wa_cust-land1,
'' '' '' 'KNA1-SPRAS' wa_cust-spras.
*To create the session
call function 'BDC_INSERT'
exporting
tcode = 'XD01'
* POST_LOCAL = NOVBLOCAL
* PRINTING = NOPRINT
* SIMUBATCH = ' '
* CTUPARAMS = ' '
tables
dynprotab = it_bdcdata
* EXCEPTIONS
* INTERNAL_ERROR = 1
* NOT_OPEN = 2
* QUEUE_ERROR = 3
* TCODE_INVALID = 4
* PRINTING_INVALID = 5
* POSTING_INVALID = 6
* OTHERS = 7
.
refresh it_bdcdata.
endloop.
*TO CLOSE THE SESSION
call function 'BDC_CLOSE_GROUP'
* EXCEPTIONS
* NOT_OPEN = 1
* QUEUE_ERROR = 2
* OTHERS = 3
.
else.
message 'No data found from the file' type 'I'.
endif.
*&---------------------------------------------------------------------*
*& Form fill_bdcdata
*&---------------------------------------------------------------------*
* text To fill the bdcdata int table
*----------------------------------------------------------------------*
* -->P_0135 text
* -->P_0136 text
* -->P_0137 text
* -->P_0138 text
* -->P_0139 text
*----------------------------------------------------------------------*
form fill_bdcdata using value(p_prog)
value(p_dynpro)
value(p_dynbgn)
value(p_fnam)
value(p_fval).
wa_bdcdata-program = p_prog.
wa_bdcdata-dynpro = p_dynpro.
wa_bdcdata-dynbegin = p_dynbgn.
wa_bdcdata-fnam = p_fnam.
wa_bdcdata-fval = p_fval.
append wa_bdcdata to it_bdcdata.
clear wa_bdcdata.
endform. " fill_bdcdata
Testing
Execute the program
Browse the test file and execute the program
Now go SM35 where the session ‘Demo’
Provide the data and click on Enter---Created session will be displayed
Now Select the session and Click on ‘Process’ tab
Now select the back ground radio button and click on Process(Enter)
( Note: foreground--A, Display errors only--E Back ground---N )
If the session is processed successfully, then it will display like below
We can also observe this in Processed Tab
Now to analyze the session
Click on Log tab
Select the required session and click on Analyze session Tab
Note: If any errors in the file then it show as Incorrect at Status instead of Processed
Now click on ‘Log created on’ tab to observe the successfully processed records and Error records (if any).
Note: After processing the session successfully,the session will be deleted from SM35
To keep the session in SM35, options KEEP and HOLD DATE in bdc_open_group FM are used
KEEP: This is used to keep the session and we need to pass X to this
HOLD DATE: this is used to pred define the date to keep the session
There are 2 methods to automate the session process
1) RSBDCSUB is program to process all the sessions automatically
Go to sa38----provide the prog rsbdcsub
Provide the required details
2) using T.code SM36
Provide all the required details and SAVE