Enterprise Territory - Auto Account Assignment using Apex

Enterprise Territory Management – Auto Account Assignment using Apex

Using Territory Management in Salesforce, we can auto assign territories to Account and give access to users working in common region. When we create Account, Territory Assignment rules automatically runs. On Account update we have a checkbox on page layout allowing to run Territory rules. However, Lightning experience does not support Territory Assignment checkbox yet on page layouts.

Unfortunately, Apex also does not support Account Assignment using Territory rules. Only way left is to use Salesforce API. Question is, just to run Enterprise Territory Management rules, should we import WSDL and convert it to respective Apex classes ? There must be some better way.

Then I remembered this article which I wrote around 5 years back. Instead of importing WSDL into Salesforce and converting it to Apex classes, we can take help of SOAP UI to find the request format and using HttpRequest, we can simulate request.

Below animation shows code in action :

Demo of Enterprise Territory Management Assignment rules using Apex in Salesforce
Demo of Enterprise Territory Management Assignment rules using Apex in Salesforce

How to get Salesforce SessionId in future Method, Batch Apex or any other Async Apex

Only challenge in invoking API from Queueable Interface is getting sessionId.

You might think that we can pass session Id directly to Queueable class/ It works in classic mode however I saw internal server error if user in Lightning experience mode. It works fine from anonymous Apex, but then fails if execution starts from Account Trigger.

For solution, Credit goes to @mattandneil for this Solution on twitter TIL series. We need to create a below Visualforce page to get sessionId and then use in our code. I tested this and works in both classic and Lightning.

Below code is self explanatory. Only catch was that we cannot get sessionId in asynchronous Apex and therefore we need to pass it as a parameter.

Posted

in

by


Related Posts

Comments

11 responses to “Enterprise Territory Management – Auto Account Assignment using Apex”

  1. Ashok Avatar
    Ashok

    Hi Jitendra,

    This worked, but the limit is only 200 records in a soap update transaction.

    There is also a limit on webservice callouts 100.

    we have more accounts to update in a single tranaction is there any alternative

    Thank you

    1. Mudassir Avatar
      Mudassir

      Hey Ashok,
      In just 5 record updates, I hit my sandbox API limits with this solution.
      Have you discovered a more scalable approach? Is this even possible

      1. Jitendra Avatar

        Can you share more detail about the error ?

        1. Roman Weinshenker Avatar
          Roman Weinshenker

          Hi
          There is no error but after say one time of usage you can see that api consumption is continuing as if you continue to work!)
          This way we got our API limit within couple of hours
          Also I found the solution built i believe on the same code: Auto Run Territory Assignment Rules by Trigg Digital
          When istalled it made the same problem
          https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3u00000Put08EAB&revId=a6H3u0000008acXEAQ

          Just cannot understand what is going on)

          1. Stephanie Avatar
            Stephanie

            If you use batch apex you shouldn’t run into this issue. You’d need something like this that then calls RunTerritoryRules:

            public class TerritoryAssignments implements Database.Batchable {

            public final String query = ‘SELECT Id FROM Account WHERE ‘;

            public Database.QueryLocator start(Database.BatchableContext BC) {
            return Database.getQueryLocator(query);
            }

            public void execute(Database.BatchableContext bc, List scope) {
            Set acctIds = new Set();
            for (Account acct : scope) {
            acctIds.add(acct.Id);
            }
            RunTerritoryRules job = new RunTerritoryRules();
            job.accntIds = acctIds;
            System.enqueueJob(job);
            }

            public void finish(Database.BatchableContext bc) {
            }
            }

            Still, it’s a huge pain that you have to do this. I feel like this should be in bold red lettering in the Salesforce documentation!

  2. Sumit Avatar
    Sumit

    it is not working

  3. omkarbajpai Avatar

    Hey there,
    Nice blog!

    Quite understandable and handy when it comes to practicality. we have a similar blog on other aspects of the subject
    check it out and tell me what Ayou think of it.a feedback is always appreciated

  4. Salesforce Practiioner Avatar
    Salesforce Practiioner

    It worked, thanks.

  5. Chaitra Avatar
    Chaitra

    @Jitendra : Is there a way to run all the rules of a territory model ?

  6. Mahesh Avatar
    Mahesh

    Hi Jitendra,

    If we remove an account from objectterritory2association is it possible to update account field?

    If yes,please let us know that would be helpful.

    Thanks!

  7. Priyanka Avatar
    Priyanka

    Hi Jitendra, where i can get test class for the RunTerritoryRules, Currently when i trying do write test class for the class Below Error i am getting.
    Methods defined as TestMethod do not support getContent call

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading