When creating Salesforce formulas, whether they be in a field, a validation rule, or Process Builder, sometimes we need data that is in another object related to the one we are in. You may already be familiar with the “__c” appendix on custom fields and objects – this tag, applied by Salesforce, signifies that this field or object is custom.
For example, if you create a custom object called “Death Star”, Salesforce will give this object an API name of “Death_Star__c”. If you then add a field to this custom object called “Contractor”, Salesforce will subsequently give this field an API name of “Contractor__c”.
Sometimes though, we need to replace “__c” with “__r” in the fields in our formula to get to the correct data.
Why is that? What does that mean?
In short, “__c” signifies something custom in Salesforce while “__r” signifies a relationship.
- When you use “__c”, you are telling Salesforce that you need the data that is stored in that particular custom field, whatever it may be.
- When you instead use “__r”, you are saying you want to access data inside the record that is linked.
Getting back to our Death Star custom object, let’s assume that the Contractor field you set up was actually a Lookup Relationship to Contacts rather than just a text field or picklist. If you wanted to just see if a Contractor was supplied through a validation rule, you would use something like this:
BUT, maybe you wanted to instead see if the Contractor was certified before someone saves the new Death Star record, because we can’t have just anyone building a Death Star! Let’s say there is also a custom field on Contacts labeled Certified (Certified__c) that is a picklist with the values “None” and “Imperial Engineer”. If, in your validation rule, you needed to validate that the Contractor was certified before saving the new Death Star record, your formula would instead look like this:
Notice how the “__c” changed to “__r” for the Contractor field? That is because we are no longer looking to see if there is a value stored on our record for that field, but instead, looking inside that relationship record for another piece of data, the value of the field Certified.
So, whenever you need to look into a relational record for a piece of data, remember to use “__r” to access the fields inside that record.
For more Salesforce tips and tricks, be sure to register for our upcoming Salesforce User Group on Wednesday, September 13th. We will be covering a featured topic and also will be answering ANY Salesforce related questions you submit during registration!