Salesforce - Apex - Query Accounts Based on Transaction History

Hi Salesforce experts,

I have a question about account performance. I would like to request updates based accounts in the activityHistory object. The problem I get is that all accounts are retrieved whether or not there is a “full” active history. So, is there a way to write this query to retrieve only accounts with activeHistory that have status = "complete" and Type_for_reporting = 'QRC'?

List<Account> AccountsWithActivityHistories = [    
    SELECT
         Id
        ,Name
        ,( SELECT
                ActivityDate
               ,ActivityType
               ,Type_for_Reporting__c
               ,Description
               ,CreatedBy.Name
               ,Status
               ,WhatId
            FROM ActivityHistories
            WHERE Status ='complete'  and Type_for_Reporting__c = 'QRC'
        )
    FROM Account
];
+1
source share
1 answer

WHERE , .

, , :

SELECT Id, Name
FROM Account
WHERE Id IN (SELECT AccountId FROM Contact) // try with NOT IN too

, .

http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select.htm

:

  • ActivityHistory
  • EventAttendee
  • OpenActivity
  • (AccountTag, ContactTag )
  • Task

, ActivityHistory .

, " ", :

  • . , , "A; .
  • WHERE.
  • 499 , .
  • ActivityDate LastModifiedDate ; . : ORDER BY ActivityDate ASC NULLS LAST, LastModifiedDate DESC.

, . ( , , ), ?

, :

List<Account> finalResults = new List<Account>();
for(Account a : [SELECT...]){
    if(!a.ActivityHistories.isEmpty()){
        finalResults.add(a);
    }
}
+1

All Articles