Find your content:

Search form

You are here

Querying Tasks within a trigger

 
Share

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.


Attribution to: Andy Schwartz

Possible Suggestion/Solution #1

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


Attribution to: Salesforce Wizard

Possible Suggestion/Solution #2

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.


Attribution to: Matt K
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3387

My Block Status

My Block Content