Trigger will be like,
trigger AccountMasterTrigger on Account (before insert, before update, after insert, after update, before delete, after delete) {
//AFTER INSERT
if(trigger.isAfter && trigger.isInsert){
//query the accounts to update otherwise you will get read only error.
List<Account> acclist=[SELECT type,Group_Type__c,Physical_Zip__c,Physical_Address_County__c,ownerId FROM Account WHERE Id IN:Trigger.newMap.keySet()];
AccountOwnershipUpdate.change(acclist);
}
}
Helper will be like,
public class AccountOwnershipUpdate {
public static List<Account> accstoupdate;
public static Zip_Code_Region__c zcr;
public static void change(List<Account> acclist){
accstoupdate=new List<Account>();
for(Account a:acclist){
if(a.type=='Prospect' && a.Group_Type__c=='Small'){
try{
zcr=[SELECT Id, Name, Zip_Code__c, Small_Group_Sales_Consultant__c, County__c
FROM Zip_Code_Region__c
WHERE Zip_Code__c=:a.Physical_Zip__c
AND County__c=:a.Physical_Address_County__c
LIMIT 1];
}catch(QueryException qe){
System.debug('Unable to query zip code region due to '+qe);
}
if(zcr!=null){
if(zcr.Small_Group_Sales_Consultant__c!=null){
a.OwnerId=zcr.Small_Group_Sales_Consultant__c;
accstoupdate.add(a);
}
}
zcr=null;
}
}
try{
update accstoupdate;
}catch(DMLException de){
System.debug('Unable to update account due to '+de);
}
}
}
trigger AccountMasterTrigger on Account (before insert, before update, after insert, after update, before delete, after delete) {
//AFTER INSERT
if(trigger.isAfter && trigger.isInsert){
//query the accounts to update otherwise you will get read only error.
List<Account> acclist=[SELECT type,Group_Type__c,Physical_Zip__c,Physical_Address_County__c,ownerId FROM Account WHERE Id IN:Trigger.newMap.keySet()];
AccountOwnershipUpdate.change(acclist);
}
}
Helper will be like,
public class AccountOwnershipUpdate {
public static List<Account> accstoupdate;
public static Zip_Code_Region__c zcr;
public static void change(List<Account> acclist){
accstoupdate=new List<Account>();
for(Account a:acclist){
if(a.type=='Prospect' && a.Group_Type__c=='Small'){
try{
zcr=[SELECT Id, Name, Zip_Code__c, Small_Group_Sales_Consultant__c, County__c
FROM Zip_Code_Region__c
WHERE Zip_Code__c=:a.Physical_Zip__c
AND County__c=:a.Physical_Address_County__c
LIMIT 1];
}catch(QueryException qe){
System.debug('Unable to query zip code region due to '+qe);
}
if(zcr!=null){
if(zcr.Small_Group_Sales_Consultant__c!=null){
a.OwnerId=zcr.Small_Group_Sales_Consultant__c;
accstoupdate.add(a);
}
}
zcr=null;
}
}
try{
update accstoupdate;
}catch(DMLException de){
System.debug('Unable to update account due to '+de);
}
}
}
No comments:
Post a Comment