Opportunity Validation Rule - Answers - Salesforce Trailblazer Community
Ask Search:
Karen SilvaKaren Silva 

Opportunity Validation Rule

Hi, I'm trying to create a validation rule that doesn't allow users to edit opptys that have a stage of "posted". It works fine for already existing oppty records but it's firing when creating new posted opptys which is not what I want. Users, except for the ones I've excluded, should not be able to edit posted opptys but can create them. 

I've used the following two formulas (separately) but to no avail:

AND(
ISPICKVAL(PRIORVALUE(StageName), "Posted"),
($Profile.Name <> "CU Sys Admin" || $UserRole.Name <> "Accounting")
)



AND(
NOT(ISNEW()),
ISPICKVAL(StageName, "Posted"),
($Profile.Name <> "CU Sys Admin" || $User.Alias <> "mcast"))
Best Answer chosen by Karen Silva
Karen SilvaKaren Silva
Hey @Steve, just realized I forgot to let you know but I got it to work! This is the formula:

AND(
((NOW()-CreatedDate)*10000) > 1,
OR(ISPICKVAL( StageName, 'Posted' ), TEXT(PRIORVALUE(StageName))
= 'Posted' ),
NOT(
OR(
$Profile.Name = 'CU Sys Admin',
$UserRole.Name = 'Accounting'
)
)
)

I looked online that somebody w the same issue had and that fix^ is a workaround so that the record is exempt from the rule it it's less than 5 sec old (the rule will apply to records older than 5 sec). Without that part, new posted opptys weren't able to get created.
https://success.salesforce.com/answers?id=906300000019MhTAAU

There's one wfr activated so maybe that was why (according to that link^) it was giving me issues upon creation. Thanks for all your help!!

All Answers

Tommaso BolisTommaso Bolis
try
AND(
  NOT(ISNEW()),
  ISPICKVAL(StageName, "Posted"),
  AND($Profile.Name <> "CU Sys Admin", $User.Alias <> "mcast")
)

 
Steve MolisSteve Molis
++ TomasoB 

You don't want to use an OR statement with NOT or <> because a User Detail record can only have 1 Username, 1 User Profile, 1 User Role, etc... 

For example if you wanted to block any other User with Profile <> X, Y, Z from doing something and:   
Joe has $Profile.Name = Q
Moe has $Profile.Name = X
Larry has $Profile.Name = Y 
Curly has $Profile.Name = Z 

If your Formula uses Logic like this:

OR(
$Profile.Name <> "X",
$Profile.Name <> "Y",
$Profile.Name <> "Z",
)

Joe is blocked because Q is <> X or Y or Z 
great!

BUT...
Moe is also blocked because X is <> Y or Z
Larry is also blocked because Y is <> X or Z
Curly is also blocked because Z is <> X or Y 

So now every User is blocked, not just the one's you want (and that's not so great) 

Unless the User is Schrodinger's Cat, then all bets are off... 
User-added image
Karen SilvaKaren Silva
@SteveMolis do you have any suggestions for the formula?

Thanks.
Steve MolisSteve Molis
What TomasoB posted should do what you want 

I might write it like this
 
AND(
NOT(ISNEW()),
TEXT(StageName) = "Posted",
$Profile.Name <> "CU Sys Admin", 
$User.Alias <> "mcast"
)
or 
AND(
NOT(ISNEW()),
TEXT(StageName) = "Posted",
$Profile.Name <> "CU Sys Admin", 
$User.Username <> "mcast@your.org"
)


 
Karen SilvaKaren Silva
@SteveMolis

It still doesn't allow new posted opportunities to get created by other users. Also, these same users can edit the Stage field itself on opptys that are posted (change it from Posted to something else) but not edit any other field. The Stage field shouldn't be allowed to be changed either :-\
Steve MolisSteve Molis
Can you confirm that there are not other Validation Rules being triggered that are doing that? 

The way this rule is written 
AND(
NOT(ISNEW()),
TEXT(StageName) = "Posted",
$Profile.Name <> "CU Sys Admin", 
$User.Alias <> "mcast"
)

It will only block them from saving the record if it is

1.  Not new
AND
2. The Stage = Posted
AND
3. The User is not CU Sys Admin
AND
4. The User Alias is not mcast 


Also can you verify that the User Profile and User Alias valies in you formula are an exact match with the actual User Profile Name and the User Alias?
Karen SilvaKaren Silva
I turned off the other rules but it still won't let me create posted opptys. Yes those are the exact names. 

I believe the PRIORVALUE function was taking care of one of the issues to not allow the stage field from being modified once the the stage is in posted. I just need to be able to create them as well. I'm not sure why the NOTISNEW functions aren't working. 

Also to your point before, would I need to add an OR function for the profile + username? So that if the user is not admin or it isn't mcast, then it should fire?

Thank you for your help thus far!
Steve MolisSteve Molis
No, you do not want to use an OR for the reasons that I posted above

Try doing a simple test using this Formula
AND(
NOT(ISNEW()),
TEXT(StageName) = "Posted"
)
If that allows you to create a brand new Opportunity with the Stage set to "Posted" and save the record, then you know that the issue is with this part of the Formula  
$Profile.Name <> "CU Sys Admin", 
$User.Alias <> "mcast"


 
Karen SilvaKaren Silva
@Steve that first formula by itself didn't allow me to create it
Steve MolisSteve Molis
That doesn't make any sense, the NOT(ISNEW()) will only fire if the record has already been saved and assigned a Salesforce record Id, it will never trigger on Create + Save, it will only be trigger by an Edit + Save

Can you post screenshots of exactly what you are doing to create the new Opportunity record and trigger the error?
Karen SilvaKaren Silva
Hey @Steve, just realized I forgot to let you know but I got it to work! This is the formula:

AND(
((NOW()-CreatedDate)*10000) > 1,
OR(ISPICKVAL( StageName, 'Posted' ), TEXT(PRIORVALUE(StageName))
= 'Posted' ),
NOT(
OR(
$Profile.Name = 'CU Sys Admin',
$UserRole.Name = 'Accounting'
)
)
)

I looked online that somebody w the same issue had and that fix^ is a workaround so that the record is exempt from the rule it it's less than 5 sec old (the rule will apply to records older than 5 sec). Without that part, new posted opptys weren't able to get created.
https://success.salesforce.com/answers?id=906300000019MhTAAU

There's one wfr activated so maybe that was why (according to that link^) it was giving me issues upon creation. Thanks for all your help!!
This was selected as the best answer