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
Lokeswara ReddyLokeswara Reddy 

Field update through Apex invoking validation rule

If opportunity stage value meets the criteria then trigger fires and invokes handler code which inturn calls external webservcie, if the response from webservcie is success then update a field in Account object. 
The plan is to secure this field and there is a validation rule to prevent any user (except system admin and interface user) from updating on this field,  ( the field can not be hidden from the PG layout and there are users with permission sets to update if the field is read only on PG layout)

I was under impression that the validation rules will by pass as the field update is happending in Apex, , but it is not.

-> User updates opportunity stage value,
-> the trigger fires and invokes webservcie,
-> webservice returns success
-> Apex tries to update field on Account , this step failed as there is a validation rule to prevent user from updating the field
Any thoughts on how to resolve this?
Best Answer chosen by Lokeswara Reddy
Nagendra ChinchinadaNagendra Chinchinada
Apex can't bypass validation rules, untill you set some boolean field(true/false) in apex which is used in validation rule to bypass. Create a boolean field, use it in validation rule as if it is true then by pass rule, and set this boolean field to True in apex before update. If request is going from apex then that validation rule will be bypassed. Validation will be fired for all other requests like page layouts, api..etc.

All Answers

Nagendra ChinchinadaNagendra Chinchinada
Apex can't bypass validation rules, untill you set some boolean field(true/false) in apex which is used in validation rule to bypass. Create a boolean field, use it in validation rule as if it is true then by pass rule, and set this boolean field to True in apex before update. If request is going from apex then that validation rule will be bypassed. Validation will be fired for all other requests like page layouts, api..etc.
This was selected as the best answer
Lokeswara ReddyLokeswara Reddy
Thanks Nagenda,