使用代码创建SAP Sales area

Created by Jerry Wang on Nov 25, 2014

代码如下:

REPORT ZPROD_MULTIPLE_CHANGE.
PARAMETERS: prd1 type comm_product-product_id OBLIGATORY DEFAULT 'ZJERRYTEST11241',
            prd2 type comm_product-product_id OBLIGATORY DEFAULT '1002030'.
DATA:   lt_productm    TYPE  comt_product_maintain_api_extt,
        ls_productm    TYPE  comt_product_maintain_api_ext,
        lt_sale_data   TYPE CRMT_CRMM_PR_SALESH_MAINTAIN_T,
        lt_prod_data   TYPE COMT_PRODUCT_MAINTAIN_API_SETT,
        ls_prod_data   LIKE LINE OF lt_prod_data,
        et_bapireturnh TYPE bapiret2_tab,
        lt_product     TYPE comt_product_s_tab,
        lt_prod_out    TYPE comt_product_tab_guid,
        lt_shorttext   TYPE comt_pr_shtext_maintain_tab,
        ls_shorttext   LIKE LINE OF lt_shorttext,
        lt_prod_id     TYPE STANDARD TABLE OF comm_product-product_id,
        lt_prod_db     TYPE STANDARD TABLE OF comm_product-product_guid.
START-OF-SELECTION.
  APPEND prd1 TO lt_prod_id.
  "APPEND prd2 TO lt_prod_id.
  LOOP AT lt_prod_id ASSIGNING FIELD-SYMBOL(<id>).
    CALL FUNCTION 'CONVERSION_EXIT_PRID1_INPUT'
      EXPORTING
        input  = <id>
      IMPORTING
        output = <id>.
  ENDLOOP.
  SELECT product_guid INTO TABLE lt_prod_db FROM comm_product FOR ALL ENTRIES IN lt_prod_id
     WHERE product_id = lt_prod_id-table_line.
  ASSERT sy-subrc = 0.
  LOOP AT lt_prod_db ASSIGNING FIELD-SYMBOL(<guid>).
    DATA(ls_product) = VALUE comt_product_s( product_guid = <guid> ).
    APPEND ls_product TO lt_product.
  ENDLOOP.
  CALL FUNCTION 'COM_PRODUCT_READ_MULTIPLE'
    EXPORTING
      it_product = lt_product
    IMPORTING
      et_product = lt_prod_out.
  LOOP AT lt_prod_out ASSIGNING FIELD-SYMBOL(<result>).
    MOVE-CORRESPONDING <result> TO ls_productm-header-com_product.
    ls_productm-header-pr_number = sy-tabix.
    PERFORM fill_sales_area USING ls_productm-header-com_product-product_guid CHANGING lt_sale_data .
    ls_prod_data-settype_id = 'CRMM_PR_SALESH'.
    GET REFERENCE OF lt_sale_data INTO ls_prod_data-data.
    APPEND ls_prod_data TO lt_prod_data.
    ls_productm-data = lt_prod_data.
    APPEND ls_productm TO lt_productm.
  ENDLOOP.
  CALL FUNCTION 'COM_PRODUCT_MAINTAIN_MULT_API'
    EXPORTING
      iv_check_only                 = ' '
      iv_suppress_inactive          = 'X'
      iv_activate_inactive_products = 'X'
    IMPORTING
      et_bapireturn                 = et_bapireturnh
    CHANGING
      ct_product                    = lt_productm
    EXCEPTIONS
      internal_error                = 1
      OTHERS                        = 2.
  LOOP AT et_bapireturnh ASSIGNING FIELD-SYMBOL(<error>).
    WRITE: / 'Error: ', <error>-message COLOR COL_NEGATIVE.
  ENDLOOP.
  CHECK et_bapireturnh IS INITIAL.
  CALL FUNCTION 'CRM_PRODUCT_UI_SAVE'
    EXPORTING
      iv_update_task = abap_false.
  ASSERT sy-subrc = 0.
  COMMIT WORK AND WAIT.
  WRITE: / 'Mass change finished successfully' COLOR COL_POSITIVE.
  FORM fill_sales_area  USING guid TYPE comm_product-product_guid CHANGING ct_sales TYPE CRMT_CRMM_PR_SALESH_MAINTAIN_T.
     CLEAR: ct_sales.
     FIELD-SYMBOLS: <line> LIKE LINE OF ct_sales.
     APPEND INITIAL LINE TO ct_sales ASSIGNING <line>.
     <line>-update_type = 'I'.
     <line>-relation-client = sy-mandt.
     <line>-relation-product_guid = guid.
     <line>-relation-fragment_guid = '380BDF7B502D63F7E10000009B38FA0B'.
     <line>-relation-sales_org = 'O 50003059'.
     <line>-relation-distr_chan = '01'.
     <line>-relation-logsys = 'AG3CLNT001'.
  ENDFORM.

如果执行时遇到如下error message:
使用代码创建SAP Sales area
debug发现error message是line 24 行raise出来的:
使用代码创建SAP Sales area
使用代码创建SAP Sales area
使用代码创建SAP Sales area
将missing logical system assign上即可:
使用代码创建SAP Sales area
最后report里hard code的sales area能够成功被创建。
使用代码创建SAP Sales area

上一篇:Date Vault 概念


下一篇:闭包的使用场景