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.
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');
}
}
}
insert dl; commit;
the addError is rolling back uncommited insert
i understand it should not be in a loop, my bad
but it should work the same way on every org.
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
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