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
mike.rametta.ax1705mike.rametta.ax1705 

Trigger To Tie a Lead to a Contact based on Email

I have a trigger I want to update a related field in the Lead Object To Store the Related Contact based on an email match. 

 

trigger LeadTrigger on Lead (before insert, before update, after insert, after update) 
{	
	if(Trigger.isBefore)
	{
        List<String> leadEmails = new List<String>();
    	for(Lead lead:Trigger.new)
    	{
        leadEmails.add(lead.Email);
    	}

    	List<Contact> contacts = [SELECT id, Unique_Email__c FROM Contact WHERE Email IN :leadEmails ];

    	Set<String> contactEmails = new Set<String>();
        Set<String> contactID = new Set<String>();
        for(Contact contact:contacts)
        {
            contactEmails.add(contact.Unique_Email__c);
            contactEmails.add(contact.id);
        }
    
        for(Lead lead:Trigger.new)
        {
            if(contactEmails.contains(lead.Email)){

// This is where i am having the problem Once i have the set
                lead.Contact__c = contactID.id;
            }
    	}
        
	}
	
	else
	{
        //gonna do something else here
	}
}

 So the issue is once I have the set I cant seem to get it to assing the contact to the Lookup field in the Lead object I get an error saying the initial term of the field expression must be an Sobject Set <String>

 

Not sure i udnerstand what i am doing wrong any help would be appreciated

Best Answer chosen by Admin (Salesforce Developers) 
Kiran  KurellaKiran Kurella

 

You need to replace your sets with a map:

 

trigger LeadTrigger on Lead (before insert, before update, after insert, after update) 
{	
	if(Trigger.isBefore)
	{
        List<String> leadEmails = new List<String>();
    	for(Lead lead:Trigger.new)
    	{
        leadEmails.add(lead.Email);
    	}

	map<String, Id> mapEmailToContactId = new map<String, Id>();
	for (Contact c : [SELECT id, Unique_Email__c FROM Contact WHERE Email IN :leadEmails ]) {
		mapEmailToContact.put(c.Unique_Email__c, c.Id);
	}

    
        for(Lead lead:Trigger.new)
        {
            if(mapEmailToContact.containsKey(lead.Email)) lead.Contact__c =   mapEmailToContact.get(lead.Email);
        
	}
	
	else
	{
        //gonna do something else here
	}
}

 

All Answers

Kiran  KurellaKiran Kurella

 

You need to replace your sets with a map:

 

trigger LeadTrigger on Lead (before insert, before update, after insert, after update) 
{	
	if(Trigger.isBefore)
	{
        List<String> leadEmails = new List<String>();
    	for(Lead lead:Trigger.new)
    	{
        leadEmails.add(lead.Email);
    	}

	map<String, Id> mapEmailToContactId = new map<String, Id>();
	for (Contact c : [SELECT id, Unique_Email__c FROM Contact WHERE Email IN :leadEmails ]) {
		mapEmailToContact.put(c.Unique_Email__c, c.Id);
	}

    
        for(Lead lead:Trigger.new)
        {
            if(mapEmailToContact.containsKey(lead.Email)) lead.Contact__c =   mapEmailToContact.get(lead.Email);
        
	}
	
	else
	{
        //gonna do something else here
	}
}

 

This was selected as the best answer
mike.rametta.ax1705mike.rametta.ax1705

Thanks that was it