You need to sign in to do that
Don't have an account?
Error while deleting
Hi,
I have written a trigger
trigger duplicateLeadCheck on Lead (after insert, after update) {
Lead mylead = trigger.new[0];
String str = mylead.FirstName+'_'+mylead.Email;
DuplicatLeadObject__c dupLeadObj = new DuplicatLeadObject__c();
dupLeadObj.Name = str;
//Long oneDay = 86400000;
Long oneDay = 180000;
Datetime currentTime = System.now();
Long l2 = currentTime.getTime();
list<Lead> leadList = [select Id, CreatedDate from Lead where Name_Email__c=:str];
if(leadList != null && leadList.size() > 0) {
for(Lead lead : leadList) {
Datetime createdDate = lead.CreatedDate;
Long l1 = createdDate.getTime();
if((l1+oneDay) > l2) {
dupLeadObj.Duplicate_Lead_Info__c = mylead.Working_Planet_Category_c1__c+'<br>'+mylead.Working_Planet_Source_source__c+'<br>'+mylead.Working_Planet_Keyword_kw__c+'<br>'+mylead.Working_Planet_Ad_cr5__c+'<br>'+mylead.Working_Planet_User_IP__c+'<br>'+mylead.Working_Planet_Timestamp__c+'<br>'+mylead.Company+'<br>'+mylead.Email+'<br>'+mylead.FirstName+'<br>'+mylead.LastName+'<br>'+mylead.Phone+'<br>'+mylead.Title;
insert dupLeadObj;
delete mylead;
//mylead.addError('Duplicate Lead');
}
}
}
}
while executing this i am getting an error like this
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger duplicateLeadCheck caused an unexpected exception, contact your administrator: duplicateLeadCheck: execution of AfterInsert caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.duplicateLeadCheck: line 18, column 1
Please help me.
Thanks and Regards
Hari G S
You issue is you cannot delete records using the trigger.new context....
To do this you will:
**Disclaimer, You have other issues with this trigger, mainly that it is not bulkified. I edited it to work with 1 record as you had it laid out***
Hi thanks for the reply.
But while trying this i am getting an error
Save error: SObject constructor must use name=value pairs. How to solve this
Thanks and Regards
Hari G S
what line?
In this line
you don't need to query while initializing an object.
this should work. Btw it is not bulk enabled.
Hi,
I have tried this but the lead not get deleted ! :(
Regards
Hari G S
Did you get any error for it? OR it just did not get deleted?
Did the duplicateLeadObj record get inserted?
If that record is also not getting inserted, then it has to be something where your if condition isn't getting covered, hence the code inside that if scope is not running.
I am referring to this section of the code :
Hi
I am not getting any errors. But the Custom object is not get inserted and the lead object is not get deleted.
Why is this?
Thanks
Hari G S
Why aren't you using the addError() method before insert/delete? I feel like I'm missing something...