+ Start a Discussion
RAJ PADMANABHAN 8RAJ PADMANABHAN 8 

Update a Field on Account if it has any child accounts using Trigger

Update a custom field on Account object if the account has any chid records using triggers.  I am trying to update the parent account record if the account has any child accounts.  The scenario is when a new account is created , it does not have any child accoounts.  We have some reporting that is built based on a flag field which looks for accounts if it has child accounts and we would like to set a flag on the parent account if a child exists.  I wrote the following trigger but it sets the child account field value and not the parent accoount field value.


trigger updateAccountChildFlag on Account(after insert, after update) {  
    if (trigger.isUpdate) {
        //Identify Child Account
        Set<Id> setAccountChangedIds = new Set<Id>();
        for (Account oAccount : trigger.new) {
            Account oOldAccount = trigger.oldMap.get(oAccount.Id);
            if (oAccount.ChildExists__c == false ) {
                setAccountChangedIds .add(oAccount.Id);
            }
        }
        //If any, get child account associated with each account
        if (setAccountChangedIds.isEmpty() == false) {
            List<Account> listChildAccounts = [SELECT Id, ParentId FROM Account WHERE ParentId IN :setAccountChangedIds];
            for (Account oAccount1 : listChildAccounts) {
                //Get Accounts
            //    oAccount = trigger.newMap.get(oAccount1.Id);
                //Set Address
                oAccount1.ChildExists__c = true;
            }
            //If any, execute DML command to save contact addresses
            if (listChildAccounts.isEmpty() == false) {
                update listChildAccounts;
            }
        }
    }
}

The intent is to update current active account record by looping through all accounts to see if the parent account has child based on parent id field.  if the result set is not empty then set a flag field indicating that the child account exists.
Best Answer chosen by RAJ PADMANABHAN 8
Ketankumar PatelKetankumar Patel
Hi Raj, 

Sorry I should have used map to remove duplicate parent entries from DML update. I updated my code please try this one and let me know if you get any other errors. 
 
trigger updateAccountChildFlag on Account(after insert, after update) {  

Map<id, Account> updateParentAccount = new Map<Id, Account>();
    if (trigger.isUpdate) {
        //Identify Child Account
        Set<Id> setAccountChangedIds = new Set<Id>();
        for (Account oAccount : trigger.new) {
           // Account oOldAccount = trigger.oldMap.get(oAccount.Id);
            if (oAccount.ChildExists__c == false ) {
                setAccountChangedIds.add(oAccount.Id);
            }
        }
        //If any, get child account associated with each account
        if (setAccountChangedIds.isEmpty() == false) {
            List<Account> listChildAccounts = [SELECT Id, ParentId FROM Account WHERE ParentId IN :setAccountChangedIds];
            for (Account oAccount1 : listChildAccounts) {
            
             Account parentAcc = new account();
                //Get Accounts
            //    oAccount = trigger.newMap.get(oAccount1.Id);
                //Set Address
                //oAccount1.ChildExists__c = true;
                parentAcc.Id = oAccount1.ParentId;
                parentAcc.ChildExists__c = true;
                
                updateParentAccount.put(parentAcc.Id, parentAcc); 
            }
           
        }
    }
     //If any, execute DML command to save contact addresses
            if (updateParentAccount.isEmpty() == false) {
                update updateParentAccount.values();
            }
}

All Answers

Ketankumar PatelKetankumar Patel
Try This..
 
trigger updateAccountChildFlag on Account(after insert, after update) {  

List<Account> updateParentAccount = new List<Account>();
    if (trigger.isUpdate) {
        //Identify Child Account
        Set<Id> setAccountChangedIds = new Set<Id>();
        for (Account oAccount : trigger.new) {
           // Account oOldAccount = trigger.oldMap.get(oAccount.Id);
            if (oAccount.ChildExists__c == false ) {
                setAccountChangedIds.add(oAccount.Id);
            }
        }
        //If any, get child account associated with each account
        if (setAccountChangedIds.isEmpty() == false) {
            List<Account> listChildAccounts = [SELECT Id, ParentId FROM Account WHERE ParentId IN :setAccountChangedIds];
            for (Account oAccount1 : listChildAccounts) {
            
             Account parentAcc = new account();
                //Get Accounts
            //    oAccount = trigger.newMap.get(oAccount1.Id);
                //Set Address
                //oAccount1.ChildExists__c = true;
                parentAcc.Id = oAccount1.ParentId;
                parentAcc.ChildExists__c = true;
                updateParentAccount.add(parentAcc); 
            }
           
        }
    }
     //If any, execute DML command to save contact addresses
            if (updateParentAccount.isEmpty() == false) {
                update updateParentAccount;
            }
}

 
RAJ PADMANABHAN 8RAJ PADMANABHAN 8
I tried the changes, but getting the following error:

Error details from log
17:50:38.095 (2095853329)|EXCEPTION_THROWN|[30]|System.ListException: Duplicate id in list: 001i000001PuI9PAAV 17:50:38.095 (2095874403)|DML_END|[30] 17:50:38.097 (2097091651)|FATAL_ERROR|System.ListException: Duplicate id in list: 001i000001PuI9PAAV.


Error in the UI:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger updateAccountChildFlag caused an unexpected exception, contact your administrator: updateAccountChildFlag: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: 001i000001PuI9PAAV: Trigger.updateAccountChildFlag: line 30, column 1
 
Ketankumar PatelKetankumar Patel
Hi Raj, 

Sorry I should have used map to remove duplicate parent entries from DML update. I updated my code please try this one and let me know if you get any other errors. 
 
trigger updateAccountChildFlag on Account(after insert, after update) {  

Map<id, Account> updateParentAccount = new Map<Id, Account>();
    if (trigger.isUpdate) {
        //Identify Child Account
        Set<Id> setAccountChangedIds = new Set<Id>();
        for (Account oAccount : trigger.new) {
           // Account oOldAccount = trigger.oldMap.get(oAccount.Id);
            if (oAccount.ChildExists__c == false ) {
                setAccountChangedIds.add(oAccount.Id);
            }
        }
        //If any, get child account associated with each account
        if (setAccountChangedIds.isEmpty() == false) {
            List<Account> listChildAccounts = [SELECT Id, ParentId FROM Account WHERE ParentId IN :setAccountChangedIds];
            for (Account oAccount1 : listChildAccounts) {
            
             Account parentAcc = new account();
                //Get Accounts
            //    oAccount = trigger.newMap.get(oAccount1.Id);
                //Set Address
                //oAccount1.ChildExists__c = true;
                parentAcc.Id = oAccount1.ParentId;
                parentAcc.ChildExists__c = true;
                
                updateParentAccount.put(parentAcc.Id, parentAcc); 
            }
           
        }
    }
     //If any, execute DML command to save contact addresses
            if (updateParentAccount.isEmpty() == false) {
                update updateParentAccount.values();
            }
}
This was selected as the best answer
RAJ PADMANABHAN 8RAJ PADMANABHAN 8
Hey Ketan

worked like a champ.  You are awesome.  I understand the changes that were made.  I guess we need to instantiate a new instance of the account whose id matches the parent id from child and then perform the update.

Appreciate it

Raj