1

I'm not sure disabling validation rule is the only way out for this particular scenario but you guys can chime-in ideas how to approach this particular issue.

I have the following validation rules on my custom__c object

if(Attachment__c == FALSE && URL == null, true, false)

and a Trigger on before insert and after delete on the same custom__c object

  ...........
  ...........
  Attachment__c = false;
  update mycustomobject;

when I try to delete the row I'm getting this error:

execution of AfterDelete

caused by: System.DmlException: Update failed. First exception on row 0 with id xxxxx; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Attachment is unchecked and URL is empty: []

I know exactly why I'm getting this error because its firing the validation-rule that i have place in but I'm not sure what is best practice to deal with this type of situation?

Anybody?

  • 1
    It looks like you posted a bunch of questions in regard to the same issue salesforce.stackexchange.com/questions/121087/… – o-lexi May 9 '16 at 20:33
  • i have had two question regards to the formula and both are different – Nick Kahn May 9 '16 at 20:35
  • 2
    they look for me very similar and you are asking here how to bypass the validation. Validation rules are used to prevent any type of changes you don't want to occur, so I belive you'd want to have validation rule adjusted rather than allowing incorrect data – o-lexi May 9 '16 at 20:46
0

If your code is the only thing that can set Attachment__c to false, you should be able to change your validation rule in include criteria for

NOT(ISCHANGED(Attachment__c))

From your last post, it would become

AND(Attachment__c == false,
NOT(ISCHANGED(Attachment__c)),
NOT(ISBLANK(UR__c)),
NOT(BEGINS(URL__c, "https://")),
NOT(BEGINS(URL__c, "http://")))

This would allow your code to set Attachment to false but would not let anyone else edit the record after until the URL field met your requirements.

Otherwise this blog walks through a more generic way for solving this problem: http://bobbuzzard.blogspot.com/2011/11/bypass-validation-rules-from-apex.html

  • Thanks for the link and its dated back to 2011 and I guess nothing much improved since then... I will go first with your approach because its very simple and only minor change but I could not able to work and my validation rules is like this if(Attachment__c == FALSE && URL == null, true, false) and try to add ISCHANGED but does not work. – Nick Kahn May 9 '16 at 22:41
  • AND(Attachment__c == false, NOT(ISCHANGED(Attachment__c)), ISBLANK(URL__c)) try that – Jimmy Jay May 10 '16 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.