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
BrandiTBrandiT 

Trigger to Populate Parent Account Lookup on Account object from Custom Lead Field

Hello!

I'm trying to create a trigger that will copy the value from a custom lead field called Parent Account to the standard Parent Account field on the account object when the lead is converted.

 

Here is the code I have: 

 

trigger populateParentAccount on Account (before Insert){
  List<Lead> convertedLeads=[SELECT Id, ConvertedAccountID,
  Parent_Account__c FROM Lead
  WHERE IsConverted=True AND ConvertedAccountId IN :trigger.new];
  Map<ID,ID> acctParentMap=new Map<ID,ID>();
  for (lead l: leads){
    acctParentMap.put(l.ConvertedAccountId,l.Parent_Account__c);
  }
  for (account a:trigger.new) {
    if (acctParentMap.containsKey(a.Id)){
      a.Parent_Account__c=acctParentMap.get(a.Id);
    }
  }
}

 

and here is the error message I'm getting: 

 

 Error: Compile Error: Variable does not exist: leads at line 6 column 16 

 

Any ideas what I'm doing wrong??

Best Answer chosen by Admin (Salesforce Developers) 
jkucerajkucera

This field is confusing - use parentId instead:

 

 

trigger populateParentAccount on Account (before Insert){
List<Lead> convertedLeads=[SELECT Id, ConvertedAccountID, Parent_Account__c
FROM Lead WHERE IsConverted=True AND ConvertedAccountId IN :trigger.new];
Map<ID,ID> acctParentMap=new Map<ID,ID>();
for (lead l: convertedleads){
acctParentMap.put(l.ConvertedAccountId,l.Parent_Account__c);
}
for (account a:trigger.new){
if (acctParentMap.containsKey(a.Id)){
a.ParentId=acctParentMap.get(a.Id);
}
}
}

 

 

All Answers

jkucerajkucera

You changed the name of your list.  Try this:

 

trigger populateParentAccount on Account (before Insert){
List<Lead> convertedLeads=[SELECT Id, ConvertedAccountID,
Parent_Account__c FROM Lead
WHERE IsConverted=True AND ConvertedAccountId IN :trigger.new];
Map<ID,ID> acctParentMap=new Map<ID,ID>();
for (lead l: convertedLeads){
acctParentMap.put(l.ConvertedAccountId,l.Parent_Account__c);
}
for (account a:trigger.new) {
if (acctParentMap.containsKey(a.Id)){
a.Parent_Account__c=acctParentMap.get(a.Id);
}
}
}

 

 

BrandiTBrandiT

Thanks a lot!  That got me through that message.  I got a couple more that I corrected, but now I'm getting a third error message that has me completely stumped.

 

Here's the revised code:

 

trigger populateParentAccount on Account (before Insert){
  List<Lead> convertedLeads=[SELECT Id, ConvertedAccountID, Parent_Account__c
  FROM Lead WHERE IsConverted=True AND ConvertedAccountId IN :trigger.new];
  Map<ID,ID> acctParentMap=new Map<ID,ID>();
  for (lead l: convertedleads){
    acctParentMap.put(l.ConvertedAccountId,l.Parent_Account__c);
  }
  for (account a:trigger.new){
    if (acctParentMap.containsKey(a.Id)){
      a.Parent=acctParentMap.get(a.Id);
    }
  }
}

 

 

and here's the new error message:

 Error: Compile Error: Illegal assignment from Id to SOBJECT:Account at line 9 column 7 

 

 

Thanks again for any help you can give me.  Triggers are still so new to me!

jkucerajkucera

This field is confusing - use parentId instead:

 

 

trigger populateParentAccount on Account (before Insert){
List<Lead> convertedLeads=[SELECT Id, ConvertedAccountID, Parent_Account__c
FROM Lead WHERE IsConverted=True AND ConvertedAccountId IN :trigger.new];
Map<ID,ID> acctParentMap=new Map<ID,ID>();
for (lead l: convertedleads){
acctParentMap.put(l.ConvertedAccountId,l.Parent_Account__c);
}
for (account a:trigger.new){
if (acctParentMap.containsKey(a.Id)){
a.ParentId=acctParentMap.get(a.Id);
}
}
}

 

 

This was selected as the best answer
BrandiTBrandiT

Thank you so much!!!  Worked perfectly  :)