{"id":6274,"date":"2017-09-22T22:16:24","date_gmt":"2017-09-22T22:16:24","guid":{"rendered":"http:\/\/www.jitendrazaa.com\/blog\/?p=6274"},"modified":"2022-07-18T11:18:40","modified_gmt":"2022-07-18T15:18:40","slug":"enterprise-territory-management-auto-account-assignment-using-apex","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/enterprise-territory-management-auto-account-assignment-using-apex\/","title":{"rendered":"Enterprise Territory Management &#8211; Auto Account Assignment using Apex"},"content":{"rendered":"<p style=\"text-align: justify;\">Using\u00a0<a href=\"https:\/\/resources.docs.salesforce.com\/206\/latest\/en-us\/sfdc\/pdf\/salesforce_implementing_territory_mgmt2_guide.pdf\"><strong>Territory Management<\/strong><\/a>\u00a0in Salesforce,<strong>\u00a0<\/strong>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,\u00a0<strong>Lightning experience\u00a0<\/strong>does not support Territory Assignment checkbox yet on page layouts.<\/p>\n<p style=\"text-align: justify;\">Unfortunately, Apex also does not support Account Assignment using Territory rules. Only way left is to use <a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.api.meta\/api\/sforce_api_header_assignmentruleheader.htm\">Salesforce API<\/a>. Question is, just to run\u00a0<strong>Enterprise Territory Management\u00a0<\/strong>rules, should we import WSDL and convert it to respective Apex classes ? There must be some better way.<\/p>\n<p style=\"text-align: justify;\">Then I remembered <a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/getting-record-from-one-salesforce-organization-to-other\/\">this article<\/a> which I wrote around 5 years back. Instead of importing WSDL into Salesforce and converting it to Apex classes, we can take help of <a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/using-soapui-with-salesforce-to-test-standard-and-custom-web-services-response\/\"><strong>SOAP UI<\/strong><\/a> to find the request format and using\u00a0<strong>HttpRequest,\u00a0<\/strong>we can simulate request.<!--more--><\/p>\n<p>Below animation shows code in action :<\/p>\n<figure style=\"width: 968px\" class=\"wp-caption aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-large\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Account-Assignment-Rule.gif?resize=968%2C784&#038;ssl=1\" alt=\"Demo of Enterprise Territory Management Assignment rules using Apex in Salesforce\" width=\"968\" height=\"784\" \/><figcaption class=\"wp-caption-text\">Demo of Enterprise Territory Management Assignment rules using Apex in Salesforce<\/figcaption><\/figure>\n<h3>How to get Salesforce SessionId in future Method, Batch Apex or any other Async Apex<\/h3>\n<p style=\"text-align: justify;\">Only challenge in invoking API from Queueable Interface is getting sessionId.<\/p>\n<p style=\"text-align: justify;\">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.<\/p>\n<p style=\"text-align: justify;\">For solution, Credit goes to <a href=\"https:\/\/twitter.com\/mattandneil\">@mattandneil<\/a> 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.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/JitendraZaa\/cc3cdb80e97d0cde197511a806b4dfab.js\"><\/script><\/p>\n<p style=\"text-align: justify;\">Below code is self explanatory. Only catch was that we cannot get <strong>sessionId<\/strong> in asynchronous Apex and therefore we need to pass it as a parameter.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/JitendraZaa\/8a6704b11c43436d72b3d16a9ed93749.js\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use Apex code to auto assign Accounts on basis of Enterprise Territory Assignment rules<\/p>\n","protected":false},"author":1,"featured_media":6275,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9],"tags":[315,426],"class_list":["post-6274","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","tag-soapui","tag-territory-management"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Enterprise-Territory-Auto-Account-Assignment-using-Apex.jpg?fit=900%2C600&ssl=1","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":6777,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/enterprise-territory-management-key-notes\/","url_meta":{"origin":6274,"position":0},"title":"Enterprise Territory Management &#8211; Key Notes","author":"Jitendra","date":"February 11, 2019","format":false,"excerpt":"How to use Apex based sharing to share opportunity using Territories","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Territory Management - Data Model","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/02\/Territory-Management-Data-Model.png?fit=1160%2C760&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/02\/Territory-Management-Data-Model.png?fit=1160%2C760&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/02\/Territory-Management-Data-Model.png?fit=1160%2C760&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/02\/Territory-Management-Data-Model.png?fit=1160%2C760&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/02\/Territory-Management-Data-Model.png?fit=1160%2C760&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":31119,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-mobile-offline-complete-guide-2026-briefcase-2\/","url_meta":{"origin":6274,"position":1},"title":"Salesforce Mobile Offline Complete Guide 2026 | Briefcase Builder, LWC Offline &amp; Best Practices","author":"Jitendra Zaa","date":"January 15, 2026","format":false,"excerpt":"Master Salesforce Mobile Offline in 2026: Complete guide covering Briefcase Builder, LWC Offline, Mobile App Plus, data priming, sync, and governor limits. Learn offline architecture, conflict resolution, and best practices with official Salesforce sources.","rel":"","context":"In &quot;Experience&quot;","block_context":{"text":"Experience","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/experience\/"},"img":{"alt_text":"Salesforce Mobile Offline Complete Guide 2026 - Featured Image","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2026\/01\/Salesforce-Mobile-Offline-Complete-Guide-featured.png?fit=1200%2C675&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2026\/01\/Salesforce-Mobile-Offline-Complete-Guide-featured.png?fit=1200%2C675&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2026\/01\/Salesforce-Mobile-Offline-Complete-Guide-featured.png?fit=1200%2C675&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2026\/01\/Salesforce-Mobile-Offline-Complete-Guide-featured.png?fit=1200%2C675&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2026\/01\/Salesforce-Mobile-Offline-Complete-Guide-featured.png?fit=1200%2C675&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":5953,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/sfdcrules-simple-yet-powerful-rule-engine-for-salesforce\/","url_meta":{"origin":6274,"position":2},"title":"SFDCRules &#8211; Simple yet powerful Rule Engine for Salesforce","author":"Jitendra","date":"March 28, 2017","format":false,"excerpt":"Open source project to evaluate rules in Salesforce using Apex","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"SFDC Rule Engine","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/03\/SFDCRules.jpg?fit=1200%2C524&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/03\/SFDCRules.jpg?fit=1200%2C524&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/03\/SFDCRules.jpg?fit=1200%2C524&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/03\/SFDCRules.jpg?fit=1200%2C524&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/03\/SFDCRules.jpg?fit=1200%2C524&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":1305,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-3\/","url_meta":{"origin":6274,"position":3},"title":"Salesforce Interview Questions \u2013 Part 3","author":"Jitendra","date":"October 12, 2010","format":false,"excerpt":"Most Frequently Asked interview questions of Apex, Visual force, SOQL in Salesforce.com SFDC","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5949,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/state-country-picklist-in-salesforce-supporting-alpha-3-iso-code\/","url_meta":{"origin":6274,"position":4},"title":"State &#038; Country picklist in Salesforce supporting Alpha-3 ISO code","author":"Jitendra","date":"March 22, 2017","format":false,"excerpt":"How to use Integration Value in State & Country picklist in Salesforce","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"State & Country Picklist in Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/03\/State-Country-Picklist-in-Salesforce-1024x299.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/03\/State-Country-Picklist-in-Salesforce-1024x299.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/03\/State-Country-Picklist-in-Salesforce-1024x299.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/03\/State-Country-Picklist-in-Salesforce-1024x299.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":3025,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-11\/","url_meta":{"origin":6274,"position":5},"title":"Salesforce Interview Questions \u2013 Part 11","author":"Jitendra","date":"August 19, 2012","format":false,"excerpt":"101. How to force lead assignment rule via Apex while updating or adding the Lead? Ans : To enforce Assignment Rules in Apex you will need to perform following steps: Instantiate the \"Database.DMLOptions\"\u009d class. Set the \"useDefaultRule\"\u009d property of \"assignmentRuleHeader\"\u009d to True. Finally call a native method on your Lead\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Pagination in SOQL using keyword Offset","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Pagination-in-SOQL-using-keyword-Offset.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Pagination-in-SOQL-using-keyword-Offset.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Pagination-in-SOQL-using-keyword-Offset.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/6274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=6274"}],"version-history":[{"count":9,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/6274\/revisions"}],"predecessor-version":[{"id":7612,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/6274\/revisions\/7612"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media\/6275"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=6274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=6274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=6274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}