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 

Trigger to insert child accounts to its parent account if parent exists and if not create parent account in salesforce

Hi,

I want to write a trigger for my below requirement, how can achieve this?

Use Case - 
We had an customer that works in a big organization. The want to integrate 8 different companies.
 
So every company had there own account data. So for example that we can have 8 different accounts for the same company.
Every account had the same group id. They will be sparated by there mandant.
 
We need a deployment that adds all accounts to one parent account. When we gets new accounts the trigger will check if there is a parent account. When there is no parent account
The system had to create this. And all other accounts with the same group number will be added to this parent account.
 
Parent accounts gets all a separate record Type. We import every da in the night one times accounts.

Please help !
Akhil ReddyAkhil Reddy
Can you rephrase the whole requiremet. It is bit unclear to understand
Madhuri KanthekarMadhuri Kanthekar
Basically, I want to write a trigger with when accounts are inserted (in bulk),  there is group id field (external Id) upon which they are categorized. So when accounts are imported it should check whether or not any parent account of same gruop id exists, and if parent account of same group id exists accounts should be inserted as child of that parent account else it should create parent account with that group Id and then inserting their childs.

Hope this explanation will help you !
Madhuri KanthekarMadhuri Kanthekar
Please someone guide me, its urgent
Madhuri KanthekarMadhuri Kanthekar
I have written trigger as - 

trigger test on account (after insert)
{
    
    // get all group ids in List <String> 
    List <String> groupIds = new List <String> ();
        For (Account accGroupId: Trigger.New)
        {
            groupIds.add (accGroupId.Pro_Alpha_ID__c);
        }
    System.debug ('--------------- groupIds ----->' + groupIds); 
    
    
  // get all accounts whose's groupId from above stringList  
    List <Account> getAccounts = new List <Account> ();
    
    GetAccounts = [SELECT Id, Name, Phone, Web Site, General_EMail_Adresse__c, Pro_Alpha_ID__c FROM Account WHERE Pro_Alpha_ID__c IN: groupIds];
    
    System.debug ('--------------- getAccounts ----->' + getAccounts);   
    
  // put all accounts and their group into the Map
    Map <String, Account> accountMap = new Map <String, Account> ();
        
    For (Account mapAcc: getAccounts)
    {
        accountMap.put (mapAcc.Pro_Alpha_ID__c, mapAcc);
    }
    System.debug ('--------------- accountMap ----->' + accountMap);
    
  // retrieve all elements in map
  
    <account> updateAccMapping = new List <account> ();
    
    For (Account accObj: Trigger.New)
    {
       Account parentAccObj = accountMap.get (accObj.Pro_Alpha_ID__c); 
        
       System.debug ('--------------- parentAccObj ----->' + parentAccObj);
        
       If (parentAccObj! = Null)
       {
           accObj.Pro_Alpha_ID__c = parentAccObj.Pro_Alpha_ID__c;
           Update accObj;
           System.debug ('-------- accObj -------->' + accObj);
       }
        Else 
       {
        Account parentAccount = new Account (); 
           ParentAccount.Name = accObj.Name;
           ParentAccount.Phone = accObj.Phone;
           ParentAccount.Website = accObj.Website;
           ParentAccount.Allgemeine_EMail_Adresse__c = accObj.Allgemeine_EMail_Adresse__c;
           ParentAccount.RecordTypeId = '0129E0000004Ozp';
           ParentAccount.Pro_Alpha_ID__c = accObj.Pro_Alpha_ID__c;
           UpdateAccMapping.add (parentAccount);
           System.debug ('------------- parentAccount ---->' + parentAccount);
           Insert updateAccMapping;
       }
        
    }
   
}
Akhil ReddyAkhil Reddy
I just reviewd your trigger and I want put some comments on it
1. Do you have any flag on Parent Account to Identify it is parent
2. If you dont have one, then you need to query if the existed account have any childs so you can idetify whether it is parent or not
3. Use set instead of  List <String> groupIds = new List <String> ();
4. 
Else 
       {
        Account parentAccount = new Account (); 
           ParentAccount.Name = accObj.Name;
           ParentAccount.Phone = accObj.Phone;
           ParentAccount.Website = accObj.Website;
           ParentAccount.Allgemeine_EMail_Adresse__c = accObj.Allgemeine_EMail_Adresse__c;
           ParentAccount.RecordTypeId = '0129E0000004Ozp';
           ParentAccount.Pro_Alpha_ID__c = accObj.Pro_Alpha_ID__c;
           UpdateAccMapping.add (parentAccount);
           System.debug ('------------- parentAccount ---->' + parentAccount);
           Insert updateAccMapping;
       }

