如何创建实用call transaction方法的BDC程序

如何创建实用call transaction方法的BDC程序,第1张

程序创建步骤:

1 对相应功能进行录屏:

shdb ------>xk01--------->生成程序(zxk01)和文件(zxk01.txt).

2.新建一个程序(zbdcxk01)

(1)声明BDCDASTA:

DATA: BDCDATA LIKE BDCDATAOCCURS 0 WITH HEADER LINE.

DATA: MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.

dataNODATA value '/' .

TABLES: T100.

parameters p_file like rlgrap-filename .

at selection-screen on value-request for p_file.

perform file_find .

(2) 创建file_find子程序

FORM file_find .

CALL FUNCTION 'KD_GET_FILENAME_ON_F4'

EXPORTING

STATIC= 'X'

CHANGING

FILE_NAME = p_file .

ENDFORM." file_find

(3)声明目标文件的类型,从程序(zxk01)中拷贝以下代码.

*** Generated data section with specific formatting - DO NOT CHANGE ***

data: begin of record occurs 0,

* data element: LIF16

LIFNR_001(016),

* data element: KTOKK

KTOKK_002(004),

* data element: NAME1_GP

NAME1_003(035),

* data element: SORTL

SORTL_004(010),

* data element: LAND1_GP

LAND1_005(003),

* data element: SPRAS

SPRAS_006(002),

end of record.

(4) 声明目标文件的结构体:

data wa_record like record.

(5)声明源文件的类型,根据业务需求,和数据格式自己做.

data: begin of it_record occurs 0,

* data element: LIF16

LIFNR_001(016),

* data element: NAME1_GP

NAME1_003(035),

* data element: SORTL

SORTL_004(010),

end of it_record.

(6)声明源文件的结构体:

data wa_it_record like it_record.

(7) 在start-of-selection .下面调用uploadfile 程序.

start-of-selection .

perform uploadfile .

(8) 创建uploadfile 子程序.

data filename type string .

filename = p_file .

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

FILENAME = filename

* FILETYPE = 'ASC'

HAS_FIELD_SEPARATOR = 'X'

* HEADER_LENGTH = 0

* READ_BY_LINE = 'X'

* DAT_MODE = ' '

* CODEPAGE = ' '

* IGNORE_CERR = ABAP_TRUE

* REPLACEMENT = '#'

* CHECK_BOM = ' '

* IMPORTING

* FILELENGTH=

* HEADER=

TABLES

DATA_TAB = it_record

* 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.

ENDFORM." uploadfile

(9)将源文件数据传给目标文件内表,自己写以下代码, record-LIFNR_001 等格式来源于程序(zxk01).

loop at it_record into wa_it_record .

record-LIFNR_001 = wa_it_record-lifnr_001 .

record-KTOKK_002 = '0001'.

record-NAME1_003 = wa_it_record-name1_003 .

record-SORTL_004 = wa_it_record-SORTL_004.

record-LAND1_005 = 'CN'.

record-SPRAS_006 = 'EN'.

append record .

endloop .

(10) 模拟功能实现步骤,这部分代码可以从程序(zxk01)中拷贝,但是要注意将 record-LIFNR_001改为 wa_record-LIFNR_001.其他类似项也一样 要加wa_,代码如下所示.

loop at record into wa_record .

perform bdc_dynpro using 'SAPMF02K' '0100'.

perform bdc_field using 'BDC_CURSOR'

'RF02K-KTOKK'.

perform bdc_field using 'BDC_OKCODE'

'/00'.

perform bdc_field using 'RF02K-LIFNR'

wa_record-LIFNR_001.

perform bdc_field using 'RF02K-KTOKK'

wa_record-KTOKK_002.

perform bdc_dynpro using 'SAPMF02K' '0110'.

perform bdc_field using 'BDC_CURSOR'

'LFA1-SPRAS'.

perform bdc_field using 'BDC_OKCODE'

'/00'.

perform bdc_field using 'LFA1-NAME1'

wa_record-NAME1_003.

perform bdc_field using 'LFA1-SORTL'

wa_record-SORTL_004.

perform bdc_field using 'LFA1-LAND1'

wa_record-LAND1_005.

perform bdc_field using 'LFA1-SPRAS'

wa_record-SPRAS_006.

perform bdc_dynpro using 'SAPMF02K' '0120'.

