2

We have an issue with Accounts in wrong territories.

Basically, we have an apex controller for Cases which modifies the account field that we are using to manage territories, but reading this link https://help.salesforce.com/apex/HTViewHelpDoc?id=territories_rules.htm&language=en I can see that apex code does not execute the territory rules, so I would need a right solution to solve this.

Ideas?

  • Like you said We can execute territory rule from Apex.. So you need to use SOAP API.. using SOAP API update those Account that will fire the territory rules.. check this ans salesforce.stackexchange.com/questions/46761/… – Ratan Paul Jan 29 '16 at 11:48
  • Sorry we can't run territory rule from apex – Ratan Paul Jan 29 '16 at 12:29
  • So, no way to auto assign the account to the right territory? Using SOAP API in Apex code seems a dirty trick – Javier Vazquez Jan 29 '16 at 14:15
  • using apex no.. Looks bad but in current situation this is the only solution.. there is an idea exchange post to run territory rules in apex. you can vote that idea. – Ratan Paul Jan 29 '16 at 14:58
  • 1
    Well, thank you Ratan, the solutions works perfectly. If you write the answer i will accept it in order to more people can read and use it. – Javier Vazquez Feb 2 '16 at 12:08
4

**As we all know. Currently we can't run territory assignment rule from apex. But We can use SOAP API for this requirement

You need to use SOAP API for firing the territory management rule. You can call pass the same org(which one you are using) credentials.

Use this class TerritoryMangRule.fireTerritoryManagementViaSoap(Pass list of Account here).

Follow the steps in link to generate WSDL https://developer.salesforce.com/page/Apex_Web_Services_and_Callouts

So Once trigger is fired call a future method from there execute the webservice and pass the All Account.

global class TerritoryMangRule
{    
    webService static void fireTerritoryManagementViaSoap(List<Account> lstAccountToUpdate) 
    { 
        partnerSoapSforceCom.Soap obj = new partnerSoapSforceCom.Soap();
        partnerSoapSforceCom.LoginResult loginResult = obj.login('org@username.com', 'Password');   
        obj.SessionHeader = new partnerSoapSforceCom.SessionHeader_element();
        obj.endpoint_x =loginResult.ServerUrl;
        obj.Sessionheader.sessionid = loginResult.sessionid;
        List<sobjectPartnerSoapSforceCom.sObject_x> lst = new List<sobjectPartnerSoapSforceCom.sObject_x>();
        for(Account ac:lstAccountToUpdate)
        {
            sobjectPartnerSoapSforceCom.sObject_x tmpObj = new sobjectPartnerSoapSforceCom.sObject_x();
            tmpObj.type_x = 'Account';
            tmpObj.Id = ac.Id;
            lst.add(tmpObj);
        }
        partnerSoapSforceCom.AssignmentRuleHeader_element  obj1 = new partnerSoapSforceCom.AssignmentRuleHeader_element();
        obj1.useDefaultRule = true;
        obj.AssignmentRuleHeader = obj1;
        partnerSoapSforceCom.SaveResult[] lst1 =obj.update_x(lst);
    } 
}

We need to just perform dummy update(Including set AssignmentRuleHeader) via SOAP API. It will run the Territory management rule.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.