Sample ABAP Program to Update Customer with CMD_EI_API

SAP CRM e1459022344657

This is a sample ABAP program to Update Customer Or Person / contact with the SAP API Class CMD_EI_API for Customer Modification.
First let’s introduce the main Class Methods , then a sample ABAP Program showing how to use the CMD_EI_API class to update SAP Customer Data.

Class CMD_EI_API Methods

The list of CMD_EI_API Methods will be:

Method Description
PREPARE_SALES_DATA? Fill Internal Tables of Sales Data
PROCESS_COMPANY_DATA? Actual Updating of Company Code Data
PROCESS_KNAS? Read KNAS Data
WORKLIST_CUSTOMER_MAINTAIN? Call Function Module WORKLIST_CUSTOMER_MAINTAIN
PROCESS_KNAT? Read KNAT Data
PROCESS_KNB5? Read KNB5 Data
PROCESS_KNBK? Read KNBK Data
PROCESS_KNBW? Read KNBW Data
PROCESS_KNEX? Read KNEX Data
PROCESS_KNVA? Read KNVA Data
PROCESS_KNVD? Read KNVD Data
PROCESS_KNVI? Read KNVI Data
UPDATE_VENDOR? Change Linked Vendors
UPDATE_STRUC_OF_TABLE? Write Data on Given Table to Internal Structure
UPDATE_SALES_TEXTS? Update Sales Texts
UPDATE_SALES_DATA? Update Sales Data
UPDATE_MODULES_SINGLE? Call Update for a Customer
UPDATE_MODULES? Call All Necessary Update Modules
PROCESS_KNVK? Read KNVK Data
PROCESS_KNVP? Read KNVP Data
PROCESS_SALES_DATA? Actual Updating of Sales Data
PROCESS_VCKUN_VCNUM? Read KNAS Data
SET_KNA1_CONFS? Fill Field KNA1
SET_KNA1_CONFS_DIRECT? Set Confirmation Flag Directly Without Checks
UPDATE_GENERAL_DATA? Update General Data
UPDATE_COMPANY_DATA? Update Company Code Data
UPDATE_COMPANY_CODE_TEXTS? Update Accounting Texts
UPDATE_CENTRAL_TEXTS? Update Central Texts
UPDATE_CENTRAL_PARTNER_TEXTS? Update Central Partner Texts
UPDATE_ADDRESS? Update Address
UNLOCK? Unlock Customer
STRUC_KNVV_COMPLETE? Complete KNVV
STRUC_KNVK_COMPLETE? Complete KNVK
STRUC_KNB1_COMPLETE? Complete KNB1
SET_KNB1_CONFS? Fill Field KNB1
SET_LOCK? Write Lock Table
STRUC_KNA1_COMPLETE? Complete KNA1
ADDRESSES_COMPLETE? Complete Addresses
ADDR_DELETE? Delete Contact Person Address
ADDR_PERSONAL_DELETE? Delete Contact Person Address
ADDR_PERS_COMP_DELETE? Delete Contact Person Address
BUILD_ADDRESS_REFERENCE? Create Address Reference
BUILD_PERSON_REFERENCE? Create Address Reference
CHECK_SENSITIVE_FIELD_KNA1? Check Sensitive Fields KNA1
CHECK_SENSITIVE_FIELD_KNAS? Check Sensitive Fields KNAS
CHECK_SENSITIVE_FIELD_KNB1? Check Sensitive Fields KNB1
CHECK_SENSITIVE_FIELD_KNB5? Check Sensitive Fields KNB5
CHECK_SENSITIVE_FIELD_KNBK? Check Sensitive Fields KNBK
CLEAR_UNCHANGED_CUSTOMER_DATA? Delete Customer Data if Unchanged
COMPLETE_AND_CHECK? Complete and Checks for All Supported Tables
CUSTOMER_ADDRESS? Update Customer Address
CUSTOMER_BTE_1320? Trigger function module OPEN_FI_PERFORM_00001320_E
CUSTOMER_BTE_1321? Call Function Module OPEN_FI_PERFORM_00001321_E
CUSTOMER_BTE_1340? Call Function Module OPEN_FI_PERFORM_00001340_E
CUSTOMER_CHDOC_CREATE? Call Function Module DEBI_WRITE_DOCUMENT
CUSTOMER_INSERT? Call Function Module CUSTOMER_INSERT
PREPARE_GENERAL_DATA? Fill Tables with General Data
PREPARE_COMPANY_CODE_DATA? Fill Internal Tables of Company Code Data
PARTNER_ADDRESSES? Update Partner Addresses (KNVK)
MAINTAIN_TEXT? Create/Update SAPscript Text in Catalog. COMMIT_TEXT Needed
MAINTAIN_SINGLE? Maintain a Customer
MAINTAIN_DIRECT_INPUT? Manage Customer Master Data Using BAPI
MAINTAIN_BAPI? Manage Customer Master Data Using BAPI
MAINTAIN? Manage Customer Master Data
LOCK? Lock Customer
KNKK_INSERT? Generate CreditMgmt Master Data Entry in Credit Control Area
INITIALIZE? Initialize Global and Current Customer Buffer
GET_NUMBER? Get Customer Number
GET_LOCK? Read Lock Table
GET_KTOKD? Get Account Group Customer from Buffer and/or Database
GET_CRITICAL_ROLES? Determine Critical Roles
GET_CONTACT_NUMBER? Get contact person number
GET_ADDRESS_NUMBER? Get Address Number
CUSTOMER_UPDATE? Call Function Module CUSTOMER_UPDATE

