I'm a bit new to SF development (mostly experienced in Marketing Cloud), but I'm working on an Apex class and I had a question about the data that would be returned from one of it's functions.

Let's say I have a series of custom objects that are being collected within queues (which should relate to each location of our business), and I only want to return records that are available in the queues that the user of my lightning component has access to.

So, if I have something like:

public with sharing class SampleClass {
    public static List<Prospect__c> getRecipients() {
            return [SELECT Id, Status__c FROM Prospect__c];

Which of these would be the result from the above function:

  1. Only records from the assigned queue the user is in will be returned (the desired outcome).

  2. No records will be returned and error will be thrown.

  3. All records from the Prospect object will be returned irregardless of that user's access to view those records

  4. Something else / Not enough info


Door Number 3. If you do not include any query filters, every record you can see will be returned. However, if you add a USING SCOPE clause, it should do what you are looking for:

Filter for records assigned to the user running the query and the user’s queues.

Making your query:

SELECT Status__c FROM Prospect__c USING SCOPE MineAndMyGroups

Note that SOQL from Apex will always include the Id field, and hence it is not necessary to include explicitly.

  • 1
    This is exactly what I was looking for. Much appreciated and will accept this as the answer as soon as I am able. Jun 4 '19 at 19:29
  • Did you ever use this query, because I don't think apex using that scope will compile? salesforce.stackexchange.com/questions/254817/…
    – James
    Aug 2 '19 at 15:34
  • @James You are correct, this never compiled. In theory, it sounds like the perfect solution but we had to come up with a workaround to push this to production. Oct 15 '19 at 0:00

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.