this code is creating another account record and not attaching any child to it and also its not setting any flag on that parent record to identify whether is parent or normal account.
Madhuri KanthekarMadhuri Kanthekar

trigger AutoAssignParentTrigger on Account (after insert)
{
     static boolean flag =false;
  // get all group ids in List<String> 
    Set<String> groupIds = new Set<String>();
        for(Account accGroupId : Trigger.New)
        {
            groupIds.add(accGroupId.Pro_Alpha_ID__c);
        }
    system.debug('---------------groupIds----->'+groupIds); 
    
    
  // get all accounts whose's groupId from above string list  
    List<Account> getAccounts = new List<Account>();
    
    getAccounts = [SELECT Id,Name,Phone,Website,Allgemeine_EMail_Adresse__c,Pro_Alpha_ID__c FROM Account WHERE Pro_Alpha_ID__c IN:groupIds];
    
    system.debug('---------------getAccounts----->'+getAccounts);   
    
  // put all accounts and their group ids into Map
    Map<String,Account> accountMap = new Map<String,Account>();
        
    for(Account mapAcc : getAccounts)
    {
        accountMap.put(mapAcc.Pro_Alpha_ID__c,mapAcc);
    }
    system.debug('---------------accountMap----->'+accountMap);
    
  // retrieve all elements in map
  
    List<Account> insertAccMapping = new List<Account>();
    List<Account> updateAccMapping = new List<Account>();
    for(Account accObj : Trigger.New)
    {
       Account parentAccObj = accountMap.get(accObj.Pro_Alpha_ID__c); 
        
       system.debug('---------------parentAccObj----->'+parentAccObj);
        
       if(parentAccObj != null)
       {    
           
          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;
         system.debug('---------------accObj.Pro_Alpha_ID__c----->'+accObj.Pro_Alpha_ID__c);
         updateAccMapping.add(updacc); 
           
          
        }
         else 
         {
           flag =true;   
           Account parentAccount = new Account(); 
           
           parentAccount.Name = accObj.Name;
           parentAccount.Phone = accObj.Phone;
           parentAccount.Website = accObj.Website;
           parentAccount.Allgemeine_EMail_Adresse__c = accObj.Allgemeine_EMail_Adresse__c;
           parentAccount.RecordTypeId = '0129E0000004Ozp';
           parentAccount.Pro_Alpha_ID__c = accObj.Pro_Alpha_ID__c;
           //insert parentAccount;              
           insertAccMapping.add(parentAccount);
          system.debug('---------------insertAccMapping----->'+insertAccMapping);         
            }        
   }
    
     if(!insertAccMapping.isEmpty())
      {  
          insert insertAccMapping; 
          flag=false;
      }
      if(!updateAccMapping.isEmpty())
      {
           update updateAccMapping;
      }
    system.debug('---------------inserted insertedAccMapping----->'+insertAccMapping);
    system.debug('---------------updated updateAccMapping----->'+updateAccMapping);
   
       

}

Above is modified my code, It is updating and mapping the child accounts to its parent, but it is throwing error when I create account with new group id (i.e.Pro_Alpha_ID__c)