二维码

[BAPI] BAPI创建销售订单BAPI_SALESORDER_CREATEFROMDAT2

Twilight发表于 2014-01-25 16:31Twilight 最后回复于 2014-01-25 16:31 [复制链接] 5272 0

在ABAP中调用BAPI的方法与调用Function Module的方法相同。只是要注意BAPI函数没有Exception,调用是否成功是通过参数return返回的。该return参数在不同的BAPI中结构不同,可能是structure,也可能是table,编程时要注意。
下面的程序是ABAP中调用BAPI_SALESORDER_CREATEFROMDAT2的例子。
注意:
1.必须调用BAPI_TRANSACTION_COMMIT完成整个事务。
2.某些数据必须用辅助结构(负责确定所要更新的数据位)完成数据更新。(如ORDER_ITEMS_IN和ORDER_ITEMS_INX)

  1. * Order header:
  2. * - Order type: OR  Important you must use the german code TA
  3. * - Sales org: 1000
  4. * - Distrb. chan.: 10
  5. * - Division: 00
  6. * - Sold to party: 1032
  7. * - Ship to party: 1032
  8. * - Purch order: DG-19970626-3
  9. * Order item:
  10. * - Material: P-100
  11. * - Qty: 1
  12. DATA:
  13. * Order partners
  14. LI_ORDER_PARTNERS    TYPE STANDARD TABLE OF BAPIPARNR,
  15. L_ORDER_PARTNERS     LIKE BAPIPARNR,
  16. * Structures for order header
  17. L_ORDER_HEADER_IN    LIKE BAPISDHD1,
  18. L_ORDER_HEADER_INX   LIKE BAPISDHD1X,
  19. * Tables for order items
  20. LI_ORDER_ITEMS_IN    TYPE STANDARD TABLE OF BAPISDITM,
  21. L_ORDER_ITEMS_IN     LIKE BAPISDITM,
  22. LI_ORDER_ITEMS_INX   TYPE STANDARD TABLE OF BAPISDITMX,
  23. L_ORDER_ITEMS_INX    LIKE BAPISDITMX,
  24. * Return table from bapi call
  25. LI_RETURN TYPE STANDARD TABLE OF BAPIRET2,
  26. L_RETURN  TYPE BAPIRET2,
  27. * Sales document number
  28.   L_VBELN LIKE BAPIVBELN-VBELN,
  29. * Error flag
  30.   L_ERRFLAG(1) TYPE C.

  31. START-OF-SELECTION.
  32. *------------------------------------------------------------------
  33. * Build partner information
  34. *------------------------------------------------------------------
  35.   CLEAR L_ORDER_PARTNERS.
  36.   L_ORDER_PARTNERS-PARTN_ROLE = 'AG'.          "Remember German codes !
  37.   L_ORDER_PARTNERS-PARTN_NUMB = '0000001000'.
  38.   APPEND L_ORDER_PARTNERS TO LI_ORDER_PARTNERS.
  39. *------------------------------------------------------------------
  40. * Build order header
  41. *------------------------------------------------------------------
  42. * Update flag
  43.   L_ORDER_HEADER_INX-UPDATEFLAG = 'I'.
  44. * Sales document type
  45.   L_ORDER_HEADER_IN-DOC_TYPE    = 'TA'.        "Remember German codes !
  46.   L_ORDER_HEADER_INX-DOC_TYPE   = 'X'.
  47. * Sales organization
  48.   L_ORDER_HEADER_IN-SALES_ORG  = '1000'.
  49.   L_ORDER_HEADER_INX-SALES_ORG  = 'X'.
  50. * Distribution channel
  51.   L_ORDER_HEADER_IN-DISTR_CHAN = '10'.
  52.   L_ORDER_HEADER_INX-DISTR_CHAN = 'X'.
  53. * Division
  54.   L_ORDER_HEADER_IN-DIVISION = '00'.
  55.   L_ORDER_HEADER_INX-DIVISION = 'X'.
  56. * Purchase order
  57.   L_ORDER_HEADER_IN-PURCH_NO_C = 'DG-19970626-300-FLC'.
  58.   L_ORDER_HEADER_INX-PURCH_NO_C = 'X'.
  59. *------------------------------------------------------------------
  60. * Build order item(s) - Only 1 is used in this example
  61. *------------------------------------------------------------------
  62. *
  63.   L_ORDER_ITEMS_IN-ITM_NUMBER = '000010'.
  64.   L_ORDER_ITEMS_INX-ITM_NUMBER = '000010'.
  65.   L_ORDER_ITEMS_IN-MATERIAL = 'P-100'.
  66.   L_ORDER_ITEMS_INX-MATERIAL = 'X'.
  67.   L_ORDER_ITEMS_IN-TARGET_QTY = '3.000'.
  68.   L_ORDER_ITEMS_INX-TARGET_QTY = 'X'.
  69.   L_ORDER_ITEMS_IN-COMP_QUANT = 3 .
  70.   L_ORDER_ITEMS_INX-COMP_QUANT = 'X'.
  71.   APPEND L_ORDER_ITEMS_IN TO LI_ORDER_ITEMS_IN.
  72.   L_ORDER_ITEMS_INX-UPDATEFLAG = 'I'.
  73.   APPEND L_ORDER_ITEMS_INX TO LI_ORDER_ITEMS_INX.
  74. *------------------------------------------------------------------
  75. * CALL Bapi
  76. *------------------------------------------------------------------
  77.   CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
  78.        EXPORTING
  79.             ORDER_HEADER_IN  = L_ORDER_HEADER_IN
  80.             ORDER_HEADER_INX = L_ORDER_HEADER_INX
  81.        IMPORTING
  82.             SALESDOCUMENT    = L_VBELN
  83.        TABLES
  84.             RETURN           = LI_RETURN
  85.             ORDER_ITEMS_IN   = LI_ORDER_ITEMS_IN
  86.             ORDER_ITEMS_INX  = LI_ORDER_ITEMS_INX
  87.             ORDER_PARTNERS   = LI_ORDER_PARTNERS.
  88. END-OF-SELECTION.
  89. *------------------------------------------------------------------
  90. * Check and write Return table
  91. *------------------------------------------------------------------
  92.   CLEAR L_ERRFLAG.
  93.   WRITE: / 'Sales dcoument: ', L_VBELN.
  94.   LOOP AT LI_RETURN INTO L_RETURN.
  95.     WRITE: / L_RETURN-TYPE, L_RETURN-MESSAGE(50).
  96.     IF L_RETURN-TYPE = 'E'.
  97.       L_ERRFLAG = 'X'.
  98.     ENDIF.
  99.   ENDLOOP.
  100. *------------------------------------------------------------------
  101. * No errors - Commit
  102. *------------------------------------------------------------------
  103. CLEAR L_RETURN.
  104.   IF L_ERRFLAG  IS INITIAL.
  105.     CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
  106.     EXPORTING
  107.         RETURN = L_RETURN
  108.         .
  109.     WRITE : / L_RETURN-TYPE , L_RETURN-MESSAGE .
  110.   ENDIF.
复制代码
回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表