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
Robert Wambold 10Robert Wambold 10 

Web-To-Lead + Trigger + Custom Field Update

Hello all,

I have a Before Insert Trigger on Leads object. On my Leads object I have a custom field, NetNewLead with a default value of TRUE.

My trigger checks the Contacts object looking for a match on Leads.Email = Contacts.Email and when a match is found NetNewLead is set to FALSE.

My trigger works correctly when I enter a new Lead, however Web-To-Lead always sets my custom field NetNewLead to FALSE even when the Leads.Email is not found as a Contacts.Email.

Any ideas? I have includded my code if you would like to take a look.

* I know I need to remove hard-coded check for RecrdTypeId.

Thanks for your help in advance.

Robert.


trigger Update_NetNewLead_Trigger on Lead (before insert) {

// Step 1 - Iterate over incoming Leads and store email in a set
    List<String> leadEmails = new List<String>();
    for(Lead lead:Trigger.new){
        leadEmails.add(lead.Email);
    }

// Step 2 - Check whether there are any existing Business Contacts with matching email

    //Id RecordTypeIdContact = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Business_Contact').getRecordTypeId();

    List<Contact> contacts = [
        SELECT 
            Id, Email 
        FROM 
            Contact
        WHERE 
            //Email IN :leadEmails and RecordTypeId=:RecordTypeIdContact 
            Email IN :leadEmails and RecordTypeId='0121A0000007rgsQAA'
            // Only Select "Business Contacts" 
    ];

    Set<String> contactEmails = new Set<String>();
    for(Contact contact:contacts){
        contactEmails.add(contact.Email);
    }

// Step 3 - Iterate over Leads if Business Contact has matching email set NetNewLead = False
    for(Lead lead:Trigger.new){
        if(contactEmails.contains(lead.Email)){
            lead.NetNewLead__c = False;
        }
    }
}

 
Best Answer chosen by Robert Wambold 10
Abhishek BansalAbhishek Bansal

Hi Robert,

Please merge your code into a single trigger since we cannot control the order of execution of apex triggers. Due to this the value get overrideen with the other trigger. Please use the below code:

trigger Update_NetNewLead_Trigger on Lead (before insert) {

    // Step 1 - Iterate over incoming Leads and store email in a set
    List<String> leadEmails = new List<String>();
    for(Lead lead:Trigger.new){
        if(lead.Email != null) {
		    leadEmails.add(lead.Email);
		}
    }

    // Step 2 - Check whether there are any existing Business Contacts with matching email
    //Id RecordTypeIdContact = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Business_Contact').getRecordTypeId();
    List<Contact> contacts = [
        SELECT 
            Id, Email 
        FROM 
            Contact
        WHERE 
            //Email IN :leadEmails and RecordTypeId=:RecordTypeIdContact 
            Email IN :leadEmails and RecordTypeId='0121A0000007rgsQAA'
            // Only Select "Business Contacts" 
    ];

    Set<String> contactEmails = new Set<String>();
    for(Contact contact:contacts){
        contactEmails.add(contact.Email);
    }

    // Step 3 - Iterate over Leads if Business Contact has matching email set NetNewLead = False
    for(Lead lead:Trigger.new){
        if(contactEmails.contains(lead.Email)){
            lead.NetNewLead__c = false;
        }
		if(lead.isClone()) {
		    lead.NetNewLead__c = true;
		}
    }
}
 

Thanks,
Abhishek Bansal.

All Answers

Abhishek BansalAbhishek Bansal
Hi Robert,

When the lead is created from Web-To-Lead form, do you fill the email field? If No, then you have to add a null check in your trigger which will not include the contacts with the null email value. And if you are setting any value in the Email field and still the checkbox is set as False then you have to add the debug statements in your trigger code and look out for the possible cause of the issue.
In case the email is blank, please adjust the code as mentioned below:
trigger Update_NetNewLead_Trigger on Lead (before insert) {

    // Step 1 - Iterate over incoming Leads and store email in a set
    List<String> leadEmails = new List<String>();
    for(Lead lead:Trigger.new){
        if(lead.Email != null) {
		    leadEmails.add(lead.Email);
		}
    }

    // Step 2 - Check whether there are any existing Business Contacts with matching email
    //Id RecordTypeIdContact = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Business_Contact').getRecordTypeId();
    List<Contact> contacts = [
        SELECT 
            Id, Email 
        FROM 
            Contact
        WHERE 
            //Email IN :leadEmails and RecordTypeId=:RecordTypeIdContact 
            Email IN :leadEmails and RecordTypeId='0121A0000007rgsQAA'
            // Only Select "Business Contacts" 
    ];

    Set<String> contactEmails = new Set<String>();
    for(Contact contact:contacts){
        contactEmails.add(contact.Email);
    }

    // Step 3 - Iterate over Leads if Business Contact has matching email set NetNewLead = False
    for(Lead lead:Trigger.new){
        if(contactEmails.contains(lead.Email)){
            lead.NetNewLead__c = False;
        }
    }
}
Please let me know if you have any further update on this.

Thanks,
Abhishek Bansal.
Robert Wambold 10Robert Wambold 10

Thank you Abhishek, my Update_NetNewLead_Trigger trigger is working correctly now.

However, I may have discovered another issue with my process.

We allow users to clone Leads...when a user clones a Lead the NetNewLead value copies to the new cloned Lead. My issue is when cloned Lead had NetNewLead = FALSE, I want a new Lead to considered a new Lead (NetNewLead = TRUE is default value). Having said that I was thinking I could create another trigger to set NetNewLead = TRUE when a Lead isCloned. It seem the clone process doesn't consider NetNewLead = TRUE is default value?

Here's my new code:

trigger LeadCloneTest_Trigger on Lead (before insert) {
    for (Lead NewLead : Trigger.new) {
        if (NewLead.isClone() ){
          NewLead.NetNewLead__c=TRUE;
        }
    }

 

 

 

Abhishek BansalAbhishek Bansal

Hi Robert,

Please merge your code into a single trigger since we cannot control the order of execution of apex triggers. Due to this the value get overrideen with the other trigger. Please use the below code:

trigger Update_NetNewLead_Trigger on Lead (before insert) {

    // Step 1 - Iterate over incoming Leads and store email in a set
    List<String> leadEmails = new List<String>();
    for(Lead lead:Trigger.new){
        if(lead.Email != null) {
		    leadEmails.add(lead.Email);
		}
    }

    // Step 2 - Check whether there are any existing Business Contacts with matching email
    //Id RecordTypeIdContact = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Business_Contact').getRecordTypeId();
    List<Contact> contacts = [
        SELECT 
            Id, Email 
        FROM 
            Contact
        WHERE 
            //Email IN :leadEmails and RecordTypeId=:RecordTypeIdContact 
            Email IN :leadEmails and RecordTypeId='0121A0000007rgsQAA'
            // Only Select "Business Contacts" 
    ];

    Set<String> contactEmails = new Set<String>();
    for(Contact contact:contacts){
        contactEmails.add(contact.Email);
    }

    // Step 3 - Iterate over Leads if Business Contact has matching email set NetNewLead = False
    for(Lead lead:Trigger.new){
        if(contactEmails.contains(lead.Email)){
            lead.NetNewLead__c = false;
        }
		if(lead.isClone()) {
		    lead.NetNewLead__c = true;
		}
    }
}
 

Thanks,
Abhishek Bansal.

This was selected as the best answer