perform bdc_field using 'BDC_CURSOR'

'LFA1-KUNNR'.

perform bdc_field using 'BDC_OKCODE'

'/00'.

perform bdc_dynpro using 'SAPMF02K' '0130'.

perform bdc_field using 'BDC_CURSOR'

'LFBK-BANKS(01)'.

perform bdc_field using 'BDC_OKCODE'

'=ENTR'.

perform bdc_dynpro using 'SAPLSPO1' '0300'.

perform bdc_field using 'BDC_OKCODE'

'=YES'.

(11) 创建 bdc_dynpro子程序,可以在程序(zxk01)中拷贝

FORM BDC_DYNPRO USING PROGRAM DYNPRO.

CLEAR BDCDATA.

BDCDATA-PROGRAM = PROGRAM.

BDCDATA-DYNPRO = DYNPRO.

BDCDATA-DYNBEGIN = 'X'.

APPEND BDCDATA.

ENDFORM." BDC_DYNPRO

(12) 创建bdc_field 程序,可以在程序(zxk01)中拷贝

FORM BDC_FIELD USINGP_FNAM P_FVAL.

IF P_FVAL <>NODATA.

CLEAR BDCDATA.

BDCDATA-FNAM = P_FNAM.

BDCDATA-FVAL = P_FVAL.

APPEND BDCDATA.

ENDIF.

ENDFORM." BDC_FIELD

(13) 调用CALL TANSACTION方法 将'XK01' 换成相应的事物码即可.

call transaction 'XK01' USING BDCDATA MODE 'N'

UPDATE 'S' MESSAGES INTO messtab.

clear bdcdata.

refresh bdcdata.

DATA: L_MSTRING(480).

DATA: L_SUBRC LIKE SY-SUBRC.

LOOP AT MESSTAB.

SELECT SINGLE * FROM T100 WHERE SPRSL = MESSTAB-MSGSPRA

AND ARBGB = MESSTAB-MSGID

AND MSGNR = MESSTAB-MSGNR.

IF SY-SUBRC = 0.

L_MSTRING = T100-TEXT.

IF L_MSTRING CS '&1'.

REPLACE '&1' WITH MESSTAB-MSGV1 INTO L_MSTRING.

REPLACE '&2' WITH MESSTAB-MSGV2 INTO L_MSTRING.

REPLACE '&3' WITH MESSTAB-MSGV3 INTO L_MSTRING.

REPLACE '&4' WITH MESSTAB-MSGV4 INTO L_MSTRING.

ELSE.

REPLACE '&' WITH MESSTAB-MSGV1 INTO L_MSTRING.

REPLACE '&' WITH MESSTAB-MSGV2 INTO L_MSTRING.

REPLACE '&' WITH MESSTAB-MSGV3 INTO L_MSTRING.

REPLACE '&' WITH MESSTAB-MSGV4 INTO L_MSTRING.

ENDIF.

CONDENSE L_MSTRING.

WRITE: / MESSTAB-MSGTYP, L_MSTRING(250).

ELSE.

WRITE: / MESSTAB.

ENDIF.

ENDLOOP.

endloop .

(14)整个过程已经结束,检查激活该程序,运行并从中选择需要上传的文件例如:zver001.txt,然后回车就可以完成了.

注意事项:

在该程序的书写过程中在以下几处设置断点可以检查出程序中的错误.

(1)bdcdata

看里面是否有数据

(2) append record .

看里面是否有数据

(3) loop at it_record into wa_it_record .

看里面是否有数据

(4)bdcdata,record和it_record都有数据那么就看

perform bdc_field using 'RF02K-LIFNR'

wa_record-LIFNR_001.

是 wa_record-LIFNR_001还是 record-LIFNR_001.

只要有你写的BDC程序的权限,就能能执行你的程序。

分两种情况:

1 -- 用户没有Tcode的权限,但是Tcode *** 作过程中不会进行其他权限校验。

这种情况下,用户能够执行你的BDC程序,也能得到成功的结果。因为call Tcode 会跳

过权限检查。

2 ---- 用户没有Tcode的权限,但是Tcode *** 作过程中会进行其他权限校验(假设用户没有)。

这样用户还是能够执行你的BDC程序,但是会得到失败的结果。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/10896213.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-12
下一篇 2023-05-12

发表评论

登录后才能评论

评论列表(0条)

保存