You need to sign in to do that
Don't have an account?
Shruti 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;
}
}
}
}
}
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;
}
}
}
}
}
Hope it helps, if it does mark it as solved.
Thanks
All Answers
Hope it helps, if it does mark it as solved.
Thanks
afterbefore update) { } You are making changes on the same object so before trigger should be called.