Understanding relationships in Salesforce

3:35 AM

Relationship between objects are created when you create a field of type Look-up... Of course, Salesforce standard objects come with their own built-in relationships.. Let's categorize relationships into two categories...

  • Standard Relationship
  • Custom Relationship
We all know how a relationship looks like in the UI, for ex: Account - Contact relationship. You can see that one Account can have many Contacts associated to it . When you create or edit a Contact you will have to select a Account from the look-up dialog to establish a relationship...

Now, that's all what we know.. But at the database level there is much more to be explored..

Standard Relationship

Standard relationships are the one's that come pre-built between Standard objects in Salesforce.

Let's understand the Account-Contact relationship.. In Contact you can find a field called Account.. This field establishes the relationship.. When you view a Contact record you will see the Account Name populated in this field... But guess what, that is not actually stored in the database..

The ID of the Account is stored in the look-up field. Hence, all look-up fields would store the ID.

Here is how the actual database fields are....

AccountId - This stores the ID of the Account.
Account -- This stores the relationship of the Account.

So, here come's the interesting part. By using the "Account" relationship you can access any field of the associated Account.

[Select Account.Name,Account.BillingCity from Contact]


And what more, you can retrieve data from a relationship to a relationship as well...

[Select Account.Name,Account.BillingCity,Account.Owner.Name from Contact]


So, they can be multi-level....

What's the advantage??

The better you understand relationships, the better you can construct complex nested queries and try to minimize the number of queries used.

Custom Relationship:

This is same as the standard relationship except that the naming convention has some differences...

THE __c and __r :

In our same example, if we create a custom Account look-up field on Contact and name it as CustomAccount... then accessing the relationship is as follows...

CustomAccount__c --> Equivalent of AccountId, holds the ID of the Account.

CustomAccount__r --> Equivalent of Account, holds the relationship

Our example query will modify as below..

[Select CustomAccount__r.Name,CustomAccount__r.BillingCity,CustomAccount__r.Owner.Name from Contact]



Wondering how i came to know about this and where will you find more details on this????

Go to Setup--> Develop --> API and click on "Generate Enterprise WSDL"

The WSDL shows the actual way data is stored, the actual field names, the actual relationships etc...

6 comments

  1. Nice write up.

    And good call on looking at the Enterprise WSDL to see how everything translates. Referring to that helps answer a lot of questions.

    ReplyDelete
  2. >Some people tend to retieve the AccountId from the Contact object, then using this AccountId query the Account object to get the account details... that is obviously a nasty method when you can do all this in a single query.....


    No I don't think anybody does that.

    ReplyDelete
  3. Agreed!! I have to admit that my writing was too bad those days.. I have re-phrased the sentences in the comment above..

    ReplyDelete
  4. any idea what is __s? and why it is used?

    ReplyDelete
  5. Thank you! That has helped me a lot...

    ReplyDelete

AD