Monday, September 2, 2019

SOQL To Fetch All Tasks Related to Account & Opportunity


When we write query to get details from Parent object also from a child object SOQL, we 

know that the particular child record is surely associated with a specific Parent Object. For 

Example we know there is a relationship between Account(Parent)

Opportunity(Child)  Object. So that means every Opportunity is related with an 

Account object for sure.


But in case of Task Or Event object, As these objects are related with multiple objects like 

Account,Contract,Campaign,Opportunity,Product,Asset,Case,Solution,Quote 

and Any Custom Objects we can't guarantee that a Task/Event record is related with 

which of the previously mentioned objects. So for Task/Event object, the parent object 

relationship is not static. This type of relationship is known as Polymorphic 

Relationship.


Requirement :  SOQL to fetch all Tasks Related to Account & Contact

Solution:  As we know Task records can be created with multiple objects including 

Account & Contact, we need to take help of WhatId field used in Task object. Using Type 

qualifier on WhatId, we can achieve the same. The Type qualifier determines the Parent 

object Type which is associated with any Task Or Event record.

             




1 comment:

  1. Can you please help me. Should we able to fetch all records from parent and child or look up relationship only.

    ReplyDelete