We are using Territory Management in our org and the challenge is to trigger processing of standard account assignment rules from Apex code. Currently we are doing this manually (from Setup -> Territory Hierarchy/Settings). So basically we want to automate clicking at the following button on UI: enter image description here

Unfortunatelly cannot find appropriate method neither in Apex nor in external APIs - rest, meta, tooling. Any suggestions would be appreciated.

  • What's the use case for doing this? The Run Rules button is mostly intended to be used to recalculate after rules are created or updated, or when records get out of sync for some reason. You wouldn't want to automate this on a regular basis because of the performance implications. – sfdcfox May 11 '18 at 20:02
  • @sfdcfox We have sort of twisted solution for territory management (custom extension on top of standard one). And to keep them in sync, when rules recalculation is triggered in custom part, the same action should be propagated to standard solution, so that they both would do their part of the job. Currently it's a manual step, but users are constantly forgetting to go and trigger standard recalculation as well, creating inconsistency at data level this way. – wesaw May 11 '18 at 21:02
  • @sfdcfox From the performance perspective, this wouldn't happen very often, i.e. couple times per day I guess. And we are limiting recalculation to specific territory (and its descendants), not for all global scope. – wesaw May 11 '18 at 21:05

From Territory Management: running territory assignment rules on Account edit:

As of version 21 of the Force.com Apex Code Developer's Guide, there is not a way to run territory assignment rules through Apex: "assignmentRuleHeader method - The database.DMLOptions object supports assignment rules for cases and leads, but not for accounts or territory management."

The only option as of API version 21 is to use the Web Services API. See AssignmentRuleHeader in the Web Services API Developer Guide for more information:

"AssignmentRuleHeader – useDefaultRule - If true for a Case or Lead, uses the default (active) assignment rule for a Case or Lead. If specified, do not specify an assignmentRuleId. If true for an Account, all territory assignment rules are applied, and if false, no territory assignment rules are applied."

A description of the API.

Heres the relevant trailhead.

Looks like both the SOAP API and the REST API expose the field you need.

| improve this answer | |
  • I need to trigger processing of assignment rules for specific territory, meaning that all existent accounts should be considered to figure out whether they match rules from that territory or not. What you are suggesting, if I got it right, is more like "trigger assignment rule when this account has been created", i.e. reverse problem that the one I'm trying to solve. Please correct me if I'm wrong. – wesaw May 11 '18 at 19:45
  • We could imagine this like I already missed mass accounts creation step, and now I need to force them being assigned to correct territory afterwards. – wesaw May 11 '18 at 20:01
  • You should be able to set this header on update operations, allowing you to run your territory rules for an existing record. – battery.cord May 12 '18 at 15:14

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.