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
Madhuri KanthekarMadhuri Kanthekar 

Unable to create parent and child account in after insert trigger

Below is my code, I want to create Parent Account and Child Account from same group id field i.e.(Pro_Alpha_ID__c - external Id).
Here If Parent account of the perticular group id does not exists then it should create Parent Account with that group id and then its child account.....Currently only child account is created

Please help to solve this


trigger AccountAfterInsertUpdate on Account (after insert) 
{
    Account parentAccObj;  
    List<Account> insertAccMapping = new List<Account>();
    List<Account> updateAccMapping = new List<Account>();
    try{ 
    if(CommonUtility.check){
      CommonUtility.check = false;
       ParentAccObj = [SELECT Id,Name,Pro_Alpha_ID__c FROM Account WHERE RecordTypeId='0129E0000004Ozp' AND Pro_Alpha_ID__c =:trigger.new[0].Pro_Alpha_ID__c];
       Account myAccount=trigger.new[0];
       Account updacc=[select id,Pro_Alpha_ID__c,ParentId from account where id = :myAccount.id];
       system.debug('---------------updacc----->'+updacc);
       system.debug('---------------parentAccObj.Id----->'+parentAccObj.Id);
       updacc.ParentId = parentAccObj.Id;            
       updacc.Pro_Alpha_ID__c = parentAccObj.Pro_Alpha_ID__c;
       update updacc;
      // system.debug('---------------accObj.Pro_Alpha_ID__c----->'+accObj.Pro_Alpha_ID__c);
 //     updateAccMapping.add(updacc);            
   
    }
   }      
    
    catch(Exception e)
    {     
        if(CommonUtility.check){
         CommonUtility.check = false; 
         //create child account first   
         Account childAccount = new Account();         
         childAccount.Name = trigger.new[0].Name;
         childAccount.Phone = trigger.new[0].Phone;
         childAccount.Allgemeine_EMail_Adresse__c = trigger.new[0].Allgemeine_EMail_Adresse__c;
         childAccount.ParentId = trigger.new[0].Id;   
         childAccount.Pro_Alpha_ID__c = trigger.new[0].Pro_Alpha_ID__c;
            system.debug('-------------Exception---------'+e.getMessage());
         //upsert childAccount;  
            
        //create a parent reference
        
        Account parentReference = new Account(Pro_Alpha_ID__c = trigger.new[0].Pro_Alpha_ID__c);
        childAccount.ParentId = parentReference.Id;
            system.debug('----------------childAccount.ParentId--------------'+parentReference.ParentId);
        
       // create parent account now
         Account parentAccount = new Account();         
         parentAccount.Name = trigger.new[0].Name;
         parentAccount.Phone = trigger.new[0].Phone;
         parentAccount.Allgemeine_EMail_Adresse__c = trigger.new[0].Allgemeine_EMail_Adresse__c;
         parentAccount.RecordTypeId = '0129E0000004Ozp';
         parentAccount.Pro_Alpha_ID__c = trigger.new[0].Pro_Alpha_ID__c;
            system.debug('-------------Exception---------'+e.getMessage());
         //upsert parentAccount;  
            
        Database.SaveResult[] results = Database.insert(new SObject[] { parentAccount, childAccount});
        // Check results.
        for (Integer i = 0; i < results.size(); i++) {
        if (results[i].isSuccess()) {
        System.debug('Successfully Created ID: '+ results[i].getId());
        } else {
        System.debug('Error: could not create sobject '+ 'for array element ' + i + '.');
        System.debug(' The error reported was: '+ results[i].getErrors()[0].getMessage() + '\n');
        }
        }
      
    }   
    }     

}
rajat Maheshwari 6rajat Maheshwari 6

Hi Madhuri,

Hope you are doing great !!!

Below is bulkified simple code to support your use case, It will help you definately :)

 

trigger AccountAfterInsertUpdate on Account (after insert) 
{
   set<Id> stGroup_Id = new set<Id>();
List<Account> List_ParentAccount = new List<account>();
Map<String,Account> mp_ParentAcc = new Map<String,Account>();
Map<String,Account> mp_ParentAcc1 = new Map<String,Account>();
  
  for(Account acc : Trigger.new)
    {
      if(acc.Pro_Alpha_ID__c!=null)
          stGroup_Id.add(acc.Pro_Alpha_ID__c);
    }




     for(Account Parentacc :  [SELECT Id,Name,Pro_Alpha_ID__c FROM Account WHERE RecordTypeId='0129E0000004Ozp' AND Pro_Alpha_ID__c IN : stGroup_Id])
     {
       

        mp_ParentAcc.put(Parentacc.Pro_Alpha_ID__c,Parentacc);

    }

         for(Account acc : Trigger.new)
           {
              if(acc.Pro_Alpha_ID__c!=null  && mp_ParentAcc.containsKey(acc.Pro_Alpha_ID__c))
                      {
                          acc.ParentId = mp_ParentAcc.get(acc.Pro_Alpha_ID__c).id;
                      }

               else if(acc.Pro_Alpha_ID__c!=null && !mp_ParentAcc.containsKey(acc.Pro_Alpha_ID__c))
                    {
                        Account ParentAccount = new Account();
                        ParentAccount.Pro_Alpha_ID__c = acc.Pro_Alpha_ID__c;
                        ParentAccount.RecordTypeId = '0129E0000004Ozp';
                        List_ParentAccount.add(ParentAccount);
                     }

              }

        if(List_ParentAccount!=null && List_ParentAccount.size()>0)
              {
                  insert List_ParentAccount;

                   for(Account act : List_ParentAccount)
                      {
                              mp_ParentAcc1.put(act.Pro_Alpha_ID__c,act);
                      }

                    for(Account acc : Trigger.new)
                        {
                           if(mp_ParentAcc1.containsKey(acc.Pro_Alpha_ID__c))
                              acc.ParentId = mp_ParentAcc1.get(acc.Pro_Alpha_ID__c).id;
                         }
                  }
   }
 

Thanks
Rajat Maheshwari

 

 

rajat Maheshwari 6rajat Maheshwari 6

Hi Madhuri,

Hope your issue get solved !!!

Please let me know, in case of any help :) 

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com