0

I am trying to query for others' Tasks within a trigger. (That is, retrieve Tasks whose OwnerId is other than the running user). I've reduced my query to the following:

Task[] anyTasks = [SELECT Id, OwnerId, Subject, ActivityDate
                   FROM Task
                   LIMIT 10
                   ALL ROWS];

This query is returning 0 rows. Even though I know there are plenty of open, recent tasks owned by others in this tenant.

This is a trigger, so there's way to specify "Without Sharing".

Is it not possible to query for tasks owned by others within a trigger, or am I missing something in my query?

Update: The problem is not Sharing, it's related to the fact that the running user is a Customer Portal userid. That userid, when executing Apex code, can insert Tasks records, but apparently cannot query [SELECT] those records. I am surprised, as this userid seems to have system-level permissions in Apex code - even having access to other objects that are not specifically permitted to a Portal or that profile. The Task object behavior seems unique.

2

It comes down to your sharing rules.

Personally, I would not put that query in the trigger at all. I would create a without sharing class that has my logic and "actions" and just use the trigger to pass the data over to the class to be worked on.

By putting it in the class referenced by your trigger you can do the without sharing to get other user's activities

|improve this answer|||||
  • Good idea. Just did that. Same result, no rows returned. (Did specify Without Sharing on the class.) – Andy Schwartz Oct 16 '12 at 18:35
  • Have you tried cutting/pasting your query into the developer console Execute Anon box? Do you get 0 results there too? – Salesforce Wizard Oct 16 '12 at 18:50
  • Just tried. Same query in dev console Execute Anonymous returns 33 rows. – Andy Schwartz Oct 16 '12 at 20:30
0

Triggers run as the System, so you may not be able to get the Current User.

To query all data, try removing LIMIT 10 from your query. Also, as I understand it, ALL ROWS does not query the remainder of the rows excluded by the LIMIT; it only queries Recycle Bin items or archived Activities.

|improve this answer|||||
  • Tried stripping out both Limit and All Rows. Still no rows returned. – Andy Schwartz Oct 16 '12 at 18:45

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.