single,single-post,postid-1828,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive




Relationship between objects is 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 an 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 ones 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.

[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…

Courtesy: http://www.forcetree.com/2009/07/understanding-relationships-in.html

“Know more about Salesforce Consulting at Techila”

Author: techila

No Comments

Post A Comment