08-29-2007 2:13 PM
Hi Experts,
My requirement is like.
I have to create a session thru BDC_OPEN_GROUP.
i have written a program. It is creating a session. but the thing is..when this session is executed in sm35 it shud execute the code which is after bdc_open_group.
but in my case its creating session and after that executind that code too.
so my requirment is only to create session and when that session get executed it shud run that code.
please help me out ..its really urgent.
regards
azad,
08-29-2007 2:18 PM
1st Step - get the data from file to internal table
2nd step is - use bdc_open_group.
3rd step is use BDC_INSERT Function module
4th step is BDC_CLOSE_GROUP.
See the example program :
report zmppc015 no standard page heading
line-size 120
line-count 55
message-id zz.
Constants
constants : c_x type c value 'X'," Dynbegin
c_tcode type tstc-tcode value 'MD61'." Transaction Code
Variables
data : v_lines_in_xcel like sy-tabix,
l_tabix like sy-tabix,
v_trans_in_ssn type i,
v_ssnnr(4) type n," Counter
v_ssnname like apqi-groupid,
v_matnr(18) type c. " Material Number
Internal Tables
Internal table for file
data : begin of t_file occurs 0,
matnr(18) type c, " Material Number
berid(10) type c, " MRP Area
PLNMG01(17) type n, " Forecast Month -01
PLNMG02(17) type n, " Forecast Month -02
PLNMG03(17) type n, " Forecast Month -03
PLNMG04(17) type n, " Forecast Month -04
PLNMG05(17) type n, " Forecast Month -05
PLNMG06(17) type n, " Forecast Month -06
PLNMG07(17) type n, " Forecast Month -07
PLNMG08(17) type n, " Forecast Month -08
PLNMG09(17) type n, " Forecast Month -09
PLNMG10(17) type n, " Forecast Month -10
PLNMG11(17) type n, " Forecast Month -11
PLNMG12(17) type n, " Forecast Month -12
WERKS(4) TYPE C, " Plant
end of t_file.
Internal table for BDCDATA Structure
data : begin of itab_bdc_tab occurs 0.
include structure bdcdata.
data : end of itab_bdc_tab.
Selection-screen
selection-screen: skip 3.
selection-screen: begin of block id1 with frame.
*
parameters: p_name like rlgrap-filename
default 'C:\My Documents\InputFile.txt'
obligatory,
bdc session name prefix
p_bdcpfx(6) default 'ZPIRCT'
obligatory,
number for transction per BDC session
p_trnssn type i
default 2000 obligatory,
retain the BDC session after successfull execution
p_keep like apqi-qerase
default c_x,
user who will be executing BDC session
p_uname like apqi-userid
default sy-uname
obligatory.
selection-screen : skip 1.
Requirement type
parameters : p_bedae like t459u-bedae,
From Date
p_date like sy-datum default sy-datum obligatory.
selection-screen: end of block id1.
at selection-screen on value-request for p_name.
F4 value for Input file
perform filename_get.
main processing
start-of-selection.
To get the data from file to Internal table
perform getdata_fromfile.
loop at t_file.
hang on to xcel line num
l_tabix = sy-tabix.
if num-of-trnas-in-session = 0, create new BDC session
if v_trans_in_ssn is initial.
perform bdc_session_open.
endif.
begin new bdc script for rtg create trans
fill in bdc-data for prod.version maintenance screens
perform bdc_build_script.
insert the bdc script as a BDC transaction
perform bdc_submit_transaction.
keep track of how many BDC transactions were inserted in the BDC
session
add 1 to v_trans_in_ssn.
if the user-specified num of trans in BDC session is reached OR
if end of input file is reached, close the BDC session
if v_trans_in_ssn = p_trnssn or
l_tabix = v_lines_in_xcel.
perform bdc_session_close.
clear v_trans_in_ssn.
endif.
clear t_file.
endloop.
top-of-page.
call function 'Z_HEADER'
EXPORTING
FLEX_TEXT1 =
FLEX_TEXT2 =
FLEX_TEXT3 =
.
&----
*& Form filename_get
&----
F4 Value for input file
----
FORM filename_get.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
DEF_PATH = 'C:\Temp\ '
MASK = ',.,..'
MODE = 'O'
TITLE = 'Select File '(007)
IMPORTING
FILENAME = p_name
EXCEPTIONS
INV_WINSYS = 1
NO_BATCH = 2
SELECTION_CANCEL = 3
SELECTION_ERROR = 4
OTHERS = 5.
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. " filename_get
&----
*& Form getdata_fromfile
&----
Upload the data from file to Internal table
----
FORM getdata_fromfile.
CALL FUNCTION 'WS_UPLOAD'
EXPORTING
FILENAME = p_name
FILETYPE = 'DAT'
TABLES
DATA_TAB = t_file
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 eq 0.
sort t_file by matnr .
delete t_file where matnr = ''.
clear v_lines_in_xcel.
describe table t_file lines v_lines_in_xcel.
if v_lines_in_xcel is initial.
write: / 'No data in input file'.
stop.
endif.
else.
write:/ 'Error reading input file'.
stop.
endif.
ENDFORM. " getdata_fromfile
&----
*& Form bdc_session_open
&----
BDC_OPEN_GROUP
----
FORM bdc_session_open.
create bdc session name = prefix-from-selectn-screen + nnnn
add 1 to v_ssnnr.
concatenate p_bdcpfx v_ssnnr into v_ssnname.
CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
CLIENT = SY-MANDT
GROUP = v_ssnname
KEEP = p_keep
USER = p_uname
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 SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM. " bdc_session_open
&----
*& Form bdc_build_script
&----
BDC Script
----
FORM bdc_build_script.
Local Variables
DATA : l_frdat(8) type c, " From Date
l_todat(8) type c, " To Date
l_frdat1(4) type c, " Year
l_frdat2(2) type c, " Month
l_frdat3(2) type c, " Day
l_tdate like sy-datum, " Subtract date(1)
l_todat1(4) type c, " Year
l_todat2(2) type c, " Month
l_todat3(3) type c. " Day
Get the material number from tables ZMSMI_FERR_RAW,
ZMSMI_SNAP_RAW and ZMSMI_SIMP_RAW
perform get_matnr.
Screen 0100.
perform bdc_screen using 'SAPMM60X' '0100'.
perform bdc_field using 'BDC_OKCODE' '/EBDPT'.
perform bdc_field using 'AM60X-MATAW' 'X'.
perform bdc_field using 'AM60X-MATNR' T_FILE-MATNR.
perform bdc_field using 'AM60X-PRGRP' SPACE.
perform bdc_field using 'AM60X-PBDNR' SPACE.
perform bdc_field using 'RM60X-BERID' T_FILE-BERID.
perform bdc_field using 'AM60X-WERKS' SPACE.
perform bdc_field using 'RM60X-VERSB' '00'.
Converted the date as per MD61 Transaction.
From date
l_frdat1 = p_date+0(4).
l_frdat2 = p_date+4(2).
l_frdat3 = p_date+6(2).
concatenate l_frdat2 l_frdat3 l_frdat1 into l_frdat.
To Date
l_tdate = p_date - 1.
l_todat1 = l_tdate+0(4) + 1.
l_todat2 = l_tdate+4(2).
l_todat3 = l_tdate+6(2).
concatenate l_todat2 l_todat3 l_todat1 into l_todat.
perform bdc_field using 'RM60X-DATVE' l_frdat.
perform bdc_field using 'RM60X-DATBE' l_todat.
perform bdc_field using 'RM60X-ENTLU' 'M'.
Screen 0127
perform bdc_screen using 'SAPMM60X' '0127'.
perform bdc_field using 'BDC_OKCODE' '=WEIT'.
if p_bedae is initial.
perform bdc_field using 'T459U-BEDAE' space.
else.
perform bdc_field using 'T459U-BEDAE' P_BEDAE.
endif.
Screen 0100.
perform bdc_screen using 'SAPMM60X' '0100'.
perform bdc_field using 'BDC_OKCODE' '/00'.
perform bdc_field using 'AM60X-MATAW' 'X'.
perform bdc_field using 'AM60X-MATNR' T_FILE-MATNR.
perform bdc_field using 'AM60X-PRGRP' SPACE.
perform bdc_field using 'AM60X-PBDNR' SPACE.
perform bdc_field using 'RM60X-BERID' T_FILE-BERID.
perform bdc_field using 'AM60X-WERKS' SPACE.
perform bdc_field using 'RM60X-VERSB' '00'.
perform bdc_field using 'RM60X-DATVE' l_frdat.
perform bdc_field using 'RM60X-DATBE' l_todat.
perform bdc_field using 'RM60X-ENTLU' 'M'.
Screen 0200
perform bdc_screen using 'SAPLM60E' '0200'.
perform bdc_field using 'BDC_OKCODE' '=S+'.
perform bdc_field using 'RM60X-PLN01(01)' T_FILE-PLNMG01.
perform bdc_screen using 'SAPLM60E' '0200'.
perform bdc_field using 'BDC_OKCODE' '=S+'.
perform bdc_field using 'RM60X-PLN01(01)' T_FILE-PLNMG02.
perform bdc_screen using 'SAPLM60E' '0200'.
perform bdc_field using 'BDC_OKCODE' '=S+'.
perform bdc_field using 'RM60X-PLN01(01)' T_FILE-PLNMG03.
perform bdc_screen using 'SAPLM60E' '0200'.
perform bdc_field using 'BDC_OKCODE' '=S+'.
perform bdc_field using 'RM60X-PLN01(01)' T_FILE-PLNMG04.
perform bdc_screen using 'SAPLM60E' '0200'.
perform bdc_field using 'BDC_OKCODE' '=S+'.
perform bdc_field using 'RM60X-PLN01(01)' T_FILE-PLNMG05.
perform bdc_screen using 'SAPLM60E' '0200'.
perform bdc_field using 'BDC_OKCODE' '=S+'.
perform bdc_field using 'RM60X-PLN01(01)' T_FILE-PLNMG06.
perform bdc_screen using 'SAPLM60E' '0200'.
perform bdc_field using 'BDC_OKCODE' '=S+'.
perform bdc_field using 'RM60X-PLN01(01)' T_FILE-PLNMG07.
perform bdc_screen using 'SAPLM60E' '0200'.
perform bdc_field using 'BDC_OKCODE' '=S+'.
perform bdc_field using 'RM60X-PLN01(01)' T_FILE-PLNMG08.
perform bdc_screen using 'SAPLM60E' '0200'.
perform bdc_field using 'BDC_OKCODE' '=S+'.
perform bdc_field using 'RM60X-PLN01(01)' T_FILE-PLNMG09.
perform bdc_screen using 'SAPLM60E' '0200'.
perform bdc_field using 'BDC_OKCODE' '=S+'.
perform bdc_field using 'RM60X-PLN01(01)' T_FILE-PLNMG10.
perform bdc_screen using 'SAPLM60E' '0200'.
perform bdc_field using 'BDC_OKCODE' '=S+'.
perform bdc_field using 'RM60X-PLN01(01)' T_FILE-PLNMG11.
perform bdc_screen using 'SAPLM60E' '0200'.
perform bdc_field using 'BDC_OKCODE' '=SICH'.
perform bdc_field using 'RM60X-PLN01(01)' T_FILE-PLNMG12.
ENDFORM. " bdc_build_script
&----
*& Form get_matnr
&----
Get the material number from tables ZMSMI_FERR_RAW,
ZMSMI_SNAP_RAW and ZMSMI_SIMP_RAW
----
FORM get_matnr.
clear v_matnr.
case t_file-werks.
when '0101'.
select single cmatnr from zmsmi_simp_raw
into v_matnr where matnr = t_file-matnr.
if sy-subrc eq 0.
clear t_file-matnr.
t_file-matnr = v_matnr.
endif.
when '0103'.
select single cmatnr from zmsmi_ferr_raw
into v_matnr where matnr = t_file-matnr.
if sy-subrc eq 0.
clear t_file-matnr.
t_file-matnr = v_matnr.
endif.
when '0102' or '0110' or '0111' or '0112' or '0113'
or '0114' or '0115' or '0116' or '0117'.
select single cmatnr from zmsmi_snap_raw
into v_matnr where matnr = t_file-matnr.
if sy-subrc eq 0.
clear t_file-matnr.
t_file-matnr = v_matnr.
endif.
endcase.
ENDFORM. " get_matnr
&----
*& Form bdc_screen
&----
BDC Script for Screen fields
----
-->P_PROG Program name
-->P_SCRN Screen Number
----
FORM bdc_screen USING p_prog
p_scrn.
clear itab_bdc_tab.
itab_bdc_tab-program = p_prog.
itab_bdc_tab-dynpro = p_scrn.
itab_bdc_tab-dynbegin = c_x.
append itab_bdc_tab.
ENDFORM. " bdc_screen
&----
*& Form bdc_field
&----
BDC Script for Screen fileds
----
-->P_NAM Field name
-->P_VAL Field value
----
FORM bdc_field USING p_nam
p_val.
clear itab_bdc_tab.
itab_bdc_tab-fnam = p_nam.
itab_bdc_tab-fval = p_val.
append itab_bdc_tab.
ENDFORM. " bdc_screen
&----
*& Form bdc_submit_transaction
&----
BDC_INSERT Function Module
----
FORM bdc_submit_transaction.
Load BDC script as a trqansction in BDC session
call function 'BDC_INSERT'
EXPORTING
tcode = c_tcode
TABLES
dynprotab = itab_bdc_tab
EXCEPTIONS
internal_error = 01
not_open = 02
queue_error = 03
tcode_invalid = 04.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
refresh itab_bdc_tab.
ENDFORM. " bdc_submit_transaction
&----
*& Form bdc_session_close
&----
text
----
FORM bdc_session_close.
CALL FUNCTION 'BDC_CLOSE_GROUP'
EXCEPTIONS
NOT_OPEN = 1
QUEUE_ERROR = 2
OTHERS = 3
.
skip 2.
if sy-subrc ne 0.
write: / 'Error Closing BDC Session ' , 'RETURN CODE: ', sy-subrc.
else.
write : / 'Session created:', v_ssnname,
50 '# of transactions:', v_trans_in_ssn.
endif.
ENDFORM. " bdc_session_close
Thanks
Seshu
08-29-2007 2:20 PM
Check out the loop statement....
c this code: Example
Calling BDC_OPEN_GROUP Function Module
LOOP AT lt_customer INTO lw_customer.
CLEAR lw_bdcdata.
REFRESH lt_bdcdata[].
PERFORM bdc_dynpro USING 'SAPMF02D' '0107'.
PERFORM bdc_field USING 'BDC_CURSOR'
'RF02D-KTOKD'.
-
-
PERFORM fr_bdc_insert.
ENDLOOP.
PERFORM fr_bdc_close_group.
&----
*& Form BDC_DYNPRO
&----
FORM bdc_dynpro USING program dynpro.
ENDFORM. "BDC_DYNPRO
----
Insert field *
----
FORM bdc_field USING fnam fval.
ENDFORM. "BDC_FIELD
&----
*& Form fr_bdc_open_group
&----
FORM fr_bdc_open_group .
CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
client = sy-mandt
group = 'ZCUST'
keep = 'X'
user = sy-uname
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 sy-subrc <> 0.
MESSAGE e000(00) WITH 'Error while opening the session'.
ENDIF.
ENDFORM. " fr_bdc_open_group
&----
*& Form fr_bdc_insert
&----
FORM fr_bdc_insert .
CALL FUNCTION 'BDC_INSERT'
EXPORTING
tcode = ' '
TABLES
dynprotab = lt_bdcdata[]
EXCEPTIONS
internal_error = 1
not_open = 2
queue_error = 3
tcode_invalid = 4
printing_invalid = 5
posting_invalid = 6
OTHERS = 7.
ENDFORM. " fr_bdc_insert
&----
*& Form fr_bdc_close_group
&----
FORM fr_bdc_close_group .
CALL FUNCTION 'BDC_CLOSE_GROUP'
EXCEPTIONS
not_open = 1
queue_error = 2
OTHERS = 3.
ENDFORM. " fr_bdc_close_group