There are six Events created for Opportunity 'ABC' with the same Subject name and I need to count all these Events based on Status field.

I am using the below query for fetching the Events but it returns only one Event. The first event was created in 2018 and four events in 2019 and one in 2020, the query returns the event which is created in 2020.

"SELECT Id,(SELECT Id,Event_Status__c FROM Events) FROM opportunity where id='xxxxxxxxxx'"

How I can count all these events by using apex, please suggest. Any help would be appreciated.

  • Is this your original query? This query is straight forward which should return all of your event irrespective of Event Status or Subject Oct 19 '20 at 10:53
  • Yes, In spite of removing status filter it only returns one event of this year
    – jsmith
    Oct 19 '20 at 11:14
  • Even, "select id from Event where WhatId ='oppid' this query returns only one record.
    – jsmith
    Oct 19 '20 at 11:32

Your activities are archived. See Work with Archived Activities and Archived Activities in the SOAP API reference for more details.

Regular queries do not include archived activities. In SOQL, you can query for archived activities by including the ALL ROWS clause.

SOQL statements can use the ALL ROWS keywords to query all records in an organization, including deleted records and archived activities. For example:

  • Thank you So much, David, for your help. By using "SELECT Id FROM Event WHERE WHATID = 'xxxxx' AND isDeleted = FALSE ALL ROWS" this query gives the count but it gives one extra count means there are six visible events under activity but got count seven please clarify.
    – jsmith
    Oct 20 '20 at 3:07
  • I can't see your data, @jsmith; I'm not sure how to answer that question
    – David Reed
    Oct 20 '20 at 3:30
  • Thanks a lot, David!
    – jsmith
    Oct 20 '20 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.