function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Balu_devBalu_dev 

Batch class to update Parent and child accounts..

          

In an account object we have ParentAccount field which creates parent-child relationships within account records. I have to update both parent and child accounts using some logic..tried few things..but did not work out.. What i did in the code is separated parent and child account records, then for every parent account record, i had to query its related child accounts as i have to check if any of its child account as well the parent account has the field value ""account class = customer''"" if so then update other field of parent and its child account records.....

 

PROBLEM is control is not going beyond below mentioned line in my code :

 

checkChildren=select id,name,Marketing_Account_Class__c, Account_Class__c from Account where parentid=:ParentAccId]; 

 

 can any body give an idea about this ??

 

here is my code:

 

 

        global void execute(Database.BatchableContext bc, sObject[] objects)

        {

          

            system.debug('list of ACCOUNTS :'+ objects);

      

       

            list<Account> theseAccounts = new list<Account>();

            Account a;

   

            for(SObject so : objects)

            {

                a = (Account)so;

                system.debug('First Account:' +a);

                if(a.ParentId == null)

                {     

                   Parentaccs.add(a.Id);

                   system.debug('Parent Account:' +a.Id);

                      

                 

                }

                else

                {

                  Childaccs.add(a.Id);

                }

                          

                theseAccounts.add(a);

            }

            list<Account> Parentaccounts= [select id,name from account where id IN :Parentaccs];

       

         for(account ac : Parentaccounts)

        {

                           

               ParentAccId= ac.id;

              checkChildren=[select id,name,Marketing_Account_Class__c, Account_Class__c from Account where          parentid=:ParentAccId];

                          

               if(checkChildren.size()>0)

                  {

                                            

                      if(ac.Account_Class__c=='Customer')

                        {

                           accountstoupdate.add(ac);

                            ac.Marketing_Account_Class__c='Customer';

                         

 for(account child :checkChildren)

                            {

                               child.Marketing_Account_Class__c='Customer';    

                               accountstoupdate.add(child);

                            }

                               }

                       

                       

                        else if(ac.Account_Class__c!='Customer')

                        {

                            for(account child :checkChildren)

                            {

                                if(child.Account_Class__c=='Customer')

                                {

                                    for(account act :checkChildren)

                                    {

                                        accountstoupdate.add(act);

                                         act.Marketing_Account_Class__c='Customer';

                                     }

                                    Update accountstoupdate;

                                    ac.Marketing_Account_Class__c='Customer';

                                    accountstoupdate.add(ac);

                              

                                }   

                            }

                               

                                                       

                        }   

                 }

            }   

            update accountstoupdate;

        

       

       }