18

I read this article for difference between __r and __c. But I still can't understand how is Hiring_Manager__c.Email will be different from Hiring_Manager__r.Email

enter image description here

Can anyone explain this?

Thanks in advance

18

__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
}

Each mechanism has a specific purpose depending on your intent.

  • Thanks @sfdcfox. That was a simple and detailed explanation. But can you tell me why is there need for two SELECTS in parent to child relationship? – MnZ Mar 27 '14 at 12:28
  • 1
    Sounds like you have a bit of reading to do before you will understand as this fully as this is foundational type knowledge. But, you need the second select (subquery) to return the children of the parent using the relationship as the key. There is a lot of documentation related to subqueries. – Eric Mar 27 '14 at 13:49
  • 1
    @MnZ It's called a sub-query, of which there are two types: the former is as I showed here, to query children from a parent. The latter lets you query a set of records based on another set of records, such as: SELECT Id, Name FROM Account WHERE Id IN (SELECT AccountId FROM Contact WHERE HasOptedOutOfEmail = TRUE). It's of limited usage, but when it is useful, it is very useful indeed. – sfdcfox Mar 28 '14 at 2:52
  • I now understand and can write parent to child and child to parent query. Thanks @sfdcfox and Eric – MnZ Apr 18 '14 at 15:26
8

__c is for Custom objects For example: Custom_Object__c . It is used for reference custom object in Apex or visualforce page, formula field etc internally. Used as suffix.

__r is for Custom objects reference For example: Custom_Object__r . It is used for reference custom object relation ship name in Apex or visualforce page, formula field etc. Used as suffix.

In image you have posted: Hiring_Mansger__r is relationship name.

Native object don't require __c as suffix.

2

I think the easiest way to think about this is that the relationship name might not be the same as the object name. For example, if you have a custom object Car_Market_User__c, you might have an object Car__c, with a Buyer__r and Seller__r. Typically, however, the name is the same, which is why it's auto generated based on the model name as Car_Market_User__r.

1

__r is used in case of accessing relationship fields. Like in your case Hiring Manager is the Parent of Position object. So if you want to access parent (Hiring Manager) object fields from child object(Position) then you must use lookupfieldName__r.fieldAPIname Hiring_Manager__r.Email

  • When do we need to access relationship fields? I mean in what condition? Can't we do it by r"__c"? – MnZ Feb 25 '14 at 7:42
1

Just happen to come across this post and thought of adding my commentary in hope to further help MnZ. I would just try to clarify a bit more to what sdfcfox and others have already explained.

__c refers directly to a custom object whereas __r refers to a reference (a relationship) between two objects such as the parent-child object relationship or child-parent object relationship in order to help you 'get' or 'navigate' to the parent from a child or vice versa.

An Example

Let us say you have a Class__c parent object (one) with a child relationship to Student__c objects (many). Now, you want to get the names of the students, and for each student, the name of the class associated with that student. You can use __r relationship to get to Class__c when querying Student__c.

In psuedo code, you may say:

Get names of students and the class name associated with each student's name

You could rephrase this in sdfc lingo like this:

Use Student__c object to get the names of the students and use student-to-class / child-to-parent relationship (Student__c.Class__r) to get the associated name of the class

Using __c and __r , the above will translate into something like:

Select Student__c.Name, Class__r.Name from Student__c;

Another Example - getting to students from a class using __r

Get class name from Class__c and the names of all students associated with the class using Class_c.Students__r

Select Class__c.Name, (many students for a given class using Class__c.Students__r) from Class__c 

or

Select Class__c.Name, (Select Name from Students__r) from Class__c;
  • thank u very much; yes it d0es make sense :) – MnZ Dec 10 '14 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.