You need to sign in to do that
Don't have an account?
Harry1008
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.
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; } }
You can include following method:
- Add a "On and OFF" Checkbox field to the object
- Set the "On and OFF" field to TRUE in a before trigger.
- 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.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.
Bypass Validation Rule:
Please let me know if it helps to you and mark this as best answer to help others.
Thank you for your time
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:
OR
Thanks
Himanshu
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.
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.