You need to sign in to do that
Don't have an account?
UnknownUser
Prevent Account Owner Change
We locked down the Account Owner field on the page layout, but the owner can still change the ownership.
We want to lock this ability down so that only certain profiles can change the ownership.
I wrote some code, but what I need to do is identify the User or profileID performing the update.
So in other words, a 'before update' trigger where if v_allowed_IDs .contains "the ProfileID performing the update" then update object, else return.
Is it possible to programatically use "the ProfileID performing the update"?
Thanks. Regards,
So it can be done this way:
trigger restrictAccountOwnerChange on Account (before update)
{
Id auth_profile= '00999999999999x';
Id v_User = UserInfo.GetUserId();
Set<String> fieldsTracked = new Set<String> {'OwnerId'};
for (Account acc : trigger.new)
{
if (Util.hasChanges(fieldsTracked, acc, trigger.oldMap.get(acc.Id))) {
if (v_User == auth_profile) {
return;
} else {
acc.addError('Cannot Change Account Owners');
}
} else {
return;
}
}
}
But anyone have a more elegant way to code it? This ain't very sexy, but it works...
Sorry! I was testing with a GetUserId, but for profile it should be:
trigger restrictAccountOwnerChange on Account (bef ore update)
{
Id auth_Profile= '00999999999999x';
Id v_Profile = UserInfo.GetProfileId();
Set<String> fieldsTracked = new Set<String> {'OwnerId'};
for (Account acc : trigger.new)
{
if (Util.hasChanges(fieldsTracked, acc, trigger.oldMap.get(acc.Id))) {
if (v_Profile == auth_Profile) {
return;
} else {
acc.addError('Cannot Change Account Owners');
}
} else {
return;
}
}
}
Please don't write code to do this. Just use a validation rule! Much more robust and doesn't require any hard work or development effort.
OR(
IF( $Profile.Name = 'Allowed Profile, false, true),
IF( $Profile.Name = 'Another Allowed Profile, false, true),
...
)
It has to be a trigger so that Admins cannot use Dataloader to change Account Owners.
Why? the data loader respects validation rules.