function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
moniferlomoniferlo 

Apex triggers and Territories

I have some Territory Assignment rules based on Account fields that are filled in by a trigger execution. The problem is that the assignment rules never apply in that way, but if the field is modified through Salesforce interface they are applied. I know that there is a SOAP header I have to use when inserting Accounts through API Web services in order to get assignment rules executed, but what about that in Apex?
I haven't found any reference that states that Apex doesn't trigger Territory Assignment rules, but my tests lead me to think so, and it is a very big problem in our org.
A_SmithA_Smith
Have you checked the "Run territory assignment rules on save by default" on the account page layout in setup?  You shouldn't need the header in this case.  Can you post your code?  I've tried this and it appears to work for me.

Thanks,
Andrew
moniferlomoniferlo
Yes, of course. I have checked 'Run territory assignment rules on save by default' in all page layouts defined for Account. I have a custom object Account Site related to Account through a master detail relationship. The Account Site object has some address fields that the user fills in, and every time any of these fields change they are propagated to custom Account address (there is a trigger on afterupdate and aftercreate for Account Site). In fact, I have configured custom Account address as not editable for standard profiles. My territory assignment rules evaluates Country (Account address field) to assign the Account to
the right territory. When user changes Country in Account Site it gets updated in Account (the trigger fires correctly), but the Territory in the Account has not been assigned. If I log in as an admin user and click Edit button and then Save without changing anything, the territory is now selected based on new Country.
DuTomDuTom
Did you ever resolve this issue?  My issue is not caused by a trigger but from an Update to Account via Apex code.  On this update I need to have the account territory refreshed.
 
 
Federico LarsenFederico Larsen

I am having the same problem. In the sandbox works OK, but when I passed to production it does not work.

Did you find a workaround to this issue?

 

Best regards and thanks in advance for any comment about it.

moniferlomoniferlo
Territory rules don't reevaluate when you update a record through Apex code, they don't work either if you update the fields involved in the territory assignment rules using Line Editing. In Spring09 a solution for something similar was released for Lead and Case Assignment rules when you create a record using Apex code. I hope this is fixed some day.
Federico LarsenFederico Larsen

Thanks for the quick response.

 

Actually, my problem is in the Convert Lead operation. In the convert lead I map a field from the lead to the Account, and  I have Territory Account assignment rules based on that field.

And even thought the field is mapped ok, the rule does not run.

 

Best regards.-

 

moniferlomoniferlo
That should work because it works for us (territory assignment rules are executed) we call the lead convert function in Apex code and we map lead fields to account fields using the standard way