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
Vijay RautVijay Raut 

Duplicate Lead filtering using Trigger

Hi
Vijay RautVijay Raut
Hi All,
 
I have to store the duplicate lead data into custom object (Lead Clone) if lead with Last Name and First Name is already Exist in SFDC.
I have written trigger as follows.
 

trigger myLeadTrigger on Lead bulk(before insert)

{

for (Lead l : Trigger.new)

{

for(Lead l1:[Select Id, Name from Lead where LastName=:l.LastName AND FirstName=:l.FirstName])

{

Lead_Clone__c dl = new Lead_Clone__c(LastName__c=l.LastName,FirstName__c=l.FirstName,Lead__c=l1.Id);

insert dl;

l.LastName.addError('Record with this name already exits Adding it in LeadClone');

}

}

}

 
But it is not giving any Error in Apex Log. Also it showing the alert message. But not create the new Lead Clone Object. Not getting what is wrong in above code.
 
If any one having idea about how to create new Custom Object record using trigger then please reply to this post.
 
Thanks in Advance
Vijay Raut
Ron HessRon Hess
you can add commit after the insert.

insert dl; commit;

the addError is rolling back uncommited insert
Vijay RautVijay Raut
Hi Ron,
 
Thanks a lot for your help.
I have checked it and working fine.
 
But one small problem there..... that commit is not allowed in the for loop.
Hence it was giving me error.
 
Also same code was working on another org.... but giving problem on my org.
 
So is there any setting on org for auto commit as code was working fine without commit on some org.
 
Thanks in Advance
 
Regards
Vijay Raut
Ron HessRon Hess
strange, i'll have to look into the commit.
i understand it should not be in a loop, my bad

but it should work the same way on every org.
Chirag MehtaChirag Mehta
I had used update or insert triggers but there was never need for using commit keword inside the triggers.

Strange to hear about the same !!!!

Even there's no reference of commit in  AJAX 9.0 Guide
http://www.salesforce.com/us/developer/docs/ajax/index.htm

Vijay RautVijay Raut

I too feeling clue less..... about that..... But thats true... Same Apex code working for one devp org and not working on other org...... :-(

Regards

Vijay raut