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 Trigger

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 from Engagement to Negotiation when the user upload the file on Engagement Status. Please see the below trigger and validation rule. I am getting the errors. Could someone please help.
Triiger:
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)
            {
                 for(LeadRecord objL: [SELECT Id, Status FROM Lead Where Id = cv.FirstPublishLocationId AND Status = 'Engagement'])
                 {
              objL.On_and_Off__c = true;
                              }
             }
        }
    else
    {
        for(contentversion cv : trigger.new)
        {
            id x = cv.FirstPublishLocationId;
            if(string.valueof(x.getSobjecttype()) == 'lead' && cv.On_and_off__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;
        }
    }
  
    
}

Validation Rule:

OR( 
IF(On_and_Off__c=FALSE, TRUE, FALSE), 
ISCHANGED(Status), 
AND( 
NOT(ISPICKVAL(Status,"Negotiation Lost")), 
OR( 
$Profile.Name <> "System Administrator"
)))
NehaDabas16NehaDabas16