+ Start a Discussion

Bypass validation rule



we have a validation rule on Lead object ,we need to make  this validation rule as inactive like do not fire in the trigger which is on task object .the trigger is copying some field values from task  to the lead object.


Thanka in advance

You can't do this, at least not directly. You trigger must be able to provide the data that the validation rule needs to allow saving the record. If that's not possible, then the validation rule should be modified to allow saving when a certain value is present.

For example, you could create a hidden field called "bypass validation" that is reset after the lead is updated, then the validation rule could read something like:

AND(NOT(Bypass_Validation__c), YOUR_CRITERIA_HERE)

Hi sfdcfox,


   Thanks for response, As per your idea i created a checkbox name(bypassvalidation__c) on lead object by default it is true, i have modified my validation rule as

               AND(NOT(Bypass_Validation__c),AND(OwnerId <> $User.Id, $UserRole.Name ='Sales Rep'))


 i have written a workflow rule to reset the field value to false to make the validation rule active from the UI,



Evaluation Criteria : Evaluate the rule when a record is created, and every time it’s edited . 

Rule Criteria : Lead: Bypass ValidationEQUALSTrue


which will make the field value to false by field update action. 


Here is my trigger :



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, Bypass_Validation__c=True));    



        leads.put(record.whoid,new lead(id=record.whoid,last_call_result__c=record.calldisposition, notes__c=record.Note_Summary__c, ownerid=userinfo.getuserid(), Bypass_Validation__c=True));




    update leads.values();




 In the above trigger please help out , how to bypass the validation rule.


Don't use a field update, because that will cause the trigger to re-execute. You'll need to set it back to false within the trigger itself. That's the reason for the validation failing.
Manos SpanoudakisManos Spanoudakis

Well the trigger may be prevented from executing a second time using Trigger Stopper (static variable) 

Bhawani SharmaBhawani Sharma
You should use ISCHANGED function in your formula. If didn't change a data, then it should not fire any error.

Hi Sfdcfox,


     could you please make the changes in the code(trigger) , I did'nt understand exactly.





Hi Sfdcfox,


Just a curious question, Can't we use Custom Settings into the validation rule and bypass the validation rule??


Will that cause any problem??




I apologize for missing this post. To answer your question, a custom setting should work just as well as a custom field. It doesn't matter if it's a field or a custom setting, both would work just fine.
Thank you sfdcfox!!

Satar Sk 6Satar Sk 6
Hi Experts,
i am confusing bypass validation rule.
Could you plz help on this.
Thanks in advance.