function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TCS CRMOnDemandTCS CRMOnDemand 

SOQL query for fetching all tasks whose WhatId is pointing to a Opportunity

Task->WhatId field is a reference to field pointing to either Account / Case / Opportunity / ..... Now i need to get all tasks whose WhatId is pointing to a Opportunity ,

Now the SOQL query that can be used for retrieval of same would be as follows

Option1

select Id,WhatId from Task where WhatId like '006%' - but this query returns error because like operator can be used only for string comparison and WhatId is a field of type ID

 

Option2

select Id,WhatId from Task where WhatId = '006%' - but this query returns no records as % is not treated to be wild char. Would like to know work around for getting all tasks having whatid pointing to a Opportunity ,

 

NOTE : - 006 is starting 3 chars of any opportunity sfdc ID

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
There's special magic in SOQL, and it goes like this:

Code:
select id, whatid from task where what.type = 'Opportunity'

Hope this helps...

All Answers

sfdcfoxsfdcfox
There's special magic in SOQL, and it goes like this:

Code:
select id, whatid from task where what.type = 'Opportunity'

Hope this helps...

This was selected as the best answer
TCS CRMOnDemandTCS CRMOnDemand
Thanks, It works
 
Can you please provide me with reference docs link from which you got to know about What.Type relantionship as we couldnt find any in Apex Guide or Apex Explorer ....



Message Edited by TCS CRMOnDemand on 12-13-2007 05:59 AM
Naresh KaneriyaNaresh Kaneriya
Thanks, It Work Perfectly