You need to sign in to do that
Don't have an account?
Nitin sharma 424
trigger on multiple objects
A (parent Acount) B,C,D E (Multiple child Accounts) All of the above given child Accounts have multiple Opps assocciated with them
There are multiple child accounts and they all have multiple Opportunitiies.Roll summary has worked and the I am able to reflect that amount on the child accounts as well as on the parent account .Howver,I want the parent account field to keep adding amount from the roll up fields which is on on Multiple Accounts..It is replacing value which is on the parent account when an account is updated and it it different from the previous Account .
I tried with Parent to Child query but could not make it.Can somebody please advise?
trigger UpdateParentAccounFromChild on Account (After Update)
{
set<id>AccountIds=new set<id>();
Map<Id,Account>MapOfParentAccountIdToAccRecord=new map<id,account>();
if(trigger.isupdate && trigger.isAfter)
{
for(Account AccountRecord:trigger.new)
{
if(AccountRecord.parentId!=null) //&& AccChildRecord.Total_of_All_Opps__c!=trigger.oldmap.get(AccChildRecord.id).Total_of_All_Opps__c)
{
AccountIds.add(AccountRecord.id);
MapOfParentAccountIdToAccRecord.put(AccountRecord.parentId,AccountRecord);
}
}
}
Account parentRecord=new account();
list<Account>Acclist=new list<account>();
list<Account>listOfAccReords=new list<account>([Select id,Total_of_All_Opps__c,ParentId from account where id in:AccountIds]);
Map<id,Account>MapOfParentIdToAccount=new map<id,Account>([select id,Sum_of_All_opps_of_Child_Acc__c from account where id in:MapOfParentAccountIdToAccRecord.keyset()]);
List<account>UpdateParentAccRec=new list<account>();
Account accrec=new account();
for(Account Acc:listOfAccReords)
{
if(acc.Total_of_All_Opps__c!=null && acc.Total_of_All_Opps__c!=trigger.oldmap.get(acc.id).Total_of_All_Opps__c)
{
if(MapOfParentIdToAccount.containsKey(acc.ParentId))
{
Account ParentAccount=MapOfParentIdToAccount.get(acc.ParentId);
accrec.id=ParentAccount.id;
if(accrec.Sum_of_All_opps_of_Child_Acc__c==null)
{
accrec.Sum_of_All_opps_of_Child_Acc__c=0;
}
accrec.Sum_of_All_opps_of_Child_Acc__c=accrec.Sum_of_All_opps_of_Child_Acc__c+acc.Total_of_All_Opps__c;
UpdateParentAccRec.add(accrec);
}
}
}
Update UpdateParentAccRec;
}
There are multiple child accounts and they all have multiple Opportunitiies.Roll summary has worked and the I am able to reflect that amount on the child accounts as well as on the parent account .Howver,I want the parent account field to keep adding amount from the roll up fields which is on on Multiple Accounts..It is replacing value which is on the parent account when an account is updated and it it different from the previous Account .
I tried with Parent to Child query but could not make it.Can somebody please advise?
trigger UpdateParentAccounFromChild on Account (After Update)
{
set<id>AccountIds=new set<id>();
Map<Id,Account>MapOfParentAccountIdToAccRecord=new map<id,account>();
if(trigger.isupdate && trigger.isAfter)
{
for(Account AccountRecord:trigger.new)
{
if(AccountRecord.parentId!=null) //&& AccChildRecord.Total_of_All_Opps__c!=trigger.oldmap.get(AccChildRecord.id).Total_of_All_Opps__c)
{
AccountIds.add(AccountRecord.id);
MapOfParentAccountIdToAccRecord.put(AccountRecord.parentId,AccountRecord);
}
}
}
Account parentRecord=new account();
list<Account>Acclist=new list<account>();
list<Account>listOfAccReords=new list<account>([Select id,Total_of_All_Opps__c,ParentId from account where id in:AccountIds]);
Map<id,Account>MapOfParentIdToAccount=new map<id,Account>([select id,Sum_of_All_opps_of_Child_Acc__c from account where id in:MapOfParentAccountIdToAccRecord.keyset()]);
List<account>UpdateParentAccRec=new list<account>();
Account accrec=new account();
for(Account Acc:listOfAccReords)
{
if(acc.Total_of_All_Opps__c!=null && acc.Total_of_All_Opps__c!=trigger.oldmap.get(acc.id).Total_of_All_Opps__c)
{
if(MapOfParentIdToAccount.containsKey(acc.ParentId))
{
Account ParentAccount=MapOfParentIdToAccount.get(acc.ParentId);
accrec.id=ParentAccount.id;
if(accrec.Sum_of_All_opps_of_Child_Acc__c==null)
{
accrec.Sum_of_All_opps_of_Child_Acc__c=0;
}
accrec.Sum_of_All_opps_of_Child_Acc__c=accrec.Sum_of_All_opps_of_Child_Acc__c+acc.Total_of_All_Opps__c;
UpdateParentAccRec.add(accrec);
}
}
}
Update UpdateParentAccRec;
}
https://salesforce.stackexchange.com/questions/269406/trigger-to-create-records-in-multiple-objects
Check this too,
https://trailblazers.salesforce.com/answers?id=9063A000000q4I9QAI#:~:text=If%20your%20question%20was%20that,yes%20you%20can%20do%20that.