0

I have a text Field called Called Five9 Inbound Campaign Name and Lookup field called Lead Source Name When I enter any text in Five9 Inbound Campaign Name lets say "Zillow - Affinito" I want to show same data on Lead Source name, which is a lookup field and has "Zillow - Affinito" in its lookup ! Tried process builder but that cannot match text field with lookup enter image description here

3
  • You'll need to use a PB-launched flow or Apex trigger to populate the lookup.
    – JaredT
    Commented Jan 18, 2018 at 16:48
  • 2
    Why do you want to have two fields track the exact same data? And what happens if you enter a value in the text field that doesn't exist in the related lookup object? Commented Jan 18, 2018 at 17:19
  • The text field is going to populate automatically. With a vendor we have. And yes the fields are going to be only the ones that exist in the related lookup object !
    – richie
    Commented Jan 18, 2018 at 17:50

1 Answer 1

0

First create a trigger on Lead

    trigger LeadTrigger on Lead (before insert, before update, before delete, after insert, after update, after delete) {

    LeadTriggerHandler handler = new LeadTriggerHandler();   

    if(Trigger.isInsert && Trigger.isBefore){
        handler.BeforeInsert(Trigger.new);
    }
    if(Trigger.isInsert && Trigger.isAfter){
        handler.AfterInsert(Trigger.new, Trigger.newMap);
    }
    if(Trigger.isUpdate && Trigger.isBefore){
        handler.BeforeUpdate(Trigger.new, Trigger.newMap, Trigger.old, Trigger.oldMap); 
    }     
    if(Trigger.isUpdate && Trigger.isAfter){
        handler.AfterUpdate(Trigger.new, Trigger.newMap, Trigger.old, Trigger.oldMap); 
    }
    if(Trigger.isDelete && Trigger.isBefore){
        handler.BeforeDelete(Trigger.old, Trigger.oldMap);
    }
    if(Trigger.isDelete && Trigger.isAfter){
        handler.AfterDelete(Trigger.old, Trigger.oldMap);
    }
}

Next you will want to define an action which executes before insert and before update. Note here that I assume your Lead_Source_Name__c lookup is to campaign.

public class LeadTriggerHandler {

    public void BeforeInsert(List<Lead> newList){
        List<Lead> leadsWithFive9InboundCampaign = new List<Lead>();
        for(Lead l : newList){
            if(l.Five9_Inbound_Campaign_Name__c != null){
                leadsWithFive9InboundCampaign.add(l);
            }
        }
        if(!leadsWithFive9InboundCampaign .isEmpty()){
            populateLeadSourceNameLookup(leadsWithFive9InboundCampaign );
        }
    }

    public void BeforeUpdate(List<Lead> newList, Map<ID,Lead> newMap, List<Lead> oldList, Map<ID,Lead> oldMap){
        List<Lead> leadsWithChangedFive9InboundCampaign = new List<Lead>();
        for(Lead l : newList){
            if(l.Five9_Inbound_Campaign_Name__c != oldMap.get(l.id).Five9_Inbound_Campaign_Name__c ){
                leadsWithChangedFive9InboundCampaign (l);
            }
        }
        if(!leadsWithChangedFive9InboundCampaign .isEmpty()){
            populateLeadSourceNameLookup(leadsWithChangedFive9InboundCampaign );
        }
    }

    private void populateLeadSourceNameLookup(List<Lead> myLeads){
        Set<String> five9CampaignNames = new Set<String>();
        for(Lead l : myLeads){
            if(l.Five9_Inbound_Campaign_Name__c == null){
                l.Lead_Source_Name__c = null;
            } else {
                five9CampaignNames.add(l.Five9_Inbound_Campaign_Name__c);
            }
        }
        Map<String,Id> campaignNameToId = new Map<String,Id>();
        for( Campaign c : [SELECT ID, Name FROM Campaign WHERE Name IN :five9CampaignNames ]){
            campaignNameToId.put(c.name,c.id);
        }
        for(Lead l : myLeads){
            if(l.Five9_Inbound_Campaign_Name__c != null){
                l.Lead_Source_Name__c = campaignNameToId.get(l.Five9_Inbound_Campaign_Name__c));
            }
        }

    }
}

This allows you to populate the lookup without making extraneous update calls like PB would. Let me know if you have any questions!

4
  • Hey Mitch, Your code looks perfect, that what I want to do. Add he ID in the lookup. I am getting few errors after creating trigger and class and adding the code! Here is the list of errors-
    – richie
    Commented Jan 21, 2018 at 21:53
  • Method does not exist or incorrect signature: void leadsWithChangedFive9InboundCampaign(Lead) from the type LeadTriggerHandler Method does not exist or incorrect signature: void BeforeInsert(List<Lead>) from the type LeadTriggerHandler Method does not exist or incorrect signature: void AfterInsert(List<Lead>, Map<Id,Lead>) from the type LeadTriggerHandler Method does not exist or incorrect signature: void BeforeUpdate(List<Lead>, Map<Id,Lead>, List<Lead>, Map<Id,Lead>) from the type LeadTriggerHandler
    – richie
    Commented Jan 21, 2018 at 21:56
  • Method does not exist or incorrect signature: void AfterUpdate(List<Lead>, Map<Id,Lead>, List<Lead>, Map<Id,Lead>) from the type LeadTriggerHandler Method does not exist or incorrect signature: void BeforeDelete(List<Lead>, Map<Id,Lead>) from the type LeadTriggerHandler Method does not exist or incorrect signature: void AfterDelete(List<Lead>, Map<Id,Lead>) from the type LeadTriggerHandl
    – richie
    Commented Jan 21, 2018 at 21:56
  • Also My Lead_Source_Name__c lookup is to custom object called Lead_Source__c Sorry I am a little new on development side. Can you please let me know how do I correct these errors
    – richie
    Commented Jan 21, 2018 at 21:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .