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
Harry1008Harry1008 

Bypass Validation rule in Apex Trigger on Lead

Hi,
Could you please explain me how to bypass a validation rule in the below trigger. The validation rule is to restrict the users not to change the Status manually. The trigger changes the status if they upload a file. I have created a checkbox called "On and OFF" but couldn't figure out how to include.
rigger FileUploadonLead on ContentVersion (after insert) {
map <id,string> lmap = new map <id,string>();
    list<lead> leadlist = new list <lead>();
    for(contentversion cv : trigger.new){
        id x = cv.FirstPublishLocationId;
        if(string.valueof(x.getsobjecttype()) == 'lead'){
            system.debug('inside test');
            lmap.put(cv.FirstPublishLocationId,'Negotiation');
            
            
        }
        
        
    }
    
    for(Lead l : [SELECT Id,Status FROM Lead Where Id IN : lmap.keySet() AND Status = 'Engagement']){
    system.debug('lmap.get() '+ lmap.get(l.id) );
        l.status = lmap.get(l.id);
        leadlist.add(l);
        
    }
    
    if(leadlist.size()>0){
        system.debug('leadlist ' + leadlist );
        update leadlist;
    }
}

 
Himanshu.SFDCHimanshu.SFDC
Hi Harry,

You can include following method:
  1. Add a "On and OFF" Checkbox field to the object
  2. Set the "On and OFF" field to TRUE in a before trigger.
  3. Add logic to your validation rules so that they do not execute if "On and OFF" is set to TRUE.
Let me know if it works for you.
Harry1008Harry1008
@Himanshu Chittora. Thank you so much for your response. I have created the checkbox field to the object and couldn't figure out how to and where to add the logic in my trigger so asked for community help.
Could you please let me know where should I add the logic in my trigger and how I should add the logic to my validation rule.
Himanshu.SFDCHimanshu.SFDC
Try this if it helps or you can modify accordingly:
Trigger FileUploadonLead on ContentVersion (before insert , after insert, before update,after update) {
    map <id,string> lmap = new map <id,string>();
    list<lead> leadlist = new list <lead>();

    //Add file upload logic to run this trigger
    //if(Fileupload)
    //{

        if(Trigger.isBefore)
        {
            for(contentversion cv : trigger.new)
            {
                cv.SkipValidation__c = true;
            }
        }
        else
        {
            for(contentversion cv : trigger.new)
            {
                id x = cv.FirstPublishLocationId;
                if(string.valueof(x.getsobjecttype()) == 'lead' && cv.SkipValidation__c==true)
                {
                    lmap.put(cv.FirstPublishLocationId,'Negotiation');
                }
            }
        
            for(Lead l : [SELECT Id,Status FROM Lead Where Id IN : lmap.keySet() AND Status = 'Engagement']){
            system.debug('lmap.get() '+ lmap.get(l.id) );
                l.status = lmap.get(l.id);
                leadlist.add(l);
             }
            
            if(leadlist.size()>0){
                system.debug('leadlist ' + leadlist );
                update leadlist;
            }
        }
    //}

}

Bypass Validation Rule:
IF( SkipValidation__c==false , value_if_true, value_if_false)

Please let me know if it helps to you and mark this as best answer to help others.
 
Harry1008Harry1008
@Himanshu.SFDC. Could you please let me know whether SkipValidation__c is a custom checkbox field to set true and false and also the Bypass Validation Rule: that you mentioned, IF( SkipValidation__c==false , value_if_true, value_if_false) where should I include this?
Thank you for your time
Himanshu.SFDCHimanshu.SFDC
Hi Harry1008,

Yes SkipValidation__c  is cutome field which you are using as 'On and Off'.
And in validation rule which you have already created, add your conditon like this:
IF( SkipValidation__c==false , //condition here//, value_if_false)

OR
AND(
SkipValidation__c=false,
//condition here//
)

Thanks
Himanshu​​​​​​​
Himanshu.SFDCHimanshu.SFDC
Can you please post your validation rule here ?
Harry1008Harry1008
HI @Himanshu.SFDC. Thank you so much for your repsonse.
My Validation rule is :
AND( 
NOT(ISCHANGED(On_and_Off__c)), 
ISCHANGED(Status), 
NOT(ISPICKVAL(Status,"Negotiation Lost")), 
NOT(ISBLANK(TEXT(Status))), 
$Profile.Name <> "System Administrator",
). and I am getting an error on line 13 and 21 in your code as CV is invalid type on line 13 and Variable doesn't exist: On_and_Off__c.
Harry1008Harry1008
@Himnanshu SFDC. Could you please help me in fixing this issue
HI @Himanshu.SFDC. Thank you so much for your repsonse.
My Validation rule is :
AND( 
NOT(ISCHANGED(On_and_Off__c)), 
ISCHANGED(Status), 
NOT(ISPICKVAL(Status,"Negotiation Lost")), 
NOT(ISBLANK(TEXT(Status))), 
$Profile.Name <> "System Administrator",
). and I am getting an error on line 13 and 21 in your code as CV is invalid type on line 13 and Variable doesn't exist: On_and_Off__c.