+ Start a Discussion
John Bradshaw 32John Bradshaw 32 

Apex Trigger to populate lookup field

I have always been able to avoid writing code till today. Our marketing team created several hundred Pardot Landing pages for our business partners sales reps to enter leads into. 
These leads come into Salesforce and have a text field  populated called Landing_Page_ID__c . The field is basically firstname.lastname  of the partner sales rep.  

I need to be able to populate a lookup on the lead called Territory_Manager__c . (this is the contact record of the partner sales rep who submiited the lead)  I have created a new field on the Conact called LPID_c which matches the text from the Landing_Page_ID__c.

I am going to watch videos and trailheads but I thought I would ask here first since I am on a deadline to fix this.
Hi John,

Its always better to try it yourself. If its not working out you can use the following trigger
trigger TriggerOnLead on Lead (before insert) {
    map<String,contact> allContactsMap = new map<String,contact>();
    for(Contact c: [SELECT id, FirstName, LastName FROM contact]){
        allContactsMap.put(c.FirstName+'.'+c.LastName, c);
    for(Lead l : trigger.new ){
        if(allContactsMap.get(l.Landing_Page_ID__c) != null) {
            l.Territory_Manager__c = allContactsMap.get(l.Landing_Page_ID__c).Id;

Best Regards
Khan AnasKhan Anas (Salesforce Developers) 
Hi John,

Greetings to you!

You can use the process builder to populate the lookup field. Please refer to the below links which might help you further.

https://help.salesforce.com/articleView?id=000337963&language=en_US&type=1&mode=1 (https://help.salesforce.com/articleView?id=000337963&language=en_US&type=1&mode=1

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas