Difference between __c and __r?

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

Answer

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

Attribution
Source : Link , Question Author : MnZ , Answer Author : sfdcfox

Leave a Comment