Updating Territory in Business Transactions
I came across this scenario some time back where in the production system , we needed to update territories based on custom logic for existing opportunities. Actually the Territories were getting determined based on employee responsible while the logic needed to be based on sales organization and sold-to-party details. For this we created a program with input parameters – Transaction Type or Actual Object id for which territory updation needs to be done. But the question was how to update the territory data into business transactions.
The solution was much simpler than i thought. Actually if you check the partner Assignment Block in WebUI , the territory details are present there for employee responsible.The following steps and code snippets would help to understand better
- The partner details could be easily from one order FM CRM_ORDER_READ by passing requested objects as partner.
insert ls_oppt-guid into table lt_header_guid. insert gc_object_name-orgman into table lt_req_objects. insert gc_object_name-partner into table lt_req_objects. * Read the Orgman and Partner Details clear: lt_orgman , lt_partner. call function 'CRM_ORDER_READ' exporting it_header_guid = lt_header_guid iv_mode = gc_mode-display it_requested_objects = lt_req_objects importing et_orgman = lt_orgman 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.
- Retrieve the Sold-to-party details from the Partner. Remember to provide your own partner function in constant lc_partner_fct.
read table lt_partner into ls_partner with key partner_fct = lc_partner_fct.
- Prepare your criteria for retrieving the Territory Details. In my case the criteria was based on Sales Area and Sold-to-party
read table lt_orgman into ls_orgman index 1. clear: ls_sales_area, lt_sales_area. ls_sales_area-dis_channel = ls_orgman-dis_channel. ls_sales_area-division = ls_orgman-division. ls_sales_area-sales_org = lv_r3_sales_org. append ls_sales_area to lt_sales_area. * Validate the Partner and Prepare for fetching the Territory Details clear lv_partner_guid. call function 'CONVERSION_EXIT_ALPHA_INPUT' exporting input = ls_partner-partner_no importing output = lv_partner_id. clear lv_partner_guid. select single partner_guid from but000 into lv_partner_guid where partner = lv_partner_id . lv_guid = lv_partner_guid. clear : lt_terrstruct , lt_terrstruct_indirect. call function 'CRM_TERRMAN_GET_TERR_BY_BUPA' exporting iv_partner_guid = lv_guid it_sales_area = lt_sales_area importing et_terrstruct_direct = lt_terrstruct et_terrstruct_indirect = lt_terrstruct_indirect exceptions internal_error = 1 others = 2.
- You can retrieve the territory id in either of lt_terrstruct ( or lt_terrstruct_indirect ).
clear: ls_terrstruct , lv_path_id.
read table lt_terrstruct into ls_terrstruct index 1.
lv_path_id = ls_terrstruct-path_id.
- Fill the structure to maintain territory details
clear: ls_partner_attribute, lt_partner_attributes. ls_partner_attribute-ref_guid = ls_oppt-guid. ls_partner_attribute-ref_kind = 'A'. ls_partner_attribute-ref_partner_fct = '00000014'. ls_partner_attribute-ref_partner_no = ls_partner_empresp-partner_no. ls_partner_attribute-ref_no_type = 'BP'. ls_partner_attribute-ref_display_type = 'BP'. ls_partner_attribute-semantic_key = 'TERRITORY'. ls_partner_attribute-fieldname = 'PATH_ID'." 'PATH_GUID'. ls_partner_attribute-value = ls_terrstruct-path_id." append ls_partner_attribute to lt_partner_attributes .
- Finally call the order maintain and order save FMs of the one order
call function 'CRM_ORDER_MAINTAIN' changing ct_partner_attributes = lt_partner_attributes exceptions error_occurred = 1 document_locked = 2 no_change_allowed = 3 no_authority = 4 others = 5. call function 'CRM_ORDER_SAVE' exporting it_objects_to_save = lt_header_guid importing et_saved_objects = lt_saved_objects exceptions document_not_saved = 1 others = 2. call function 'BAPI_TRANSACTION_COMMIT'. * Initailize the Transaction call function 'CRM_ORDER_INITIALIZE' exporting it_guids_to_init = lt_header_guid iv_keep_lock = ''.
Now you can find that territory would be updated.