1

I just started (still learning) my career in Salesforce 3 months ago and am having trouble building a specific validation rule. The idea is to control who can change the ownership of a record.

As of now, it is allowing only The current owner to change it, system admins, sales operations and if a boolean field on the object is checked.

Currently, it is like this (and works):

AND( 
    ISCHANGED(OwnerId), 
    $User.Id != PRIORVALUE(OwnerId),  
    $Profile.Name != 'System Administrator', 
    $UserRole.Name != 'Sales Operations', 
    NOT(Bypass_Opportunity_Validation__c)
)  

I am trying to add another exception. It the userrole contains CSM + manager or CSM + Director. I wrote the following:

AND(
    ISCHANGED(OwnerId), 
    $User.Id != PRIORVALUE(OwnerId),  
    $Profile.Name != 'System Administrator', 
    $UserRole.Name != 'Sales Operations', 
    NOT(Bypass_Opportunity_Validation__c), 
    NOT( 
        AND( 
            CONTAINS($UserRole.Name, "CSM"), 
            OR( 
                CONTAINS($UserRole.Name, "Manager"), 
                CONTAINS($UserRole.Name, "Director") 
            )
        ) 
    )
)

I believe there might be something wrong with Salesforce because I built a simple one and it fails. For example:

This works: (I can edit it as a system admin)

AND( 
    ISCHANGED(OwnerId), 
    $User.Id != PRIORVALUE(OwnerId), 
    $Profile.Name != 'System Administrator' 
)  

This doesn't work: (I cannot edit it as a CSM User)

AND( 
    ISCHANGED(OwnerId), 
    $User.Id != PRIORVALUE(OwnerId), 
    $Profile.Name != 'CSM User'
) 

The profile exists. When I run a query like this:

SELECT Name, Id FROM Profile WHERE Name LIKE '%CSM%'

I get 1 result:

screenshot

Can you shed some light here?

Edit: I am testing changing the owner from A to B. Using a third user with Profile "CSM User" and Role "CSM EMEA Manager". If user I am logged in as is a System Administrator, it allows me to change the owner. But not with the other. It gives me the error setup on this validation rule.

11
  • Welcome To SSE! In case of the two simple validation rules are you testing by changing the owner of the record and are all conditions matching? Jul 21, 2022 at 10:10
  • Hi @SaiPraveenKakkirala, Yes, I am testing changing the owner from A to B. Using a third user with Profile "CSM User" and Role "CSM EMEA Manager". Jul 21, 2022 at 10:56
  • And thank you for chipping in @SaiPraveenKakkirala :D Jul 21, 2022 at 11:02
  • So only this validation rule is active on the object . AND( ISCHANGED(OwnerId), $User.Id != PRIORVALUE(OwnerId), $Profile.Name != 'CSM User' ) . If only this os active you should not be getting the error Jul 21, 2022 at 11:04
  • @SaiPraveenKakkirala THANK YOU!! it was not a validation rule, but it sent me in the right direction. I was not being able to edit it due to the hierarchy setup. I tested on a different opp and it worked. Jul 21, 2022 at 11:39

1 Answer 1

0

As @SaiPraveenKakkirala mentioned, the code was right and was supposed to work. The problem was due to the hierarchy setup. The user I was using to test could not change that specific opportunity. I tested with a different one (owned by someone he manages) and it worked.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .