You need to sign in to do that
Don't have an account?

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??
This field is confusing - use parentId instead:
All Answers
You changed the name of your list. Try this:
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:
Thanks again for any help you can give me. Triggers are still so new to me!
This field is confusing - use parentId instead:
Thank you so much!!! Worked perfectly :)