[SalesForce] Validation rule looking at prior value on related object

Related to my previous question: How to make accounts read-only / locked?

I'm trying to prevent changes from being made to accounts with a certain record type.

One possible change a user could make is to the list of related objects (e.g. its contacts). Accordingly, I'm trying to write a validation rule for these object types that prevents them from being either added to or removed from these records. I thought the following would work:

AND(
    OR(
        Account.RecordTypeId = "0124E0000000fKD",
        PRIORVALUE(Account.RecordTypeId) = "0124E0000000fKD"
    ),
    OR(
        ISCHANGED(AccountId),
        ISNEW()
    )
)

But a get a syntax error:

The PRIORVALUE function cannot reference the Account.RecordTypeId
field

Some Googling reveals that others have had similar problems (e.g. this thread).

So: what's the right way to check the record type of the previously linked account?

Best Answer

The Account isn't being modified, so checking its previous value doesn't make any sense. It didn't change! Just remove that part of your formula:

AND(
    Account.RecordTypeId = "0124E0000000fKD",
    OR(
        ISCHANGED(AccountId),
        ISNEW()
    )
)

I recommend you also filter on RecordType.DeveloperName. It makes your validation less brittle as it should work in all your environments, and also much easier to read:

Account.RecordType.DeveloperName = "Some_Record_Type_You_Can_Read"
Related Topic