+ Start a Discussion
sam_Adminsam_Admin 

Email opt out from converted Lead

I want to update the email optout field from converted contact to the existing contacts. For ex lets say i have 3 contacts with same email address(email@gmail.com) and emailoptout not checked and iam converting new lead with emailoptout box checked and has same email(email@gmail.com), i want my trigger to update the email optout box to true on these 3 existing contacts. 

trigger updateLeadOnEmailOptOut on Lead (after update) {
    List<Lead> duplicateLeads = new List<Lead>();
    Map<String, Lead> leadEmailMap = new Map<String, Lead>();
    Map<Id, Lead> leadIdMap = new Map<Id, Lead>();
    Map<Id, Contact> contacts = new Map<Id, Contact>();

    for (Integer i = 0; i < Trigger.new.size(); i++) {
        if (Trigger.old[i].HasOptedOutOfEmail != Trigger.new[i].HasOptedOutOfEmail) {
            leadEmailMap.put(Trigger.old[i].email, Trigger.new[i]);
            leadIdMap.put(Trigger.old[i].id, Trigger.new[i]);        
        }
    }    

    If (leadIdMap.size()>0) {


        for (Lead dupLead : [SELECT Id, Name, Email, HasOptedOutOfEmail
                             FROM Lead
                             WHERE Email IN : leadEmailMap.KeySet()
                             AND Id NOT IN : leadIdMap.KeySet()
                             AND IsConverted = FALSE]) {
                                 Lead lead = leadEmailMap.get(dupLead.Email);
                                 If (dupLead.HasOptedOutOfEmail <> lead.HasOptedOutOfEmail) { 
                                     dupLead.HasOptedOutOfEmail = lead.HasOptedOutOfEmail;   
                                     duplicateLeads.add(dupLead);
                                 }
                             }    

        If (duplicateLeads.size()>0) update duplicateLeads;
    }
    for(Lead record: Trigger.new) {
        if(record.ConvertedContactId != null) {
            contacts.put(record.ConvertedContactId, new Contact(Id=record.ConvertedContactId, HasOptedOutOfEmail=record.HasOptedOutOfEmail));
        }
    }
    update contacts.values(); 
}
Best Answer chosen by sam_Admin
Neetu_BansalNeetu_Bansal
Hi,

Use the below trigger:
trigger ContactTrg on Contact( after insert, after update )
{
	Boolean runTrigger = true;
	
	if( runTrigger )
	{
		Set<String> emailAddresses = new Set<String>();
		for( Contact con : trigger.new )
		{
			if( trigger.isInsert
				|| ( trigger.isUpdate && ( con.HasOptedOutOfEmail != trigger.oldMap.get( con.Id ).HasOptedOutOfEmail
											|| con.Email != trigger.oldMap.get( con.Id ).Email )
					)
			)
			{
				emailAddresses.add( con.Email );
			}
		}
		
		Map<String, List<Contact>> emailToContactMap = new Map<String, List<Contact>>();
		for( Contact con : [ Select Email, HasOptedOutOfEmail from Contact where Email IN: emailAddresses AND Id Not IN: trigger.new ])
		{
			List<Contact> contacts = new List<Contact>();
			if( emailToContactMap.containsKey( con.Email ))
			{
				contacts = emailToContactMap.get( con.Email );
			}
			
			contacts.add( con );
			emailToContactMap.put( con.Email, contacts );
		}
		
		List<Contact> contactsToBeUpdated = new List<Contact>();
		for( Contact con : trigger.new )
		{
			if( trigger.isInsert
				|| ( trigger.isUpdate && ( con.HasOptedOutOfEmail != trigger.oldMap.get( con.Id ).HasOptedOutOfEmail
											|| con.Email != trigger.oldMap.get( con.Id ).Email )
					)
				)
			{
				if( emailToContactMap.containsKey( con.Email ))
				{
					for( Contact c : emailToContactMap.get( con.Email ))
					{
						if( c.HasOptedOutOfEmail != con.HasOptedOutOfEmail)
						{
							c.HasOptedOutOfEmail = con.HasOptedOutOfEmail;
							contactsToBeUpdated.add( c );
						}
					}
				}
			}
		}
		
		if( contactsToBeUpdated.size() > 0 )
		{
			runTrigger = false;
			update contactsToBeUpdated;
		}
	}
}
Let me know, if you need any other help.

Thanks,
Neetu