ABAP调用CIS webservice

2020-11-27 17:14:18 浏览数 (1)

代码语言:javascript复制
FUNCTION ZMLSCP1_FR0008.
*"----------------------------------------------------------------------*"*"本地接口:
*"  IMPORTING*"     VALUE(I_EBELN) TYPE  EBELN OPTIONAL*"     VALUE(I_MATNR) TYPE  MATNR OPTIONAL*"  EXPORTING*"     VALUE(O_STATU) TYPE  CHAR1*"     VALUE(O_MSG) TYPE  CHAR255*"  TABLES*"      OT_RESOURCE STRUCTURE  ZSRM_RESOURCE*"----------------------------------------------------------------------*&Flag0-手动;1-自动
  DATA:L_TABIX TYPE SY-TABIX.
  DATA:LV_FLAG TYPE C.
  DATA:LV_CON  TYPE STRING.
  DATA: ERROR TYPE REF TO CX_AI_SYSTEM_FAULT.
  DATA: PRXY  TYPE REF TO ZCO_REQUEST_IMATNR_RESOURCE_SE."Proxy NameDATA: Z_IN  TYPE ZREQUEST8."request inDATA: Z_OUT TYPE ZRESPONSE8."request out
  DATA: O_CX  TYPE  REF  TO  CX_ROOT.
  DATA: O_ERROR TYPE REF TO CX_ST_ERROR.
  TYPES:BEGIN OF TY_RE,
    BACKUP1 TYPE STRING,"备用字段1BACKUP2 TYPE STRING,"备用字段2BACKUP3 TYPE STRING,"备用字段3BACKUP4 TYPE STRING,"备用字段4BACKUP5 TYPE STRING,"备用字段5EBELN   TYPE STRING,"采购订单号EBELP   TYPE STRING,"采购订单行项目号LIFNRAMOUNT TYPE STRING,"供应商库存数量MAKTX   TYPE STRING,"物料描述MATNR   TYPE STRING,"物料代码PROAMOUNT TYPE STRING,"生产数量STOREAMOUNT TYPE STRING,"线边仓库存数量*    PRODATE TYPE DATUM,"生产日期
        END OF TY_RE.
  DATA:GT_RE TYPE STANDARD TABLE OF TY_RE,
       WA_RE LIKE LINE OF GT_RE.
*&job user
  IF SY-UNAME EQ''.
    LV_FLAG ='1'.
  ELSE.
    LV_FLAG ='0'.
  ENDIF.
  CONCATENATE '<in0>'LV_FLAG'</in0>' '<in1>'I_EBELN'</in1>' '<in2>'I_MATNR'</in2>' INTO LV_CON.
*&s1.call proxyget data via cis
*  Z_IN-REQUEST_DATA ='<?xml version="1.0" encoding="UTF-8"?><handleIMatnrResourceService><in0>'&& LV_FLAG &&'</in0></handleIMatnrResourceService>'.
  CONCATENATE '<?xml version="1.0" encoding="UTF-8"?><handleIMatnrResourceService>'LV_CON'</handleIMatnrResourceService>'INTO Z_IN-REQUEST_DATA.
  CLEAR ERROR.
  TRY.
      CREATE OBJECT PRXY
        EXPORTING
          LOGICAL_PORT_NAME ='ZMLSCP_GET_RESOURCE'.
      CALL METHOD PRXY->REQUEST
        EXPORTING
          INPUT  = Z_IN
        IMPORTING
          OUTPUT = Z_OUT.
    CATCH CX_AI_SYSTEM_FAULT INTO ERROR.
      O_STATU ='E'.
      O_MSG = ERROR->ERRORTEXT.
      RETURN.
  ENDTRY.
*&1.1 isit ok
  IF Z_OUT-RETURN_CODE ='E'.
    O_STATU ='E'.
    O_MSG = Z_OUT-RETURN_DESC.
*    RETURN.
  ENDIF.
*for test:
  IF Z_OUT-RETURN_DATA IS INITIAL.
    CONCATENATE
    '<?xml version="1.0" encoding="UTF-8"?>'
    '<handleIMatnrResourceServiceResponse><out>'
    '<MLMatnrResource><backup1/><backup2/><backup3/><backup4/><backup5/><ebeln>4800064316</ebeln><ebelp>00010</ebelp><lifnrAmount/><maktx>A级冰箱型号标贴_C0414.6-1(A</maktx>'
    '<matnr>000000000800013563</matnr><proAmount/><storeAmount>12.0</storeAmount></MLMatnrResource>'
    '<MLMatnrResource><backup1/><backup2/><backup3/><backup4/><backup5/><ebeln>4800064316</ebeln><ebelp>00020</ebelp><lifnrAmount/><maktx>A级冰箱型号标贴_C0414.6-1(A</maktx>'
    '<matnr>000000000800013563</matnr><proAmount/><storeAmount>12.0</storeAmount></MLMatnrResource>'
    '<MLMatnrResource><backup1/><backup2/><backup3/><backup4/><backup5/><ebeln>4800064317</ebeln><ebelp>00010</ebelp><lifnrAmount/><maktx>A级冰箱型号标贴_C0414.6-1(A</maktx>'
    '<matnr>000000000800013563</matnr><proAmount/><storeAmount>12.0</storeAmount></MLMatnrResource>'
    '</out></handleIMatnrResourceServiceResponse>'INTO Z_OUT-RETURN_DATA .
  ENDIF.
*&s2.transfer XML
  IF Z_OUT-RETURN_DATA IS NOT INITIAL."业务系统返回报文不为空
    TRY .
        CLEAR:GT_RE,WA_RE.
        CALL TRANSFORMATION  ZML_XML_TRANS02"ZML_XML_RESOURCESOURCE XML Z_OUT-RETURN_DATA
        RESULT MLMATNRRESOURCE =  GT_RE."OT_RESOURCE[]IF  SY-SUBRC NE0.
          O_STATU ='E'.
          O_MSG ='XML解析失败!'.
          RETURN.
        ENDIF.
      CATCH CX_ST_ERROR INTO O_ERROR .
        O_STATU ='E'.
        O_MSG = O_ERROR->GET_TEXT( ) .
    ENDTRY.
  ELSE.
    O_STATU ='E'.
    O_MSG ='返回结果为空!'.
  ENDIF.
*&s3.data save to ZSRM_RESOURCE
    LOOP AT GT_RE INTO WA_RE.
      MOVE-CORRESPONDING WA_RE TO OT_RESOURCE.
      OT_RESOURCE-ZDATUM = SY-DATUM.
      OT_RESOURCE-ZCPUTM = SY-UZEIT.
      OT_RESOURCE-USNAM  = SY-UNAME.
      OT_RESOURCE-ZFLAG  = LV_FLAG.
      APPEND OT_RESOURCE.
* MODIFY GT_RE FROM WA_RE.
* CLEAR WA_RE.
    ENDLOOP.
* TRY
    CHECK OT_RESOURCE[] IS NOT INITIAL.
    MODIFY ZSRM_RESOURCE FROM TABLE OT_RESOURCE.
    IF SY-SUBRC EQ0.
      COMMIT WORK AND WAIT .
      DESCRIBE TABLE OT_RESOURCE LINES L_TABIX.
      O_STATU ='S'.
      O_MSG   ='成功存入ZSRM_RESOURCE'&& L_TABIX &&'条记录!'.
    ELSE.
      ROLLBACK WORK.
      O_STATU ='E'.
      O_MSG   ='数据存入 ZSRM_RESOURCE表失败!'.
    ENDIF.
ENDFUNCTION.

0 人点赞