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
Shruti NigamShruti Nigam 

Hello I want to make trigger for If a Territory__c record’s sales representative is changed (and only when it is changed), repeat Requirement #2’s logic for all Accounts associated with the Territory.

Hello I want to make trigger for If a Territory__c record’s sales representative is changed (and only when it is changed), repeat Requirement #2’s logic for all Accounts associated with the Territory.
Here is requirement 2:
Requirement #2: When an Account’s BillingPostalCode (aka Zip Code), is changed,           
1. Change the Account Owner to the sales representative assigned to the new zip code          
2. Change the Owner field of all the Account’s Contacts to the same sales rep           
3. Change the Owner field of all the Account’s Open Opportunities to the same sales rep    
Navin Selvaraj23Navin Selvaraj23
Hi Shruthi,

You have to use both Trigger.oldMap,Trigger.newMap,Trigger.New in the same method to check value is changed or not. if old value is not equal to new value, then you can perform the next operations.

For requirement 2, the same approach you have to follow. checking old postal code value should not be equal to new postal code, then do the steps 1,2,3 by fetching the salesrep record using the new zipcode.

Do you have any seperate object to store zipcode and corresponding sales representative.?

Hope it helps. If it helps, please mark it as the best answer. if you need any code sample. please feel free to comment below. Always happy to helpl

Regards,
Navin
Naren9Naren9
Hi Shruthi,
You can use the below code.
I have tested in my Org and it is working.

If it helps, please mark as best answer.


trigger TerritoryOwnerChange on Terriotry__c (After Update) {
    for (Terriotry__c T : Trigger.new)
    {  
        //Check whether Old and New owenr are different
      if(Trigger.oldmap.get(T.id).OwnerId  !=  Trigger.newmap.get(T.id).OwnerId )
      {
          //Get the New Owner Id and zipcode and used in below SOQL
         Id newuserId = Trigger.newmap.get(T.id).OwnerId; 
          String Territoryzipcode = Trigger.newmap.get(T.id).Zip_Code__c;
          
          system.debug('OldOwnerId'+ Trigger.oldmap.get(T.id).OwnerId + 'NewOwnerId'+ Trigger.newmap.get(T.id).OwnerId); 
          
          List<Account> NewAccountlisttoUpdate = new List<Account>();
          List<Account> AccountstoUpdateOwner=[Select Id,OwnerId from Account where BillingPostalCode = :Territoryzipcode];
          Set<Id> set_accountId = new Set<Id>();//used to store the Updated Account Ids and used in Contact Search SOQL
          for (Account a1 : AccountstoUpdateOwner )
          {
              if (a1.OwnerId != newuserId)
              {
                  a1.OwnerId=newuserId;
                  NewAccountlisttoUpdate.add(a1);
                  set_accountId.add(a1.Id);
              
              }
          }
          update NewAccountlisttoUpdate;
          //For the Above updated Accounts, check the Contacts and Update with new Owner;
          List<Contact> NewContactlisttoUpdate = new List<Contact>();
              for (Contact Con : [Select Id,OwnerId from Contact where accountId in :set_accountId])
              {
                  if (Con.OwnerId != newuserId)
                      {
                          Con.OwnerId=newuserId;
                          NewContactlisttoUpdate.add(Con);
                                       
                      }
                  
              }
            update NewContactlisttoUpdate;
          
          //For the Above updated Accounts, check the Opportunities and Update with new Owner;
          List<Opportunity> NewOpptylisttoUpdate = new List<Opportunity>();
              for (Opportunity Oppty : [Select Id,OwnerId from Opportunity where accountId in :set_accountId and StageName != 'Closed Won'])
              {
                  if (Oppty.OwnerId != newuserId)
                      {
                          Oppty.OwnerId=newuserId;
                          NewOpptylisttoUpdate.add(Oppty);
                       }
                  
              }
            update NewOpptylisttoUpdate;            
                          
            }
        
    }

}
 
Naren9Naren9
If it solves your problem, please  mark it as the best answer.

Thanks,
Naren
SunnyalexSunnyalex
Hello Naren9 i have seen your code can you please describe all the 3 Requirement  in details so that it will be easier for me to do.
Indresh BhargavIndresh Bhargav
Hi @Naren9 @Shruti what if we have to write the trigger for requirement #2? Any help??