You need to sign in to do that
Don't have an account?
Prevent from saving the data
Hi,
I have a doubt in trigger.
While creating a Lead object i have written a trigger to chack some details. If that check fails i wanted to write some information from the Lead object to my Custom Object, and this lead object should not be get inserted.
if((l1+oneDay) > l2) {
DuplicatLeadObject__c dupLeadObj = new DuplicatLeadObject__c();
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;
dupLeadObj.Name = str;
insert dupLeadObj;
mylead.addError('Duplicate Lead');
}
This is how my code looks like, because of the addError function my Custom object is also not get inserted.
Is there a way to do this??
Thanks and Regards
Hari G S
Hi Hari,
Problem in you r code is that since the lead object is throwing the error because of that your custom object is also not saved.
this is because both of these DML opertaions are happeing in single context and becuase of failure of the second one (your Lead) the first one (Custome object insert) is rollbacked.
So instead on insert use below one
Database.insert(dupLeadObj,false); // false means if any other operatin fails soes not roll back
Just tested in my org, this does not work. (This was my thought as well to resolve this....
Since the error is added to a different object during the same transaction the database.insert gets rolled back as well.
hmm, Try to move the creation of of yur custom object in some helper class like below and future annotation over it
public class Utils{
@future
prublic static createMyCustomObj(Lead mylead){
DuplicatLeadObject__c dupLeadObj = new DuplicatLeadObject__c();
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;
dupLeadObj.Name = str;
insert dupLeadObj;
}
}
You cannot pass objects to @future methods....
pass desired values as individual parameters