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 Actually I have written one trigger for this scenario  Change the Account Owner to the sales representative assigned to the new zip code but further i want to shorten  this trigger code Can anyone please help me? ​​​​​​

hello everyone Actually I have written one trigger for this scenario  Change the Account Owner to the sales representative assigned to the new zip code but further i want to shorten  this trigger code Can anyone please help me?
Here is my code:
trigger Requirement1 on Account (after update) {
Set<String> setId1=new set<String>();
    Set<Id> setacc=new Set<Id>();
    for(Account acc:Trigger.new)
    {
             String code1 = Trigger.oldMap.get(acc.Id).BillingPostalCode;
             String code2 = acc.BillingPostalCode;
                if (code2 != code1) {
                      setId1.add(code2);
                         setacc.add(acc.Id);         
                     System.debug('+setId1');
    }
     Map<String,Territory__c> ter1=new Map<String,Territory__c>();
{
    List<Territory__c> ter2 = [SELECT id,Zip_Code__c,User1__c  FROM Territory__c where Zip_Code__c IN:setId1];
              for (Territory__c ter3 : ter2)
{
                ter1.put(ter3.Zip_Code__c,ter3);
  }
                for(Account acc1:Trigger.new)
                   {
                    Territory__c ter3 = ter1.get(acc1.BillingPostalCode);
                    if(ter3!=null)
                    {
                        acc1.OwnerId=ter3.User1__c;
                    }
                }
           }
        } 
     }
Best Answer chosen by Shruti Nigam
Meghna Vijay 7Meghna Vijay 7
Hi Shruti,
 
 for(Account acc:Trigger.new)
    {
             //String code1 = Trigger.oldMap.get(acc.Id).BillingPostalCode;
             //String code2 = acc.BillingPostalCode;
                if (acc.BillingPostalCode != Trigger.oldMap.get(acc.Id).BillingPostalCode) {
                      setId1.add(acc.BillingPostalCode);
                         //setacc.add(acc.Id);         
                     //System.debug('+setId1');
    }
Map<String,Territory__c> ter1=new Map<String,Territory__c>();
//Query :- There will be a unique zipcode for every territory record? If yes then you can use ZipCode as key //otherwise it is not correct
 for (Territory__c ter3 : [SELECT id,Zip_Code__c,User1__c  FROM Territory__c where Zip_Code__c IN:setId1]) {
       ter1.put(ter3.Zip_Code__c,ter3);     
}

Hope it helps, if it does mark it as solved.

Thanks
 

All Answers

Meghna Vijay 7Meghna Vijay 7
Hi Shruti,
 
 for(Account acc:Trigger.new)
    {
             //String code1 = Trigger.oldMap.get(acc.Id).BillingPostalCode;
             //String code2 = acc.BillingPostalCode;
                if (acc.BillingPostalCode != Trigger.oldMap.get(acc.Id).BillingPostalCode) {
                      setId1.add(acc.BillingPostalCode);
                         //setacc.add(acc.Id);         
                     //System.debug('+setId1');
    }
Map<String,Territory__c> ter1=new Map<String,Territory__c>();
//Query :- There will be a unique zipcode for every territory record? If yes then you can use ZipCode as key //otherwise it is not correct
 for (Territory__c ter3 : [SELECT id,Zip_Code__c,User1__c  FROM Territory__c where Zip_Code__c IN:setId1]) {
       ter1.put(ter3.Zip_Code__c,ter3);     
}

Hope it helps, if it does mark it as solved.

Thanks
 
This was selected as the best answer
Meghna Vijay 7Meghna Vijay 7
trigger Requirement1 on Account (after  before update) { } You are making changes on the same object so before trigger should be called.