+ Start a Discussion
P KumarP Kumar 

Lead Duplicate Trigger error

Hi,

My requirement is as follows

1) The Trigger should update the existing lead with updated Lead Creation Date, lead source and lead source detail if found with same details like first name, last name, email, country and leadsource and leadsource details are not same, Should not insert new record.
2) if lead source and lead source details are also same then through error (Stop inserting new record).
3) if no matching found then insert the record.

I have written the following trigger, which is not working.

am getting follwoing error : 
Apex trigger APACLeadDuplicateofExistingLead caused an unexpected exception, contact your administrator: APACLeadDuplicateofExistingLead: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.APACLeadDuplicateofExistingLead: line 9, column 1

please help as soon as possible.

//Trigger for APAC Region – Managing Duplicate Leads of Existing Lead

trigger APACLeadDuplicateofExistingLead on Lead (before insert)
{
        Id rtID= [select Id,name from RecordType where name ='APAC - Open' limit 1].Id;

         for(Lead l:Trigger.new)
         {
                Lead lid = [select ID from Lead where FirstName =: l.Name and LastName =: l.LastName and Email =: l.Email and Country_code__c =: l.Country_code__c and RecordTypeId =: rtID limit 1];

                if(l.LeadSource!= lid.LeadSource || l.Lead_Source_Detail_NEW__c != lid.Lead_Source_Detail_NEW__c )
                {
                    Lead updateLead =[select id from lead where id =: lid.id];
                    
                    updateLead.Lead_Creation_Date__c=System.Today();
                    updateLead.LeadSource=l.LeadSource;
                    updateLead.Lead_Source_Detail_NEW__c=l.Lead_Source_Detail_NEW__c;
                    
                    update updateLead;

                    l.addError('Lead already exist with same details, Lead Id : ' + lid.id);
                }
                if(l.LeadSource== lid.LeadSource && l.Lead_Source_Detail_NEW__c == lid.Lead_Source_Detail_NEW__c )
                {
                    l.addError('Lead already exist with same details, Lead Id : ' + lid.id);
                }
         }
}

Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
trigger APACLeadDuplicateofExistingLead on Lead (before insert)
{
        Id rtID= [select Id,name from RecordType where name ='APAC - Open' limit 1].Id;

         for(Lead l:Trigger.new)
         {
                List<Lead> lid = [select ID from Lead where FirstName =: l.Name and LastName =: l.LastName and Email =: l.Email and Country_code__c =: l.Country_code__c and RecordTypeId =: rtID limit 1];

				if(lid.size() > 0)
				{
					if(l.LeadSource!= lid[0].LeadSource || l.Lead_Source_Detail_NEW__c != lid[0].Lead_Source_Detail_NEW__c )
					{
						Lead updateLead =[select id from lead where id =: lid[0].id];
						
						updateLead.Lead_Creation_Date__c=System.Today();
						updateLead.LeadSource=l.LeadSource;
						updateLead.Lead_Source_Detail_NEW__c=l.Lead_Source_Detail_NEW__c;
						
						update updateLead;

						l.addError('Lead already exist with same details, Lead Id : ' + lid[0].id);
					}
					if(l.LeadSource== lid[0].LeadSource && l.Lead_Source_Detail_NEW__c == lid[0].Lead_Source_Detail_NEW__c )
					{
						l.addError('Lead already exist with same details, Lead Id : ' + lid[0].id);
					}
				}	
         }
}

 
P KumarP Kumar

Hi Amit,

This code is not working as per my requirement. when i clone it is saving the record.
This should stop creating the record and update the exisiting lead.


Regards,
Pranav