+ Start a Discussion

How can we make a trigger free from validation rule

I have a validation rule on lead object which is for our users to set them as owner for the lead.i written a trigger on task object to update the lead fields with task fields here i'm getting an error


activitycall2:execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 00QE000000ExKxvMAF; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION.


how can i make my trigger free form this validation rule through Apex.

Prafull G.Prafull G.

Unfortunately, there is no straight way to avoid/bypass validation rules using Apex. However there are few workarounds. One of them is explained in this post




Salesforce not provides any functionlity to avoid validation at the time of trigger execution except Lead conversion trigger. 


You have require Inactive OR deactivate the validation, and write the manul validation( Validation logic)  in your the trigger.


Click on KUDOS button if the post helps you!!!

If you find this as solution  please marked as solution for others users help in a same query.


Thank  You, 

Raj Jha


Hi Raj,crmtech21


Actually this is my situation i have a validation rule on lead object for my users to take ownership, if user role is 'salesrep'.I'm writing a trigger on task object of lead to update lead fields i'm facing a tricky situation, in my sandbox i'm not getting any errors as i mention.I login as one of my user of salesrep role i'm able to create the task on a lead which is not owned by him(owned by queue), while updating leads i'm setting present login user as lead owner will it bypass the validation rule? But in production i'm getting the error ...!


Here is my trigger code...please go thriugh this and tell me the exact behaviour


trigger activitycall2 on task(after insert, after update) {
    Set<id> leadIds = new set<id>();    // Lead container
    task[] tasks = new task[0];         // matched tasks
    id queueid,Id1;
    UserRole[] roles;
    Lead a;
    // are there any leads?
    for(task record:trigger.new) {
        if( record.CallDurationInSeconds > 0 || record.whoid != null && record.whoid.getsobjecttype() == lead.sobjecttype ) {
            System.debug('size is'+leadIds.size());
    // if no leads, stop here
    if(leadids.isempty()) {
   else {
        // Gettting user Role
        roles = [select id,name from userrole where id = :userinfo.getuserroleid()];

        // does the queue exist, and what is the id?
        for(group record:[select id from group where type = 'queue' and developername = 'Mailed_Data']) {
            queueid = record.id;
        List<Lead> ownerids=[select id ,ownerid from lead where ownerid = :queueid and id in :leadids];
         a = ownerids.get(0);
        // find any leads owned by the queue
        leadids.retainall((new Map<id,lead>([select id from lead where ownerid = :queueid and id in :leadids])).keyset());
    map<id,lead> leads = new map<id,lead>();
    for(task record:tasks) {
        leads.put(record.whoid,new lead(id=record.whoid,last_call_result__c=record.calldisposition, notes__c=record.Note_Summary__c, ownerid=userinfo.getuserid()));
        leads.put(record.whoid,new lead(id=record.whoid,last_call_result__c=record.calldisposition, notes__c=record.Note_Summary__c));
    update leads.values();

 Reply me ASAP...

Hi ,
Thanks for replying.As per Bob's post he has validation and trigger on same object here my situation is some what different hope you understand i have validation on lead object trigger on task object please guide me with the changes ..i really need this very urgent...