I am getting the following error when I run a Salesforce SOQL query with a standard account.

SELECT  ( SELECT Id, Subject FROM OpenActivities ORDER BY ActivityDate
ASC , LastModifiedDate DESC  LIMIT 500) FROM Account WHERE Id in (
'001b0000009owSN', '001b0000009owNo' )

MALFORMED_QUERY: Implementation restriction: activity aggregate relationships only allow security evaluation for non-admin users when a single parent record is evaluated

Same query under Admin account works well.

Similar query works well under Standard account:

SELECT  ( SELECT Id, Name FROM Contacts ORDER BY Email ASC  LIMIT 500)
FROM Account WHERE Id in ( '001b0000009owSN', '001b0000009owNo' )

How can the problematic query be re-written? Why does that limitation exists?



Do you know if you are using with sharing on the apex classes?

The implication from SOQL Query ActivityHistory Problem is that switching to the System context will bypass the non-admin user restriction.

As a guess, I'd say it is too expensive for Salesforce to evaluate if a non-admin user has access to the referenced ActivityHistory records. Where is an admin doesn't require the same level of access checking.

Failing that, can you split the query into two parts or query ActivityHistory rather than OpenActivities?

Set accountIds = new Set(); accountIds.add('001b0000009owSN'); accountIds.add('001b0000009owNo'); // What fields do you actually want from Account? List accounts = [Select Id, Name from Account where ID in :accountIds]; List activities = [Select Id, AccountId, Subject FROM OpenActivities ORDER BY ActivityDate ASC, LastModifiedDate DESC WHERE AccountId in :accountIds LIMIT 500];

See also OpenActivity - Usage:

The following restrictions on users who don’t have “View All Data” permission help prevent performance issues:

  • In the main clause of the relationship query, you can reference only one record. For example, you can’t filter on all records where the account name starts with ‘A’; instead, you must reference a single account record.
  • You can’t use WHERE clauses.
  • You must specify a limit of 499 or fewer on the number of rows returned in the list.
  • You must sort on ActivityDate in ascending order and LastModifiedDate in descending order; you can display nulls last. For example: ORDER BY ActivityDate ASC NULLS LAST, LastModifiedDate DESC.
  • Regarding the "with sharing", the SOQL above is used via API and not in APEX.
    – R.Epstein
    Apr 8 '14 at 22:14
  • Naturally, this is a Salesforce performance restriction. But, why would the SAME SOQL structure work for other objects as notes in the question? Why would this object be treated differently?
    – R.Epstein
    Apr 8 '14 at 22:14
  • re: Spliting: not sure what you mean.
    – R.Epstein
    Apr 8 '14 at 22:17
  • Querying other objects: maybe it wll work, but the same problem exists with other objects as well. For example the following query also fails with the same error: SELECT ( SELECT Id, Subject, Who.Name, What.Name, IsTask, ActivityDate, Owner.Name, LastModifiedDate, Account.Name FROM ActivityHistories ORDER BY ActivityDate DESC , LastModifiedDate DESC LIMIT 500) FROM Account WHERE Id in ( '001b0000009owSN', '001b0000009owNo' )
    – R.Epstein
    Apr 8 '14 at 22:17
  • re the "...reference only one record...", implies that queries that do work, shouldn't. For example, the 2nd example in the original question
    – R.Epstein
    Apr 8 '14 at 22:20

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.