trigger AccountUpdateTerms on Account (before update, after update) {
Map<Id, Account> acc = new Map<Id, Account>();
acc = trigger.oldMap;
for(account newacc : trigger.new){
if(newacc.Credit_Hold__c != acc.get(newacc.Id).Credit_Hold__c || newacc.Payment_Terms__c != acc.get(newacc.Id).Payment_Terms__c ||
newacc.Price_Level__c != acc.get(newacc.Id).Price_Level__c){
List<Account> accupd = new List<Account>();
accupd = [SELECT Id,Company_Code__c,Bill_To_Account__c,Credit_Hold__c,Payment_Terms__c FROM Account WHERE Bill_To_Account__c =: newacc.Id ];
if(trigger.isBefore && accupd.size() >0){
for(account upd : accupd){
upd.ByPass_Validation_Rules__c= TRUE;
}
update accupd;
}
if(trigger.isAfter && accupd.size() >0){
for(account upd : accupd){
upd.Company_Code__c = newacc.Company_Code__c;
upd.Credit_Hold__c = newacc.Credit_Hold__c;
upd.Payment_Terms__c = newacc.Payment_Terms__c;
upd.Price_Level__c= newacc.Price_Level__c;
}
update accupd;
}
}
}
}
Hi Javier ,The above mentioned will throw error due to loop recursion trigger AccountUpdateTerms on Account (before update,after update) {
Map<Id, Account> acc = new Map<Id, Account>();
acc = trigger.oldMap;
List<Account> allNewRecords = accupd = [SELECT Id,Company_Code__c,Bill_To_Account__c,Credit_Hold__c,Payment_Terms__c FROM Account WHERE Bill_To_Account__c =: newacc.newMap.keysets()];
List<Account> accupd=new list<Account>();
for(account newacc : trigger.new)
{
if(newacc.Credit_Hold__c != acc.get(newacc.Id).Credit_Hold__c || newacc.Payment_Terms__c != acc.get(newacc.Id).Payment_Terms__c || newacc.Price_Level__c != acc.get(newacc.Id).Price_Level__c)
{
accupd.add(acc);
}
}
Map<Id,List<Account>> newacc=new Map<Id,List<Account>>();
for(Account a:accupd)
{
for(Account b:accupd)
{
List<Account> macc;
if(b.Bill_To_Account__c==a.id)
{
if(acc.contains(a.id))
{
macc=acc.get(a.id);
macc.add(b);
macc.put(a,b);
}
else
{
macc=new List<Account>();
macc.add(b);
macc.put(a,b);
}
}
}
}
if (Trigger.isBefore) {
if (ContractTriggerHandler.runBU()) {
ContractTriggerHandler.processBeforeUpdate(accupd);
}
}
else {
if (ContractTriggerHandler.runAU()) {
ContractTriggerHandler.processAfterUpdate(newacc);
}
}
}
Trigger handler class :
public class ContractTriggerHandler {
private static boolean runBeforeUpdate = true;
private static boolean runAfterUpdate = true;
public static boolean runBU() {
if (runBeforeUpdate) {
runBeforeUpdate = false;
return true;
}
else {
return false;
}
}
public static boolean runAU() {
if (runAfterUpdate) {
runAfterUpdate = false;
return true;
} else {
return false;
}
}
public static void processBeforeUpdate(List<account> Inputs) {
for(account upd : Inputs)
{
upd.ByPass_Validation_Rules__c= TRUE;
}
update Inputs;
}
public static void processAfterUpdate(Map<Id,List<Account>> newacc) {
List<Account> allAccountRecordsToUpdate=new List<Account>();
for(Id mapKeyId : newacc.keySet())
{
List<Account> upAllRecordForKey=newacc.get(mapKeyId);
for(Account upd:upAllRecordForKey)
{
upd.Company_Code__c = .Company_Code__c;
upd.Credit_Hold__c = newacc.Credit_Hold__c;
upd.Payment_Terms__c = newacc.Payment_Terms__c;
upd.Price_Level__c= newacc.Price_Level__c;
allAccountRecordsToUpdate.add(upd);
}
}
update allAccountRecordsToUpdate;
}
}
Hope this code helps. This is not a verified one but does the functionality as expected by you , it follows the bulk trigger practise.
Let me know if you need further help.Thanks and Regards,Shiva RV