+ Start a Discussion
Matthew LohMatthew Loh 

Create a trigger to populate Parent Account lookup upon Lead conversion

Referring to this post: https://developer.salesforce.com/forums/?id=906F00000008l8SIAQ
I tried the "best answer" from that thread, but I get the error "expecting right curly bracket, found '<'".

The "best answer" was:
 
trigger populateParentAccount on Account (before Insert){<br> List<Lead> convertedLeads=[SELECT Id, ConvertedAccountID, Lead_Parent__c<br> FROM Lead WHERE IsConverted=True AND ConvertedAccountId IN :trigger.new];<br> Map<ID,ID> acctParentMap=new Map<ID,ID>();<br> for (lead l: convertedleads){<br> acctParentMap.put(l.ConvertedAccountId,l.Lead_Parent__c);<br> }<br> for (account a:trigger.new){<br> if (acctParentMap.containsKey(a.Id)){<br> a.<font color="#ff0000">ParentId</font>=acctParentMap.get(a.Id);<br> }<br> }<br>}


Note, I'm using Salesforce Professional edition so Apex isn't natively opened to me. Upgrading everyone to enterprise is out of the question, so  I was hoping to create a trigger in d eveloper edition, then install it as a package in my production, as suggested here: https://developer.salesforce.com/docs/atlas.en-us.packagingGuide.meta/packagingGuide/dev_packages_apex_ge_pe.htm . 

Automating this "Parent Account" field would be a lifesafer, because the way our sales works is ALL of our leads have a Lead Parent (a custom picklist which I've added in Lead Stage).

I am also new to Salesforce, please be nice! :( Just started using it a couple of weeks ago and I'm trying to get all our sales processes streamlined before the volume of our leads pick up.

 Having to manually add the Parent Account everytime a Lead is converted is very inefficient, so I would really like to find a way to make this automatic. Since I can't map the lead fields to Parent Account because that is a look-up field.

Thank you!
Bhushan Patil 44Bhushan Patil 44
Hello Matthew, The code is perfect but when you are copying the code from the post there are some html tags like <br>,<font> which are also included in the code just remove there tags and your code will be working fine or use this code


trigger populateParentAccount on Account (before Insert){
    List<Lead> convertedLeads=[SELECT Id, ConvertedAccountID, Lead_Parent__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.Lead_Parent__c);
    } 
    for (account a:trigger.new){
     if (acctParentMap.containsKey(a.Id)){ 
     a.ParentId = acctParentMap.get(a.Id); 
     }
    }
}
Karthi XaviKarthi Xavi
Hello Mathew,
Design tags [<br><font> etc.] are not allowed in Apex classes and triggers, remove <br>,<font> tags from the code and use,
 
Matthew LohMatthew Loh
Hello @Bhushan,

I used your Code and no problems popped up in the developer console.

I then clicked Save and tried it out, but the Parent Field is not populated in Accounts stage upon lead conversion.

Could it be because my original Lead_Parent__c custom field in Lead stage is a restricted picklist, whereas the Parent Account field in Accounts Stage is look-up?

I made sure that the Parent Account already existed as an Account in Accounts stage before converting.

Any advise please? Thanks!

 
Matthew LohMatthew Loh
See image: User-added image