关键点:LOGIC_SWITCH-COND_HANDL = ‘X’
下面具体实例:
FUNCTION ZSD_CRM_CONTRACT_CREATE.
*"----------------------------------------------------------------------
"“本地接口:
*” IMPORTING
*" VALUE(TESTRUN) TYPE BAPIFLAG-BAPIFLAG OPTIONAL
*" VALUE(I_DOC_TYPE) TYPE BAPISDHD1-DOC_TYPE OPTIONAL
*" VALUE(I_SALES_ORG) TYPE BAPISDHD1-SALES_ORG OPTIONAL
*" VALUE(I_DISTR_CHAN) TYPE BAPISDHD1-DISTR_CHAN OPTIONAL
*" VALUE(I_DIVISION) TYPE BAPISDHD1-DIVISION OPTIONAL
*" VALUE(I_CT_VALID_F) TYPE BAPISDHD1-CT_VALID_F OPTIONAL
*" VALUE(I_CT_VALID_T) TYPE BAPISDHD1-CT_VALID_T OPTIONAL
*" VALUE(I_PURCH_NO_C) TYPE BAPISDHD1-PURCH_NO_C OPTIONAL
*" VALUE(I_ZZEM_HTQDRQ) TYPE ZZEM_HTQDRQ OPTIONAL
*" VALUE(I_VTTYP) TYPE VTTYP OPTIONAL
*" VALUE(I_PYMET) TYPE EPIC_EXAMPLE_PAYREF OPTIONAL
*" EXPORTING
*" VALUE(E_JSON) TYPE STRING
*" VALUE(RESULTCODE) TYPE BAPI_MTYPE
*" VALUE(RESULTMSG) TYPE BAPI_MSG
*" TABLES
*" IT_CONTRACT_ITEMS_IN STRUCTURE VBAP OPTIONAL
*" IT_CONTRACT_PARTNERS STRUCTURE BAPIPARNR OPTIONAL
*" ET_RETURN STRUCTURE BAPIRET2 OPTIONAL
*"----------------------------------------------------------------------
DATA : LS_CONTRACT_HEADER_IN TYPE BAPISDHD1,"销售和分销凭证抬头
LS_CONTRACT_HEADER_INX TYPE BAPISDHD1X,"销售和分销凭证抬头
LS_CONTRACT_HEAD_IN TYPE BAPISDH1,"销售和分销凭证抬头
LS_CONTRACT_HEAD_INX TYPE BAPISDH1X,"销售和分销凭证抬头
LS_LOGIC_SWITCH TYPE BAPISDLS,
LT_CONTRACT_ITEMS_IN LIKE TABLE OF BAPISDITM,
LS_CONTRACT_ITEMS_IN LIKE BAPISDITM,
LT_CONTRACT_ITEMS_INX LIKE TABLE OF BAPISDITMX,
LS_CONTRACT_ITEMS_INX LIKE BAPISDITMX,
LT_CONTRACT_PARTNERS LIKE TABLE OF BAPIPARNR,
LS_CONTRACT_PARTNERS LIKE BAPIPARNR,
LT_PARTNERCHANGES LIKE TABLE OF BAPIPARNRC,
LS_PARTNERCHANGES LIKE BAPIPARNRC,
LT_CONDITIONS_IN LIKE TABLE OF BAPICOND,
LS_CONDITIONS_IN LIKE BAPICOND,
LT_CONDITIONS_INX LIKE TABLE OF BAPICONDX,
LS_CONDITIONS_INX LIKE BAPICONDX,
LV_SALESDOCUMENT TYPE BAPIVBELN-VBELN,"销售订单号
LT_RETURN LIKE TABLE OF BAPIRET2,
LS_RETURN LIKE BAPIRET2.
DATA:BEGIN OF LS_MSG,
TYPE TYPE BAPI_MTYPE,"
MESSAGE TYPE BAPI_MSG,"
END OF LS_MSG.
DATA: LT_MSG LIKE TABLE OF LS_MSG,
ls_ZZCRM_CONTRACT TYPE ZZCRM_CONTRACT.
** filling partners details
CLEAR LT_CONTRACT_PARTNERS.
LOOP AT IT_CONTRACT_PARTNERS.
CLEAR:LS_CONTRACT_PARTNERS.
*
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT’
EXPORTING
input = IT_CONTRACT_PARTNERS-PARTN_NUMB
IMPORTING
output = IT_CONTRACT_PARTNERS-PARTN_NUMB.
LS_CONTRACT_PARTNERS-partn_role = IT_CONTRACT_PARTNERS-PARTN_ROLE.
LS_CONTRACT_PARTNERS-partn_numb = IT_CONTRACT_PARTNERS-PARTN_NUMB.
LS_CONTRACT_PARTNERS-itm_number = '000000'.
APPEND LS_CONTRACT_PARTNERS TO LT_CONTRACT_PARTNERS.
LS_CONTRACT_PARTNERS-partn_role = IT_CONTRACT_PARTNERS-PARTN_ROLE.
LS_CONTRACT_PARTNERS-partn_numb = IT_CONTRACT_PARTNERS-PARTN_NUMB.
LS_CONTRACT_PARTNERS-itm_number = '000000'.
APPEND LS_CONTRACT_PARTNERS TO LT_CONTRACT_PARTNERS.
ENDLOOP.
- " 抬头数据 - 组织数据
CLEAR:LS_CONTRACT_HEADER_IN.
LS_CONTRACT_HEADER_IN-doc_type = I_DOC_TYPE ."销售凭证类型
LS_CONTRACT_HEADER_IN-sales_org = I_SALES_ORG ."销售组织
LS_CONTRACT_HEADER_IN-distr_chan = I_DISTR_CHAN ."分销渠道
LS_CONTRACT_HEADER_IN-division = I_DIVISION ."产品组
LS_CONTRACT_HEADER_IN-ct_valid_f = I_CT_VALID_F."有效起始日期(框架协议,产品建议)
LS_CONTRACT_HEADER_IN-ct_valid_t = I_CT_VALID_T."有效至日期(框架协议,产品建议)
LS_CONTRACT_HEADER_IN-purch_no_c = I_PURCH_NO_C."合同号
LS_CONTRACT_HEADER_INX-DOC_TYPE = ‘X’.
LS_CONTRACT_HEADER_INX-SALES_ORG = ‘X’.
LS_CONTRACT_HEADER_INX-DISTR_CHAN = ‘X’.
LS_CONTRACT_HEADER_INX-DIVISION = ‘X’.
LS_CONTRACT_HEADER_INX-CT_VALID_F = ‘X’.
LS_CONTRACT_HEADER_INX-CT_VALID_T = ‘X’.
LS_CONTRACT_HEADER_INX-PURCH_NO_C = ‘X’.
CLEAR LS_LOGIC_SWITCH.
LS_LOGIC_SWITCH-COND_HANDL = ‘X’.
*行数据
CLEAR:LT_CONTRACT_ITEMS_IN.
LOOP AT IT_CONTRACT_ITEMS_IN.
CLEAR:LS_CONTRACT_ITEMS_IN,LS_CONTRACT_ITEMS_INX.
"增加前导零
*项目行号
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT’
EXPORTING
input = IT_CONTRACT_ITEMS_IN-POSNR
IMPORTING
output = IT_CONTRACT_ITEMS_IN-POSNR.
*物料号
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT’
EXPORTING
input = IT_CONTRACT_ITEMS_IN-MATNR
IMPORTING
output = IT_CONTRACT_ITEMS_IN-MATNR.
*
LS_CONTRACT_ITEMS_IN-itm_number = IT_CONTRACT_ITEMS_IN-POSNR.
LS_CONTRACT_ITEMS_IN-material = IT_CONTRACT_ITEMS_IN-MATNR.
LS_CONTRACT_ITEMS_IN-target_qty = IT_CONTRACT_ITEMS_IN-ZMENG."销售单位目标数量
LS_CONTRACT_ITEMS_IN-target_qu = IT_CONTRACT_ITEMS_IN-ZIEME."目标数量计量单位
APPEND LS_CONTRACT_ITEMS_IN TO LT_CONTRACT_ITEMS_IN.
LS_CONTRACT_ITEMS_INX-itm_number = IT_CONTRACT_ITEMS_IN-POSNR.
LS_CONTRACT_ITEMS_INX-material = 'X'.
LS_CONTRACT_ITEMS_INX-target_qty = 'X'.
LS_CONTRACT_ITEMS_INX-target_qu = 'X'.
APPEND LS_CONTRACT_ITEMS_INX TO LT_CONTRACT_ITEMS_INX.
*Item Conditions DEVK911113 新增
CLEAR: LS_CONDITIONS_IN,LS_CONDITIONS_INX.
LS_CONDITIONS_IN-itm_number = IT_CONTRACT_ITEMS_IN-POSNR."项目号
LS_CONDITIONS_IN-cond_type = ‘ZA01’. "条件类型 konv-kschl
LS_CONDITIONS_IN-cond_value = IT_CONTRACT_ITEMS_IN-netpr. "净价 vbap-NETPR/单价 konv-kbetr
LS_CONDITIONS_IN-currency = IT_CONTRACT_ITEMS_IN-waerk. "货币或% vbap-waerk konv-waers
LS_CONDITIONS_IN-cond_unit = IT_CONTRACT_ITEMS_IN-ZIEME. "条件单位vbap-KMEIN
LS_CONDITIONS_IN-cond_p_unt = 1. "条件定价单位vbap-KPEIN
APPEND LS_CONDITIONS_IN TO LT_CONDITIONS_IN.
LS_CONDITIONS_INX-itm_number = IT_CONTRACT_ITEMS_IN-POSNR."项目号
LS_CONDITIONS_INX-cond_type = 'ZA01'. "条件类型 konv-kschl
LS_CONDITIONS_INX-cond_value = 'X'. "净价 借用vbap-NETPR/单价 实际为konv-kbetr
LS_CONDITIONS_INX-currency = 'X'. "货币或% vbap-waerk konv-waers
LS_CONDITIONS_INX-cond_unit = 'X'. "条件单位vbap-KMEIN
LS_CONDITIONS_INX-cond_p_unt = 'X'. "条件定价单位vbap-KPEIN
APPEND LS_CONDITIONS_INX TO LT_CONDITIONS_INX.
ENDLOOP.
**call bapi
CALL FUNCTION ‘BAPI_CONTRACT_CREATEFROMDATA’
EXPORTING
- SALESDOCUMENTIN =
CONTRACT_HEADER_IN = LS_CONTRACT_HEADER_IN - CONTRACT_HEADER_INX =
- SENDER =
- BINARY_RELATIONSHIPTYPE = ’ ’
- INT_NUMBER_ASSIGNMENT = ’ ’
- BEHAVE_WHEN_ERROR = ’ ’
LOGIC_SWITCH = LS_LOGIC_SWITCH
TESTRUN = TESTRUN - CONVERT = ’ ’
IMPORTING
SALESDOCUMENT = LV_SALESDOCUMENT
TABLES
RETURN = LT_RETURN
CONTRACT_ITEMS_IN = LT_CONTRACT_ITEMS_IN - CONTRACT_ITEMS_INX =
CONTRACT_PARTNERS = LT_CONTRACT_PARTNERS
CONTRACT_CONDITIONS_IN = LT_CONDITIONS_IN
CONTRACT_CONDITIONS_INX = LT_CONDITIONS_INX - CONTRACT_CFGS_REF =
- CONTRACT_CFGS_INST =
- CONTRACT_CFGS_PART_OF =
- CONTRACT_CFGS_VALUE =
- CONTRACT_CFGS_BLOB =
- CONTRACT_CFGS_VK =
- CONTRACT_CFGS_REFINST =
- CONTRACT_DATA_IN =
- CONTRACT_DATA_INX =
- CONTRACT_TEXT =
- CONTRACT_KEYS =
- EXTENSIONIN =
- PARTNERADDRESSES =
- EXTENSIONEX =
.
*处理bapi结果
LOOP AT LT_RETURN TRANSPORTING NO FIELDS WHERE TYPE = ‘A’ OR
TYPE = ‘E’ OR
TYPE = ‘X’.
EXIT.
ENDLOOP.
*内表赋值
ET_RETURN[] = LT_RETURN.
IF SY-SUBRC EQ 0.
CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK’.
LOOP AT LT_RETURN INTO LS_RETURN WHERE TYPE = ‘E’.
CLEAR LS_MSG.
LS_MSG-TYPE = 'E'.
LS_MSG-MESSAGE = LS_RETURN-MESSAGE.
APPEND LS_MSG TO LT_MSG.
CONCATENATE RESULTMSG '*' LS_RETURN-MESSAGE INTO RESULTMSG.
ENDLOOP.
RESULTCODE = ‘E’.
ELSE.
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
EXPORTING
WAIT = ‘X’.
CLEAR LS_MSG.
LS_MSG-TYPE = ‘S’.
LS_MSG-MESSAGE = LV_SALESDOCUMENT.
APPEND LS_MSG TO LT_MSG.
RESULTCODE = ‘S’.
RESULTMSG = LV_SALESDOCUMENT.
*保存合同相关数据
IF I_ZZEM_HTQDRQ IS NOT INITIAL.
CLEAR ls_ZZCRM_CONTRACT.
ls_ZZCRM_CONTRACT-VBELN = LV_SALESDOCUMENT.
ls_ZZCRM_CONTRACT-ZZEM_HTQDRQ = I_ZZEM_HTQDRQ.
ls_ZZCRM_CONTRACT-VTTYP = I_VTTYP.
ls_ZZCRM_CONTRACT-PYMET = I_PYMET.
MODIFY ZZCRM_CONTRACT FROM ls_ZZCRM_CONTRACT.
ENDIF.
ENDIF.
***内表->JSON
E_JSON = /ui2/cl_json=>serialize( data = LT_MSG ).
ENDFUNCTION.