You need to sign in to do that
Don't have an account?
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,
Hi ,
I think You should try before insert and update.
Thanks
Shailu
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)
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:
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
Please confirm the status of your issue so that we can mark it as solved