You need to sign in to do that
Don't have an account?
Rachel Linder 8
Writing Trigger to Update Child Records based on a field on the Parent Record
I have started writing the following trigger to update child records based on a checkbox field being updated on the Parent Record:
trigger Agency_in_Collections on Account (after update) {
for (Account Agency: Trigger.new){
Account oldAgency = Trigger.oldMap.get(Agency.Id);
if(oldAgency.Agency_in_Collections__c != Agency.Agency_in_Collections__c && Agency.Agency_in_Collections__c == true)
{
List<Account> children = [ SELECT Id, AccountID, Agency_in_Collections__c from
Account where AccountID = :Agency.ID];
List<Account> newids = new List <Account>
();
for(Account acc: children){
if(acc.Agency_in_Collections__c != acc.Agency_in_Collections__c){
acc.Agency_in_Collections__c = acc.Agency_in_Collections__c;
newids.add(acc);
}
}
if (newids.isEmpty()== false){
update newids;
}
}
}
}
The checkbox field on the Parent Record is called "Agency in Collections". The field name on the Child Record is "Agency on Collections".
I was working on this in sandbox and was getting an error when saving the trigger. The error is:
Please help. If there is a better way as well please let me tknow.
trigger Agency_in_Collections on Account (after update) {
for (Account Agency: Trigger.new){
Account oldAgency = Trigger.oldMap.get(Agency.Id);
if(oldAgency.Agency_in_Collections__c != Agency.Agency_in_Collections__c && Agency.Agency_in_Collections__c == true)
{
List<Account> children = [ SELECT Id, AccountID, Agency_in_Collections__c from
Account where AccountID = :Agency.ID];
List<Account> newids = new List <Account>
();
for(Account acc: children){
if(acc.Agency_in_Collections__c != acc.Agency_in_Collections__c){
acc.Agency_in_Collections__c = acc.Agency_in_Collections__c;
newids.add(acc);
}
}
if (newids.isEmpty()== false){
update newids;
}
}
}
}
The checkbox field on the Parent Record is called "Agency in Collections". The field name on the Child Record is "Agency on Collections".
I was working on this in sandbox and was getting an error when saving the trigger. The error is:
Please help. If there is a better way as well please let me tknow.
Don't use the Soql , DML inside the for Loop it will hit the governor asap once u processing the bulk data processing:
I hope you are using the Account Hierarchy field here to make the relation between two Accounts if it's yes then the ParentId field below or you are using any custom relationship field then use that field api name below
Please try the below code:
trigger Agency_in_Collections on Account (after update) {
set<Id> ParentIds = new set<Id>();
List<Account> children = new List<Account>();
List<Account> accountstoUpdate = new List<Account>();
Map<Id,String> accountMap = new Map<Id,String>();
for (Account Agency: Trigger.new){
Account oldAgency = Trigger.oldMap.get(Agency.Id);
if(oldAgency.Agency_in_Collections__c != Agency.Agency_in_Collections__c && Agency.Agency_in_Collections__c == true)
{
ParentIds.add(Agency.Id);
accountMap.put(Agency.Id,String.ValueOf(Agency_in_Collections__c)); ////If the Agency_in_Collections__c is lookup then use Map<Id,Id> don't use string.
}
}
if(ParentIds.size() > 0)
{
children = [ SELECT Id, AccountID, Agency_in_Collections__c from
Account where ParentId = :Agency.ID]; ///// If parentId is not working then use the Parent.Id
}
if(children.size() > 0)
{
for(Account acc:children )
{
if(accountMap.containsKey(acc.ParentId))
{
acc.Agency_in_Collections__c = accountMap.get(acc.ParentId);
}
accountstoUpdate.add(acc);
}
}
if(accountstoUpdate.size() > 0)
{
update accountstoUpdate;
}
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Maharajan.C
On the Local Agency Account Record Type when the checkbox field called Agency in Collections is checked then we want to update the checkbox field Agency in Collections on the Advertiser Child Record Type. We have accounts as a related list to accounts. So, our "hierarchy" starts with a record type of Local Agency. Then we create "Advertiser" record types underneath.
Hope that helps.
trigger Agency_in_Collections on Account (after update) {
set<Id> ParentIds = new set<Id>();
List<Account> children = new List<Account>();
List<Account> accountstoUpdate = new List<Account>();
Map<Id,boolean> accountMap = new Map<Id,boolean>();
for (Account Agency: Trigger.new){
Account oldAgency = Trigger.oldMap.get(Agency.Id);
if(oldAgency.Agency_in_Collections__c != Agency.Agency_in_Collections__c && Agency.Agency_in_Collections__c == true)
{
ParentIds.add(Agency.Id);
accountMap.put(Agency.Id,Agency.Agency_in_Collections__c);
}
}
if(ParentIds.size() > 0)
{
children = [ SELECT Id, AccountID, Agency_in_Collections__c from
Account where ParentId IN: ParentIds];
}
if(children.size() > 0)
{
for(Account acc:children )
{
if(accountMap.containsKey(acc.ParentId))
{
acc.Agency_in_Collections__c = accountMap.get(acc.ParentId);
}
accountstoUpdate.add(acc);
}
}
if(accountstoUpdate.size() > 0)
{
update accountstoUpdate;
}
}
Here is a quick screenshot of the Local Agency Page Layout showing the related list to the child records.