We're using Collaborative Forecasting with Territories in Salesforce. Using an apex trigger, we would like to update a new field(Territroy_Label__c ) we created on the Account Level with the territory the account is in using the Label field from the Territory Level.

This is what I have so far:

trigger AddTerritory on Account (after insert,after update) {

    for (Account acctinloop : Trigger.new) { 

        acctinloop.Territory_Label__c = 'frog'  ; 


Where 'frog' is should be the name of the territory the account is in from the label field on the Territory level. I'm not sure if this is the right code though. Please let me know if you need any additional details.


The Last Halibut

  • 1
    after insert/update triggers will never implicitly modify the records in Trigger.new; only before insert/update triggers. Also, hopefully, field Territroy_Label__c can be spell-corrected before you deploy – cropredy May 1 '15 at 23:01
  • I'm confused by where 'frog' is supposed to come from. Clearly you don't want to arbitrarily assign 'frog' to all of your accounts, so how are you planning to get the correct value to assign to Territory_Label__c? Are you going to query the Territory Object to find the Territory the Account is in or are you trying to assign this via a VisualForce page? If the latter, you wouldn't need an After Trigger or presumably any kind of Trigger. You have this firing on Account, did you mean to have it fire on Territory? If so, then a trigger would be appropriate. – crmprogdev May 2 '15 at 17:30
  • @crmprogdev Thanks for the response! The 'frog' is supposed to be the territory label associated with the account on the Territory Object. The reason I put from is because I don't know how to assign this value to Territory_Label_c . I don't know what VisualForce page is, so is the only other method to query the Territory Object to find the Territory the Account is in, as you suggested. – The_Last_Halibut May 6 '15 at 15:14
  • If you don't know what a VisualForce page is, then you need to go through the Force.com Workbook to learn about the platform before writing triggers. I'd also recommend the Apex Workbook. Yes, you'll need to query the Territory Object once you have a set of Ids that need to assigned. I'll post some sample code. – crmprogdev May 6 '15 at 16:47

First, there are two different models for Territory management. You'll need to figure out which one you have. From the SOAP API Developer Guide, here's the ERD for Territory Managment 2.0 Objects:

Territory Management 2.0 Objects

And here's the ERD for original Territory Management Objects:

Territory Managment Objects

As you can see from both of the above, it appears to me that the easiest way to get the Territory the Account is assigned to will be to use the Account.OwnerId which is something that will be in Trigger.new. I'm assuming that Territory_label__c is the Name field for Territory2.

trigger AddTerritory on Account (after insert,after update) {

   set<Id>acctIds = new set<Id>();
   acctIds = trigger.newMap.keyset();
   // This gives us all the Ids of the inserted or updated accounts

   set<Id>OwnIds = new set<Id>();

   for(Account a : Trigger.new) {

   // Query to get the Territory names

   Map<Id,Territory2>aTerrMap = new Map<Id,Territory2>();
   aTerrMap = [SELECT Id, Name, UserTerritory2Association__r.User FROM Territory2 WHERE UserTerritory2Association.User in :OwnIds];

   // We now have a the Territories related by ownerID and will need to use the ownerID to associate the Territory with the Account. 

   // Will be easier to do that if we put the territory names into a map with the OwnerIds as the key.

   map<Id,string>oId2tName = new map<Id,string>();

   for(ID t2:aTerrMap.keyset()) {

   list<Account>acct2Up = new list<Account>();

   for (Id aId : acctIds) { 

       account acct = new account(Id=aId)

       acct.Territory_Label__c = oId2tName.get(trigger.newMap.get(aId).OwnerId); 



   if(acct2Up.isEmpty == false) update acct2Up;


Note: I've not added any tests here to see if Territory_Label__c was already populated. In the AfterUpdate portion of your trigger, you particularly may want to do that so you won't be updating records that don't need it.

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.