Customer Relationship Management Blogs by Members
Find insights on SAP CRM and other customer relationship management products and topics in blog posts from community members and share your own perspective.
cancel
Showing results for 
Search instead for 
Did you mean: 
Private_Member_3759
Participant


Whenever there is requirement for changing the parties involved in contract or to create a new business partner, we need to use appropriate value of logical key that plays an important part in maintaining the partners.

I would like to share some piece of code that will help in updation and creation of parties involved.

First of all use CRM_ORDER_READ to read the existing contract and check if partners already exist so that we will be able to update it.

DATA: lt_header                     TYPE crmt_object_guid_tab,
          lt_orderadm_h              TYPE crmt_orderadm_h_wrkt,
          ls_orderadm_h             TYPE crmt_orderadm_h_wrk,

          lt_partner                     TYPE  crmt_partner_external_wrkt,
          ls_partner                    TYPE  crmt_partner_external_wrk,
          lt_com_orderadm_h      TYPE  crmt_orderadm_h_comt,
          ls_com_orderadm_h     TYPE  crmt_orderadm_h_com,

          lt_com_partner             TYPE  crmt_partner_comt,
          ls_com_partner            TYPE  crmt_partner_com,

          lt_input_fields               TYPE  crmt_input_field_tab,
          ls_input_fields              TYPE  crmt_input_field,

          lt_saved_objects          TYPE crmt_return_objects,
          lt_objects_not_saved    TYPE  crmt_object_guid_tab,
          cv_log_handle              TYPE  balloghndl,
          lt_exception                 TYPE  crmt_exception_t,

          lv_tmp                         TYPE char4..

**** Pass guid of the existing contract to lt_header******

   CALL FUNCTION 'CRM_ORDER_READ'

   EXPORTING
      it_header_guid                    = lt_header
    IMPORTING
      et_orderadm_h                    = lt_orderadm_h
       et_partner                          = lt_partner
      EXCEPTIONS         
      document_not_found            = 1
      error_occurred                     = 2
      document_locked                = 3
      no_change_authority            = 4
      no_display_authority            = 5
      no_change_allowed             = 6
      OTHERS                            = 7.

****** Read table lt_orderadm_h********

   READ TABLE lt_orderadm_h INTO ls_orderadm_h INDEX 1.
  IF sy-subrc EQ 0.
    MOVE-CORRESPONDING ls_orderadm_h TO ls_com_orderadm_h.
    INSERT ls_com_orderadm_h INTO TABLE lt_com_orderadm_h.

  ENDIF.

  

    READ TABLE  lt_com_partner INTO ls_com_partner WITH KEY partner_no = 'X' partner_fct = 'Y'.

                                                                                 " X = partner number Y = partner function  
    IF sy-subrc NE 0.
      PERFORM maintain_header_partner USING 'X' 'Y'. ****  updating the parties involved using partner number and partner function**********  

    ENDIF.

  

********** After creation of input fields, we will use FM to maintain the changes***********

   CALL FUNCTION 'CRM_ORDER_MAINTAIN'
    EXPORTING
      it_partner                        = lt_com_partner
    IMPORTING
      et_exception                   = lt_exception
    CHANGING
      ct_orderadm_h                = lt_com_orderadm_h
      ct_input_fields                 = lt_input_fields
      cv_log_handle                 = cv_log_handle
          EXCEPTIONS
      error_occurred                 = 1
      document_locked            = 2
      no_change_allowed         = 3
      no_authority                   = 4
      OTHERS                        = 5.

**********If no exception found then save the transaction**************

If lt_exception is INITIAL.

   CALL FUNCTION 'CRM_ORDER_SAVE'
      EXPORTING
        it_objects_to_save   = lt_header
      IMPORTING
        et_saved_objects     = lt_saved_objects
        et_exception            = lt_exception
        et_objects_not_saved = lt_objects_not_saved
      EXCEPTIONS
        document_not_saved   = 1
        OTHERS               = 2.

ENDIF.

********* Commit the transaction**********

IF lt_saved_objects IS NOT INITIAL.

   CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.
********* Deque the transaction*************
      CALL FUNCTION 'CRM_ORDER_DEQUEUE'
        EXPORTING
          iv_guid = ls_saved_objects-guid.

ENDIF.

FORM  maintain_header_partner USING lv_partner_no  TYPE crmt_partner_number
                                    lv_partner_fct TYPE crmt_partner_fct.
 

  ls_com_partner-ref_guid      = ls_orderadm_h-guid.
  ls_com_partner-ref_kind      = 'A'.
  ls_com_partner-partner_no    = lv_partner_no.
  ls_com_partner-display_type  = 'BP'.
  ls_com_partner-no_type       = 'BP'.

  REFRESH lt_field_names.
  READ TABLE lt_partner INTO ls_partner WITH KEY partner_fct = lv_partner_fct
                                                                              ref_guid = ls_orderadm_h-guid.
  IF sy-subrc EQ 0.
***Change Partner
    ls_com_partner-ref_partner_handle = '0000'.
    ls_com_partner-ref_partner_no       = ls_partner-partner_no.
    ls_com_partner-ref_partner_fct       = ls_partner-partner_fct.
    ls_com_partner-ref_no_type           = ls_partner-no_type.
    ls_com_partner-ref_display_type     = 'BP'.

****** logical key for update is a combination of ref_partner_handle that contains the value of partner to be updated, ref_partner_function , ref_partner_fct that is to be changed ,
ref_display_type and ref_no_type.***************************
                              

     CONCATENATE ls_com_partner-ref_partner_handle 

           INTO ls_input_fields-logical_key.
           ls_input_fields-logical_key+12(20) = ls_com_partner-ref_partner_no.

    CONCATENATE ls_com_partner-ref_display_type
            ls_com_partner-ref_no_type
            INTO lv_tmp.

    ls_input_fields-logical_key+32(4) = lv_tmp.
  ELSE.
***Create Partner
    ls_com_partner-partner_fct    = lv_partner_fct.
    ls_com_partner-kind_of_entry  = 'C'.

    ls_input_fields-logical_key  '0000'. "for create it is simply four zeros.

    ls_field_names-fieldname = 'KIND_OF_ENTRY'.
    INSERT ls_field_names INTO TABLE lt_field_names.

    ls_field_names-fieldname = 'PARTNER_FCT'.
    INSERT ls_field_names INTO TABLE lt_field_names.
  ENDIF.

  INSERT ls_com_partner INTO TABLE lt_com_partner.

  IF NOT lt_com_partner IS INITIAL.
    ls_field_names-fieldname = 'PARTNER_NO'.
    INSERT ls_field_names INTO TABLE lt_field_names.

    ls_field_names-fieldname = 'DISPLAY_TYPE'.
    INSERT ls_field_names INTO TABLE lt_field_names.

    ls_field_names-fieldname = 'NO_TYPE'.
    INSERT ls_field_names INTO TABLE lt_field_names.

    ls_input_fields-ref_guid = ls_orderadm_h-guid.
    ls_input_fields-ref_kind = 'A'.
    ls_input_fields-objectname = 'PARTNER'.

    ls_input_fields-field_names = lt_field_names.
    INSERT ls_input_fields INTO TABLE lt_input_fields.
  ENDIF.

ENDFORM.