How to Update SAP Product Hierarchy Category on SAP CRM with full ABAP Code

SAP CRM Catalog e1461269538601

How to Update Product Hierarchy & Category on SAP CRM will guide through updating (Creation, Updating and Deletion) of SAP CRM Product Category using ABAP Code.

SAP CRM Product Hierarchy and Category

In this section, the technical information of the SAP CRM Hierarchy and SAP CRM Category is explained.

For SAP CRM Hierarchy, the main SAP CRM Table is : COMM_HIERARCHY for Hierarchy and the main fields are:

  • HIERARCHY_GUID: Category Hierarchy GUID
  • HIERARCHY_ID : Category Hierarchy ID

For SAP CRM Category, the main Table is COMM_CATEGORY for Category and the main fields are:

  • CATEGORY_GUID : Category GUID
  • CATEGORY_ID : Category ID
  • HIERARCHY_GUID : Category Hierarchy GUID
  • PARENT_GUID : Parent Category GUID.

The First step is retrieve the corresponding GUIDs for CATEGORY_ID and HIERARCHY_ID.

SAP CRM Product Category and Hierarchy BAPI

The Two main BAPI to use when dealing with Update Product Category are:

  • COM_PROD_CAT_REL_MAINTAIN_READ
    • Read All the Product Categories for a SAP CRM Product
  • COM_PROD_CAT_REL_MAINTAIN_UPD
    • Update the Product Categories of CRM Product.

Update SAP CRM Product Category ABAP Snippet

This ABAP Code will handle the job to assign SAP CRM Product Categories to a SAP CRM Product.
You can handle the deletion and update with the flag of ls_set_maintainupdate_type (D for Deletion and U for Update).

" Data Declaration for Assigning Product Category to Product
DATA: lt_set_maintain_old         TYPE comt_prod_cat_rel_maintain_tab.
DATA: ls_set_maintain_old         LIKE LINE OF lt_set_maintain_old.
DATA: lt_set_maintain             TYPE comt_prod_cat_rel_maintain_tab.
DATA: ls_set_maintain             LIKE LINE OF lt_set_maintain.
DATA: ls_set_maintain_data        TYPE comt_prod_cat_rel.
DATA: lt_bapiret                  TYPE bapiret2_tab.

DATA: lv_product_guid             TYPE product_guid.
DATA: lv_new_category_guid        TYPE comt_category_guid.
DATA: lv_new_category_id          TYPE comt_category_id.
DATA: lv_new_hierarchy_guid       TYPE comt_hierarchy_guid.

"@ TO BE DONE : Calculate the new category id & Guid and the SAP CRM Product Category Hierarchy 
"@ Tables to use: COMM_HIERARCHY and COMM_CATEGORY 

" Prepare SAP CRM Product Maintain
CALL FUNCTION 'COM_PRODUCT_MAINTAIN_READ'
  EXPORTING
    iv_product_guid = lv_product_guid
  EXCEPTIONS
    not_found       = 1
    internal_error  = 2
    OTHERS          = 3.
IF sy-subrc = 0.

  Read SAP CRM Assigned Categories
  CALL FUNCTION 'COM_PROD_CAT_REL_MAINTAIN_READ'
    EXPORTING
      iv_product_guid = lv_product_guid
    IMPORTING
      et_set_maintain = lt_set_maintain_old
    EXCEPTIONS
      not_found       = 1
*     WRONG_CALL      = 2
      OTHERS          = 3.
  IF sy-subrc = 0.

    "@ TO BE DONE : Calculate if it is D/U : Deletion or Updating  
    ls_set_maintain-update_type = 'U'. " U works for Update Product Category or Insert 

    Ls_set_maintain_data-category_guid  =  lv_new_category_guid.
    Ls_set_maintain_data-hierarchy_guid =  lv_new_hierarchy_guid.
    Ls_set_maintain_data-product_guid   =  lv_product_guid.
    Ls_set_maintain_data-category_id    =  lv_new_category_id.
    Ls_set_maintain_data-logsys         =  lv_logsys.
    Ls_set_maintain-data                =  ls_set_maintain_data.
    
    "Update Product Category 
    CALL FUNCTION 'COM_PROD_CAT_REL_MAINTAIN_UPD'
      EXPORTING
        iv_product_guid = lv_product_guid
        iv_update_type  = ls_set_maintain-update_type
        it_set_maintain = lt_set_maintain
      IMPORTING
        et_bapireturn   = lt_bapiret
      EXCEPTIONS
        internal_error  = 1
        recateg_failed  = 2
        OTHERS          = 3.
    IF sy-subrc = 0.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
    ENDIF.
  ENDIF.
ENDIF.

Check more detail. Hierarchy about Assign Product Categories to Products.