+ 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???