Relationship Query In Salesforce
In this blog we will learn about how to build relationship query in salesforce. In salesforce, every object and field has a default Label, a Name, and an API Name.
- Lable: Object or filed name which is shown to user.
- Name: Developer-/Admin-defined internal field name in custom objects.
- API Name: It is unique name which is used by developers in code to perform various operation. In custom Object, it is derived from name and a suffix while in standard object label and API name are same.
We use the API name in query to fetch the desired records. Let’s see the various suffix used in salesforce for custom objects.
Suffix
1). __x : For external object (Not in Salesforce multitenant database but still support the SOQL).
2). __Share: The sharing object.
3). __Feed: The chatter feed.
4). __latitude__s/__longitude__s: Geolocation Latitude/Longitude coordinate fields.
5). __r : To represent relationship between object.
6). __tab : For custon tab
7). __c : Suffix used for custom object name.
.
Relationship query
Now let’s see some SOQL for parent-child relationship.
1). SOQL for two standard object. Let’s say Account as parent object and Contact as child object .
-> Query to fetch the all Contact which is associated with Account ‘Webkul’.
// When record is fetch from Contact SELECT Name, Account.Name FROM Contact where Account.Name = 'Webkul' // When record is fetch from Account. // Contacts is child relationship name of Contact object. SELECT Name, (SELECT Name FROM Contacts) FROM Account where Name = 'Webkul'
# Output
2). When one is standard object and one is custom object.
-> Let’s say we have a custom object with name Product_Variant__c and standard object product with name product2 which is parent of variant. Suppose we want to know the name of the variant where product name is shirt.
// When record is fetch from product // Product_Variants is child relationship name of Product_Variant__c // __r relationship suffix of custom object SELECT Name, (SELECT Name FROM Product_Variants__r) FROM Product2 WHERE Name = 'Shirt' // When record is fetch from Product_Variant__c // Product__r is relationship name of custom field Product__c SELECT Name,Product__r.Name FROM Product_Variant__c WHERE Product__r.Name = 'Shirt'
#Output
3). SOQL for two custom objects. Let’s say we have two custom object Variant with name Variant__c (Parent) and Product Variant with name Product_Variant__c(Child). As shown in above example Product Variant is also the child of product.
-> Query for the name of Variant where product name is Shirt.
// When record is fetch from Product Variant. // Variant_c is the name of custom field Variant__c which is lookup of custom object Variant_c SELECT Name, Variant__r.Name FROM Product_Variant__c WHERE Product__r.Name = 'Shirt'
#Output
Support
That’s all for Relationship Query In Salesforce, still if you have any further query feel free to contact us, we will be happy to help you https://wedgecommerce.com/contact-us/.