Syntax Highlighter JS

Thursday, April 19, 2012

Formula fields that reference record type names across objects

Hard coding record type ID's in validation rules (or apex for that matter), isn't good practice as it will break across instances (i.e. test vs. prod) and is high maintenance.    So it makes sense to just use the record type name right?

Right and you can use record type names in formula fields and that makes perfect sense.  So you create some record types on the opportunity object as pictured.

Then you create a field called record_type_name__c that has a formula of $RecordType.Developer Name.

Next you add a test record and then open up explorer to query the field and make sure the results are correct:

Now you can reference that field in your formulas on the opportunity object without any problem.  What if you want to reference this formula field from another object? 

Please note you can cross reference this formula field in APEX or SOQL, it appears that this behavior is unique to validation rules.  If you were able to create this cross referenced field prior to salesforce putting the error in place then the cross reference field will return the record type of the child object.  Very odd indeed.

The use case here is simple, create a validation rule that checks the record type name of a parent (master detail relationship in my example) object and implement some testing logic.

So goto your child object and setup a formula that references the record type name formula field from the parent (opportunity) object.

But when you hit save you will get this error:
Which claims the field in the referenced formula does not exist.  Note that I didn't type this name into salesforce but instead was allowed to pick it from the list that sales force populated for me.

Due to this error, you simply can't reference record type names across objects in validation rules (even though you can query them like normal fields in SOQL).   This forces you to put this in apex trigger or to cheat and use a workflow rule to update a hidden field with the record type on the object.   Neither is an ideal situation.  The apex trigger adds development and testing time while the workflow rule with hidden field unnecessarily increases data usage just feels like a total hack

If you have a 3rd way or found a way to make this work, please feel free to comment below and let me know.


  1. Create a text field in the parent with default value as $RecordType.Name

  2. Anonymous, your work around does work but is for the name and not the developer name. The developer name is supposed to be more permanent and as such it is the recommended field to use. Since this was written a while back, it is possible that this has changed on the platform.

    Also you are missing the point that Salesforce pretends this is a field in some places (i.e. lets you query it) but in others it isn't exposed like a 'real ' field is. Salesforce needs to be more consistent in how they expose this kind of thing. The .link 'field' is another great example of the platform having phantom fields.

  3. Based on the fact that the idea is 'coming in the next release', it looks like salesforce is going to implement this. Here is a link to the idea.

    1. Yeah it's now Winter '14 release, and I'm still having this problem. You can't cross-object reference Record.Name in a validation rule. So annoying.