Insert SAP CRM Product Relations with BAPI Fuction



User Rating:  / 5
PoorBest 
Details
Category: ABAP
Created on Wednesday, 30 January 2013 09:11
Last Updated on Wednesday, 30 January 2013 09:11
Published on Wednesday, 30 January 2013 09:11
Written by Administrator
Hits: 14402

The following simple code inserts SAP CRM Product Relations ( using BAPI Function ) :

View source
PROGRAM zins_pr_rel.
 
tables : comm_product.
 
**********************************************************************
*
* www.developerpages.gr
*
* Insert CRM Product Relations
*
**********************************************************************
 
 
data : begin of itab_rel occurs 100,
   PRODUCT_ID like comm_product-product_id,
   PRODUCT_ID_REL like comm_product-product_id.
data : end of itab_rel.
 
data : begin of pr_rel_list occurs 100,
        product_id type comm_product-product_id.
data : end of pr_rel_list.
 
data : i_ASSOCRULESOURCE like BAPIBUS20200_SOURCE_EXT
                  occurs 0 with header line,
      i_ASSOCRULERESULT like BAPIBUS20200_RESULT_EXT
                  occurs 0 with header line,
 
      i_ASSOCRULEHEADER like BAPIBUS20200_HEADER_EXT
                   occurs 0 with header line,
       i_RETURN like BAPIRET2
                  occurs 0 with header line.
 
* fill internal table  pr_rel_list
PERFORM fill_itab_rel.
 
* fill internal table  pr_rel_list
PERFORM  fill_pr_rel_list.
 
* Insert Relations
perform insert_product_relations.
 
*&---------------------------------------------------------------------*
*&      Form  insert_relations
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form insert_product_relations.
  data : l_sequence type i.
 
  loop at pr_rel_list.
    refresh i_return. clear i_return.
    refresh i_ASSOCRULESOURCE.
    refresh i_ASSOCRULERESULT.
    refresh i_ASSOCRULEHEADER.
 
    select single * from comm_product where product_id = pr_rel_list-product_id.
    if sy-subrc = 0.
      i_ASSOCRULEHEADER-OBJTYPE = '01'.
      i_ASSOCRULEHEADER-RTYPE = '1'.
      i_ASSOCRULEHEADER-STATE = '1'.
      i_ASSOCRULESOURCE-product_guid = comm_product-product_guid.
      append i_ASSOCRULESOURCE.
      l_sequence = 1.
      loop at itab_rel where product_id = pr_rel_list-product_id.
        add 1 to l_sequence.
        select single * from comm_product where product_id = itab_rel-product_id_rel.
        if sy-subrc = 0.
          i_ASSOCRULERESULT-PRODUCT_GUID = comm_product-product_guid.
          unpack l_sequence to i_ASSOCRULERESULT-sequence.
          append i_ASSOCRULERESULT.
          write :/ 'Add Rel Product : ', itab_rel-product_id_rel.
        else.
          write :/ 'Rel Product : ', itab_rel-product_id_rel, ' not found !'.
        endif.
      endloop.
 
      refresh i_return. clear i_return.
 
      CALL FUNCTION 'BAPI_PR_AR_CREATEFROMDATA'
        EXPORTING
          ASSOCRULEHEADER       = i_ASSOCRULEHEADER
          TESTRUN               = ''
* IMPORTING
*   ASSOCIATIONGUID       =
        TABLES
          ASSOCRULESOURCE       = i_ASSOCRULESOURCE
          ASSOCRULERESULT       = i_ASSOCRULERESULT
          RETURN                = i_return.
 
      write :/ 'Product : ', pr_rel_list-product_id.
      loop at i_return.
        write :/ i_return-MESSAGE.
      endloop.
 
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          WAIT = 'X'.
    else.
      write :/ 'Product : ', pr_rel_list , ' cannot found !'.
    endif.
 
  endloop.
 
endform.                    "insert_relations
 
 
*&---------------------------------------------------------------------*
*&      Form  FILL_PR_REL_LIST
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FILL_itab_rel .
 
 
* fill table PR_REL_LIST from database table or file
 
 
ENDFORM.                    " FILL_PR_REL_LIST
 
 
*&---------------------------------------------------------------------*
*&      Form  FILL_PR_REL_LIST
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FILL_PR_REL_LIST .
 
 
* fill table PR_REL_LIST from database table or file PR_REL_LIST
 
 
ENDFORM.                    " FILL_PR_REL_LIST