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 :
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.
Leave a Reply