Update Customer with CMD_EI_API Sample ABAP Coding

This sample covers updating customer’s contacts using the Class API CMD_EI_API.

  
  DATA: ls_knvp                   TYPE knvp.
  DATA: ls_master_data            TYPE cmds_ei_main,
        ls_master_data_correct    TYPE cmds_ei_main,
        ls_message_correct        TYPE cvis_message,
        ls_master_data_defective  TYPE cmds_ei_main,
        ls_message_defective      TYPE cvis_message.

  DATA: lt_customers              TYPE cmds_ei_extern_t,
        ls_customer               TYPE cmds_ei_extern,
        ls_sales_data             TYPE cmds_ei_cmd_sales,
        lt_sales                  TYPE cmds_ei_sales_t,
        ls_sales                  TYPE cmds_ei_sales,
        lt_functions              TYPE cmds_ei_functions_t,
        ls_function               TYPE cmds_ei_functions.


"it_relation contains the list of Contacts to uplaod 

 LOOP AT it_relation ASSIGNING .
    MOVE  TO ls_rel.

    AT NEW kunnr.

      CLEAR lv_vkorg.

      REFRESH : lt_functions, lt_sales.
      CLEAR: ls_customer, ls_sales_data.

      SELECT SINGLE vkorg FROM knvp
        INTO lv_vkorg
        WHERE kunnr = ls_rel-kunnr.

      ls_customer-header-object_instance-kunnr = ls_rel-kunnr.
      ls_customer-header-object_task  = 'U'. " I/U/D
      ls_sales_data-current_state = space.
      ls_sales-task = 'M' .
      ls_sales-data_key-vkorg = lv_vkorg.
      ls_sales-data_key-vtweg = '01'.
      ls_sales-data_key-spart = '01'.
      ls_sales-functions-current_state = space.

    ENDAT.

    CLEAR ls_function.
    " Suppression
    IF NOT ( sy-datum GE ls_rel-date_debut AND sy-datum LE ls_rel-date_fin ) .
      ls_function-task = 'D'. " I/U/D/M           
    ENDIF.

    IF ls_function-task NE 'D'.
      SELECT SINGLE * FROM knvp INTO ls_knvp
        WHERE kunnr = ls_rel-kunnr
        AND   ( kunn2 = ls_rel-kunn2 OR parnr = ls_rel-kunn2 ).
      IF sy-subrc NE 0 .
        ls_function-task = 'I'.
      ELSE.
        ls_function-task = 'U'.
      ENDIF.
    ENDIF.

    ls_function-data_key-parvw = ls_rel-parvw. 
    ls_function-data-partner   = ls_rel-kunn2. 
*    ls_function-datax-partner  = 'X'.          

    APPEND ls_function TO lt_functions.

    AT END OF kunnr.

      ls_sales-functions-functions = lt_functions[].
      APPEND ls_sales TO lt_sales.

      ls_sales_data-sales[]  = lt_sales[].
      ls_customer-sales_data = ls_sales_data.
      APPEND ls_customer TO lt_customers.
    ENDAT.

  ENDLOOP.

  ls_master_data-customers = lt_customers[].


  cmd_ei_api=>initialize( ).


  LOOP AT lt_customers INTO ls_customer.
    cmd_ei_api=>lock( iv_kunnr = ls_customer-header-object_instance-kunnr ).
  ENDLOOP.
  
  " Update Customer with CMD_EI_API
  CALL METHOD cmd_ei_api=>maintain_bapi
    EXPORTING
      iv_test_run              = space
      iv_collect_messages      = 'X'
      is_master_data           = ls_master_data
    IMPORTING
      es_master_data_correct   = ls_master_data_correct
      es_message_correct       = ls_message_correct
      es_master_data_defective = ls_master_data_defective
      es_message_defective     = es_message_defective.

  IF es_message_defective IS INITIAL.
    BREAK-POINT.
    COMMIT WORK.
  ELSE.
    BREAK-POINT.
  ENDIF.

  LOOP AT lt_customers INTO ls_customer.
    cmd_ei_api=>unlock( iv_kunnr = ls_customer-header-object_instance-kunnr ) .
  ENDLOOP.