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
Hari.gsHari.gs 

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

abhishektandon2abhishektandon2

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

Starz26Starz26

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.

 

 

abhishektandon2abhishektandon2

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;

 

}

 

 

}

Starz26Starz26

You cannot pass objects to @future methods....

 

 

abhishektandon2abhishektandon2

pass desired values as individual parameters