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
AnthonyBAnthonyB 

Validation failing when fired as a result of TRIGGER update

Background, I want to update Account owner when user updates custom object (Budget__c) owner.

 

When user updates Budget__c owner, an after update trigger fires and updates related Account owner. When SysAdmin is logged in everything works fine else it fails validation rule.

 

Administrator set following validation rule on Account to prevent users updating ownerid directly (00e30000000mDYz is Sys Admin profile id)

 

AND(ischanged(OwnerId), $Profile.Id <> '00e30000000mDYz')

 

I was under impression Trigger runs as System Admin so why does this fail??

 

TIA  :robotfrustrated:

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

You are using a globale variable in the validation rule. VR do not run in system mode. The global variables are related to the running user and thus since the profile in not system admin it will not allow it.

 

One way to do it is:

 

1. Create a flag on the object to indicate the trigger is updating.

2. set the flag to tru in the trigger.

3. Add that flag == false into the validation rule.

4. After rule runs, use field update to ensur that flag is reset to false.

 

That way, when a trigger updates to owner the validation rule passes, but if a user tries to do it the flag will be false and the validation rule will fail.

All Answers

Starz26Starz26

You are using a globale variable in the validation rule. VR do not run in system mode. The global variables are related to the running user and thus since the profile in not system admin it will not allow it.

 

One way to do it is:

 

1. Create a flag on the object to indicate the trigger is updating.

2. set the flag to tru in the trigger.

3. Add that flag == false into the validation rule.

4. After rule runs, use field update to ensur that flag is reset to false.

 

That way, when a trigger updates to owner the validation rule passes, but if a user tries to do it the flag will be false and the validation rule will fail.

This was selected as the best answer
Niket SFNiket SF

Hello 

 

     AnthonyB please make sure that system admin user profile Id is 15 digit or 18 digit. Some time it happends that we check the 15 digit id with the 18 one and validation get fils. So please use system.debug() method to check that it's validating with the same Id or not.  

 

If it helps you please marker as solved. It helps other also.

 

Thanks 

Nik's

 

 

Skype name : niket.chandane

 

AnthonyBAnthonyB

@Starz26 I was hoping SF had a built-in method to detect context but your solution worked just as well.

 

Much appreciated =)