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
Saurabh SmartySaurabh Smarty 

Query not giving currect results

Hi All,

I am trying to get a list of all opportunities who have associated events and the event status is completed, below is the query I made :

SELECT Id,Name,Date_of_Last_Completed_Event__c,(select id, Status__c, EndDateTime from events where Status__c = 'Completed') from Opportunity where Date_of_Last_Completed_Event__c = null

While running the query in query editor its fetching those opportunites as well who have no associated events which is not needed and because of this its fetching more than 1 lac records. I am not sure what is wrong here, any help is greatly appreciated !!!
RKSalesforceRKSalesforce
Hi Saurabh,

Please try below query:
SELECT Id,Name,Date_of_Last_Completed_Event__c,(select id, Status__c, EndDateTime from events where Status__c = 'Completed' AND WhatId != null) from Opportunity where Date_of_Last_Completed_Event__c = null
PLease mark as best answer if helped.

Regards,
Ramakant