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
Matt O'HaraMatt O'Hara 

Account Territory update after custom field update - How To

I'm new to development but have been watching Intro to Apex webinars and I think I have a good foundation to write my first Trigger. Before I get started I'd like to make sure that I'm heading in the right direction.

I have a number of fields on Accounts that are updated via a Process when the Billing Address Postal/Zipcode field changes. Rep1, Rep2, Rep3, etc. This works fine. I need a Trigger that updates the Territories. The Territory rules just look at the field values for Rep1, Rep2, Rep3, etc.

I think that I'm tring to do an "after update, after insert" Trigger, but I don't know what I'm trying to update. Is it AccountShare?

Thanks in advance!
Andy BoettcherAndy Boettcher
Hi Matt!

AccountShare handles record visibility (basically the "Sharing" button) - but if you're talking actual Salesforce Territory Management, are you looking for a way to force the Territory rules to recalculate?
Matt O'HaraMatt O'Hara
Yes exactly, I need to force the Territory Rules to recalculate, but can it be done for just a single (or multiple, specified) Account(s)? I want to make what happens when you manually edit a record in the UI happen because the field updates are not triggering the Territory update.
Andy BoettcherAndy Boettcher
Matt,

Well, that's a bit tricky.  =)  There isn't a way in straight APEX to force those to recalculate.  You'll need to invoke the SOAP API to make this happen - check out this post:  https://developer.salesforce.com/forums/ForumsMain?id=906F00000008vLTIAY
Matt O'HaraMatt O'Hara
That seems to conflict with what Salesforce is talking about here
https://help.salesforce.com/apex/HTViewSolution?id=000006792&language=en_US

Does that change things at all?
Andy BoettcherAndy Boettcher
Yes and no - you cannot force a recalc of the Territory rules via APEX, but how I read that document is that you can update a zipcode field on the Account and have the existing rules assign it.