function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jdeveloperjdeveloper 

what the difference between __c and __r

hi, im new to salesforce and i was wondering what was the difference between __c and __r when refering to a custom object. Also can u give me examples. thanks

Best Answer chosen by Admin (Salesforce Developers) 
nylonnylon

Hi,

 

"__r" is used for retrieving field values from the object's related another object when those objects have relationship via Lookup field.

See links below for detail and samples.

 

In Formula:

http://developer.force.com/cookbook/recipe/displaying-fields-from-a-related-record-on-a-detail-page

In SOQL:

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm

All Answers

nylonnylon

Hi,

 

"__r" is used for retrieving field values from the object's related another object when those objects have relationship via Lookup field.

See links below for detail and samples.

 

In Formula:

http://developer.force.com/cookbook/recipe/displaying-fields-from-a-related-record-on-a-detail-page

In SOQL:

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm

This was selected as the best answer
Santosh Kumar YadavSantosh Kumar Yadav
__r represents a custom relationship. There are two uses for __r. We use it when we query a custom relationship from child to parent, or from parent to child.
For example, if you have two custom objects, called Service__c and Service_Line__c, where the Service Line has a field that references a Service as its parent, you can query from child to parent, or parent to child.
The child to parent relationship query looks like this:
SELECT Id, Service__c, Service__r.Name FROM Service_Line__c
While the parent to child relationship looks like this:
SELECT Id, Name, (SELECT Id, Name FROM Service_Lines__r) FROM Service__c
To access parent and children records in Apex Code, you'd use the same syntax:
Service_Line__c line = [select ... from service_line__c where ...];
if (line.service__r.name == 'Master Service') {
// Do something
}
Service__c service = [select ... from service__c where ...];
for(Service_Line__c line:service.Service_Lines__r) {
// Do something
}
 
Alexis MassonAlexis Masson
Thanks Santosh Kumar yadav, for your example :
"For example, if you have two custom objects, called Service__c and Service_Line__c, where the Service Line has a field that references a Service as its parent, you can query from child to parent, or parent to child.
The child to parent relationship query looks like this:
SELECT Id, Service__c, Service__r.Name FROM Service_Line__c
While the parent to child relationship looks like this:
SELECT Id, Name, (SELECT Id, Name FROM Service_Lines__r) FROM Service__c"

I think it's unlocking my understanding of custom relationship :)
 
Lukesh KarmoreLukesh Karmore
Alexis and santosh kumar thank you
__c and __r  are not use inSOQL with standard object..
It is use only with custom object???
dhsio hdxsoidhsio hdxsoi
I understand the issue you are facing here and would like to share some information about it check here (https://bestsportsshoe.com/best-basketball-shoes-for-kids/) how they are guiding about it.
veniora12 joeveniora12 joe
Great Post! Thank you for visiting here. https://tractorsinfo.net/myenvoyair-login/
Debaranjan GhoshDebaranjan Ghosh
Thanks everybody for making simple explanation of complex tricky concepts .
While working on similar thing I had  a slightly different observation I have 1 child table with a look up field and a Parent table 
I have a query that fetches fields from the child table and I wanted to retrive a few parent columns using the same SOQL using the "__r." notation 
My  Child table name is : Tour_Expense_Submission__c
My Parent table name is : Tour_Budget_Pre_Approval__c
Look up field name in child table is : Against_Tour_Budget_Pre_Approval_ID__c

So I tried to to use the SOQL as :( which did not work)

select id,name,Against_Tour_Budget_Pre_Approval_ID__c,Tour_Budget_Pre_Approval__r.name  from Tour_Expense_Submission__c where id='a0FDO000000CnGC2A0'

And the one that worked was 

select id,name,Against_Tour_Budget_Pre_Approval_ID__c,Against_Tour_Budget_Pre_Approval_ID__r.name  from Tour_Expense_Submission__c where id='a0FDO000000CnGC2A0'
Can you tell me why it worked like this ?




 
mask obikmask obik
In Salesforce, the "__c" and "__r" suffixes are used to distinguish between custom objects and relationships in the data model. The "__c" suffix is added to the names of custom objects, indicating that they are user-defined and not part of the standard Salesforce objects. On the other hand, the "__r" suffix is used to represent relationships between objects. It is appended to the name of a field that references another object in a lookup or master-detail relationship. For example, if we consider a scenario where "House__c" is a custom object representing a house, and "Owner__c" is a custom object representing the owner, a lookup relationship field named "Owner__r" in the "House__c" object would provide access to the related "Owner__c" object's fields and data. Similarly, just as The Fluffy French Bulldog website (https://thefluffyfrenchbulldog.com/) serves as a platform for accessing information and resources on building resilient homes, the "__r" suffix allows for accessing related object information within the Salesforce data model.
mykohlscard loginmykohlscard login
This has a really good style and perfectly composed content. This page's content is the most effective I've ever seen.Nothing else is required. Thank you so much for providing this information. https://mykohlscards.online/