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
David SomekhDavid Somekh 

Problem with lead insertion in web2lead

Hi all.

Well a strange case, for sure!

 

I have a trigger on Lead which is benn fired before entering lead to the system.

The trigger does not allowes to put in the system 2 leads with the same email address.

 

when entering manualy leads to the system trigger never allowes 2 tlead with the same emai.

SO standing alone the trigger seems to work.

 

I use web2lead program to get data about users that sunscribe to the website.

So the leads enter to my salesforce system mostly by web2lead.

It seems that is some casses the trigger is not fired / no working.

 

I still have some casses with duplicate emails.

By the way, there are duplicate which are not allowed.

It seems like sometimes the trigger work and some don't.

 

Any ideas? Has someone had the same issue?

 

Thanx!

DeptonDepton

I am working on the same case...

 

Can you share code so we can have a look at it togehter?

 

Thanks!

David SomekhDavid Somekh

O.k. so the triggers looks like this:

 

 

// loop over all given Leads look for wrong mail
	for(Lead anyLead : Trigger.new){
		String anyLeadComments = anyLead.Comments__c;
		if (anyLeadComments != null){ 
			if (anyLeadComments.contains('http://')){
				anyLead.addError('Can not save lead - comments field contains \'http://\'');
				return;
			}
		} 
		// else - check lead
		else{
			String anyeLeadEmail = anyLead.Email;
			if (anyeLeadEmail != null){
				if (anyeLeadEmail.contains('@') == false){
					anyLead.addError('Can not save lead - Email adress does not contains @');
				}
				List<Lead> leads_in_sys = [Select Id , 	CreatedDate
											 From Lead 
											 where Email = :anyeLeadEmail];
				// look up of duplicate email
				for(Lead anyLeadInSys : leads_in_sys){
					// if created less than 180 days from today don't allow duplicates
					if (anyLeadInSys.CreatedDate >= Datetime.now().addDays(-30)){
						anyLead.addError('Can not save lead - There is already another lead with the same email in the system');
					}
				}
			}
		}

 

What do you have?

 

DeptonDepton

Here you go:

 

trigger leadDuplicatePreventer on Lead (before insert, before update) {

    Map<String, Lead> leadMap = new Map<String, Lead>();

    for (Lead lead : System.Trigger.new) {

         

        // Make sure we don't treat an email address that 

        // isn't changing during an update as a duplicate. 

     

    if ((lead.Email != null) &&

          (System.Trigger.isInsert ||

                (lead.Email !=

                    System.Trigger.oldMap.get(lead.Id).Email))) {

         

         // Make sure another new lead isn't also a duplicate 

     

     if (leadMap.containsKey(lead.Email)) {

                lead.Email.addError('Another new lead has the '

                                    + 'same email address.');

            } else {

                leadMap.put(lead.Email, lead);

            }

       }

    }

     

    // Using a single database query, find all the leads in 

     

    // the database that have the same email address as any 

     

    // of the leads being inserted or updated. 

     

    for (Lead lead : [SELECT Email FROM Lead

                      WHERE Email IN :leadMap.KeySet()]) {

        Lead newLead = leadMap.get(lead.Email);

        newLead.Email.addError('A lead with this email '

                             + 'address already exists.');

    }

}