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
Dipak@pvsDipak@pvs 

Update Parent Account Lookup field in Account

Hi,

 

I want to update the Parent Account Lookup field in account record using after insert and after update trigger. The condition to update the parent account lookup field is that if recruiter id of account record matches with the parent account the corresponding record will be updated in parent account lookup field of account record.

I am trying with the following code but getting error:

 

trigger UpdateParentAccountInAccount on Account(after insert, after update)
{
    List<string> lst_RecID= new List<string>();
    Map<String, Id> map_ParentAccounts = new Map<String, Id>();
   
    for(Account acct : Trigger.new)
    {
        lst_RecID.add(acct.Recruiter_ID__c);
    }
   
    for(Parent p : [  Select Id,
                                    Recruiter_ID__c
                               From Parent
                              Where Recruiter_ID__c In :lst_RecID])
    {
        map_ParentAccounts.put(p.Recruiter_ID__c , p.Id);
    }
   
    for(Account acct : Trigger.new)
    {                          
        if(map_ParentAccounts.containsKey(acct.Recruiter_ID__c) == true)
        {
            acct.Parent = map_ParentAccounts.get(acct.Recruiter_ID__c);
        }
         else
        {
            //throw error ...
           
           // adb.addError('You can\'t insert Active_Database record without Account Number!');
            return;
        }      
    }   
}

 

 

Any suggestion will be helpful for me.

 

Thanks,

ShaTShaT

Hi ,

 

I think You should try before insert and update.

 

Thanks

Shailu

Abhay AroraAbhay Arora

I think you have some issue with the acct.Recruiter_ID__c seems like this is null...can you check this in debug logs

 

            acct.Parent = map_ParentAccounts.get(acct.Recruiter_ID__c)

            

Dipak@pvsDipak@pvs

I have written the following code:

 

trigger UpdateParentAccountInAccount on Account(after insert, after update)
{   

List<string> lst_ID = new List<string>();   

Map<String, Id> map_Accounts = new Map<String, Id>();       

for(Account a : Trigger.new) {       

if(a.Recruiter_ID__c != null &&
   a.Recruiter_ID__c != ''){
    lst_ID.add(a.Recruiter_ID__c);   
    }
}       
System.debug('\n ####### lst_ID \n '+lst_ID);
for(Account acc : [  SELECT ID, Name, Recruiter_ID__c
                                FROM ACCOUNT
                                    where Recruiter_ID__c In :lst_ID])   

{       
System.debug('\n 1');
map_Accounts.put(acc.Recruiter_ID__c , acc.ID);   

}        
System.debug('\n 2 map_Accounts \n'+map_Accounts);

for(Account a : Trigger.new)  

 { 
 if(a.Recruiter_ID__c != null && !a.Recruiter_ID__c.equals('')){
  if(map_Accounts.containsKey(a.Recruiter_ID__c) == true)
 {
 a.ParentId = map_Accounts.get(a.Recruiter_ID__c);   
  }
  }
  }

 }

 

But, I am getting following error:

 

Error:Apex trigger UpdateParentAccountInAccount caused an unexpected exception, contact your administrator: UpdateParentAccountInAccount: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.UpdateParentAccountInAccount: line 33, column 1.
 
 
In debug log,  a.ParentId = map_Accounts.get(a.Recruiter_ID__c);   shows read only.
 
What can be written at this line.
 
Any suggestion will be helpful.
 
Thanks,
Abhay AroraAbhay Arora

ok so the problem is that the parentid of account is a readonly field because of which the whole logic is failing

Is this a person account you are trying to update ?

 

Also if you are trying to update the record i think you can do the below beforeupdate trigger

Abhay AroraAbhay Arora

Please confirm the status of your issue so that we can mark it as solved