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
Edwin KEdwin K 

Ignore validation rule in trigger or apex class

Hi all,

 

I want to ask is it possible for trigger / apex class to not call validation rule? My case is, I want my validation rules to be run when only users create / update some records in the object from web ui but NOT in trigger / apex class, meaning only from trigger / apex class ignore the active validation rule. Are there any ways to achieve my goal?

 

Second question, is validation rule can be configured in profile? My second case is, for example the validation rule named 'checked by admin' only affected for system admin profile, the standard user profile is not affected by that validation rule.

 

Any suggestions would be great..

Thanks...

Best Answer chosen by Admin (Salesforce Developers) 
goabhigogoabhigo

1. I don't think it is possible. But I am surprised why you need to by-pass validation rule for trigger/class. When a user create or update records from UI, in the background trigger/class is called, hence data is inserted or updated. While inserting/updating if you have written any validation rules, it will be checked. Once passed, the record is saved into database ('commit' ).

 

2. Validation rule can be configured for profiles (not in profile). Taking your example, 'checked by admin' should be effective to system admin. So if the current logged in user has system admin profile, the validation rule is not applicable, is it? If yes, then include, AND( $User.ProfileId <> 00e90000000pYq6 , your existing validation rule logic)

--- To get the profile ID, click on System admin profile and from the address bar, copy the ID.

All Answers

goabhigogoabhigo

1. I don't think it is possible. But I am surprised why you need to by-pass validation rule for trigger/class. When a user create or update records from UI, in the background trigger/class is called, hence data is inserted or updated. While inserting/updating if you have written any validation rules, it will be checked. Once passed, the record is saved into database ('commit' ).

 

2. Validation rule can be configured for profiles (not in profile). Taking your example, 'checked by admin' should be effective to system admin. So if the current logged in user has system admin profile, the validation rule is not applicable, is it? If yes, then include, AND( $User.ProfileId <> 00e90000000pYq6 , your existing validation rule logic)

--- To get the profile ID, click on System admin profile and from the address bar, copy the ID.

This was selected as the best answer
Edwin KEdwin K

Thanks... The only possible way is to add addition logic in validation rule just like your second solutions.. Thanks a lot..

kitsunebravekitsunebrave

Hi, 

I suffered the same situation deploying a validation rule to production caused a lot of issue on the deployment interface. We recently discovered that a new validation “VTX_Validate_Sum_of_Incident” impacts in salesforce. The following trigger V VTX001_CalculateChangeCI_PrimeTime have 0% code coverage. Each trigger must have at least 1% code coverage.

We were getting 100% on the trigger was because of the previous test class executions.

Upon our continued investigation on the issue, we could see that the Trigger has zero coverage and this is failing because of Data and to be specific it is failing on the particular validation rule. See the sfdc server logs below:

12:57:37.921 (10927603051)|VALIDATION_RULE|03dL00000005Rir|VTX_Validate_Sum_of_Incident
12:57:37.921 (10928077221)|VALIDATION_FORMULA|(BMCServiceDesk__Status__c = 'AWAITING PEER REVIEW' &#124;&#124; BMCServiceDesk__Status__c = 'AWAITING APPROVAL') && Sum_of_Incident__c <1 && CreatedDate >= DATETIMEVALUE("2018-01-29 12:00:00")|BMCServiceDesk__Status__c=AWAITING APPROVAL , CreatedDate=2018-02-15 17:57:34 , Sum_of_Incident__c=0
12:57:37.921 (10928153323)|VALIDATION_FAIL

And the test class was not failing because we were handling the exceptions in our class, but the exception was as below:

12:57:37.921 (10937401158)|VF_PAGE_MESSAGE|Please link a Incident that relate to the Change before submit for approval. You need to have a Incident link to the change when the change is Awaiting Peer Review or Awaiting Approval.
12:57:37.921 (10937586709)|EXCEPTION_THROWN|[305]|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please link a Incident that relate to the Change before submit for approval. You need to have a Incident link to the change when the change is Awaiting Peer Review or Awaiting Approval.: [BMCServiceDesk__BLANK__c]
12:57:37.921 (10938231352)|USER_DEBUG|[308]|DEBUG|=======in testChangeCILink catch==exp.getMessage()====Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please link a Incident that relate to the Change before submit for approval. You need to have a Incident link to the change when the change is Awaiting Peer Review or Awaiting Approval.: [BMCServiceDesk__BLANK__c]

So, in summary it was hard to discovery this kind of issue. As resolution we desired to deactivated the VR but we will be glad if we can see another permanent solutions better than deactivation. Validations in Salesforce sometimes are painful.

Please any suggestion?

Thanks,

David