Salesforce __c vs __r: Understanding Custom Fields and Relationship Queries

When working with custom objects and relationships in Salesforce, you’ll frequently encounter the suffixes __c and __r. While they may look similar, they serve very different purposes and are used in different contexts.

Understanding when to use __c and when to use __r is essential for:

  • Writing correct SOQL queries
  • Working efficiently with parent-child relationships
  • Avoiding common Salesforce development mistakes

In this blog, we’ll explain __c and __r with clear definitions, SOQL examples, and real-world use cases.

What is __c in Salesforce?

The suffix __c represents a custom field or a custom object in Salesforce.

In the context of relationships:

  • A lookup or master-detail custom field ending with __c stores the ID of the related record
  • It is used when creating, updating, or filtering records

Example

On the custom object Employee__c:

Company_Name__c

This field stores the Company record ID, not the company’s name.

Key Points About __c

  • Stores data (record IDs or values)
  • Used in DML operations (insert, update)
  • Used in list view filters and WHERE clauses
  • Represents the actual field in the database

What is __r in Salesforce?

The suffix __r represents a relationship reference, not a field.

It is used to:

  • Access fields from related records
  • Traverse relationships in SOQL queries using dot notation

Key Points About __r

  • Read-only
  • Cannot be used for inserts or updates
  • Used only in SOQL queries
  • Enables navigation between parent and child records

Child-to-Parent Relationship Example

Use Case

You want to retrieve the Company Name while querying Employee records.

SOQL Query

SELECT Name, Company_Name__r.Name FROM Employee__c WHERE Company_Name__r.Name != null

SOQL Query

Explanation

  • Company_Name__c stores the Company record ID
  • Company_Name__r allows access to the related Company record
  • Name retrieves the Company’s Name field

This is called a Child-to-Parent query, where:

  • The query starts from the child object
  • __r is used with dot notation to access parent fields

Real-World Use Case

A Salesforce admin wants to display:

Employee Name – Company Name in a report or Lightning component without duplicating data.

Parent-to-Child Relationship Example

Use Case

You want to retrieve all Employees related to a specific Company.

SOQL Query

SELECT Name, (SELECT Name FROM Employees__r) FROM Company__c

Parent-to-Child Relationship

Explanation

  • The query starts from the parent object (Company__c)
  • A subquery is used to fetch child records
  • Employees__r is the Child Relationship Name

Important:

Employees__r is not the object name. It is the Child Relationship Name, defined on the lookup or master-detail field.

You can find it in:

Setup → Object Manager → Lookup/Master-Detail Field → Child Relationship Name

This is known as a Parent-to-Child query.

Real-World Use Case

A customer success team wants to:

  • View all employees linked to a company
  • Display them in a related list or analytics dashboard

__c vs __r: Quick Comparison

Quick Comparison

Common Mistakes to Avoid

  • Using __r in DML operations
  • Trying to update parent fields using __r
  • Assuming Child Relationship Name = Object API Name
  • Using __c to access related object fields

Conclusion

Understanding the difference between __c and __r is fundamental when working with Salesforce relationships.

  • Use __c to store and manage relationship data
  • Use __r to navigate relationships and access related fields in SOQL

Once this distinction is clear, writing relationship queries becomes simpler, cleaner, and far less error-prone.

Interested in expert Salesforce support or custom development? Learn more about our services at support@astreait.com or visit astreait.com to schedule a consultation.