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
sfdcksfdck 

Bypass validation rule

Hi 

 

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

sfdcfoxsfdcfox
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)
sfdcksfdck

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 ) {

            leadids.add(record.whoid);

            System.debug('size is'+leadIds.size());

            tasks.add(record);

        }

    }

   

    // if no leads, stop here

    if(leadids.isempty()) {

        return;

    }

   

   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];

        if(ownerids.size()>0){

        

         a = ownerids.get(0);

         Id1=a.OwnerId; 

         System.debug(Id1);    

        }

        

       

            

        // 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) {

   

        if(Id1!=queueid){ 

        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));    

        }

        else{

        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.

  

sfdcfoxsfdcfox
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.
sfdcksfdck

Hi Sfdcfox,

 

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

 

 

Thanks.

Devendra@SFDCDevendra@SFDC

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??

 

Thanks,

Devendra

sfdcfoxsfdcfox
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.
Devendra@SFDCDevendra@SFDC
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.
Regards,
SS