+ Start a Discussion

Help with Case Owner validation Rule



We are running a SF instance with 2 different companies under a parent company.  For cases, we don't want employees of company A to be able to transfer (change ownership of) the case to an employee of company B.


I'm trying to accomplish this by entering a validation rule on the Case Owner field.  Here is my initial attempt which produces a syntax error:


 OwnerId.CompanyName <>  $User.CompanyName


 The problem I'm running into is that I don't seem to be able to access the CompanyName field for the user who is the assigned owner of the case.  I'm not tied to this approach, I just need to accomplish the goal that employees of company A cannot assign cases to employees of company B and vice versa.


That is correct, because Owner is a polymorphic field -- it can be either a Queue or a User, and so the fields available to you are only the ones that make sense in both contexts (because Queues do not have a CompanyName).

I believe you may be better served by putting this logic in an Apex trigger on before insert and before update.  To the end user violating it, it will look the same as if a validation rule had fired, but you'll be able to do some more complex logic as you are doing.


And by the way, how will you enforce these rules if in fact the case owner is assigned to a queue instead of a user?

Thank you for the guidance.  I hadn't thought of the queue aspect.  So, can you think of another way to accomplish the goal of preventing a users of one company from assigning a case to a user of another company?  Is there an easier way that doesn't have the problem of handling the case where the owner could be a queue, or is there a way to add an exception to ignore the rule if the owner is a queue?
Any way you cut it, the only way you're going to be able to enforce that rule is with a before insert and before update Apex trigger.