+ Start a Discussion
Larry WasserLarry Wasser 

SOQL Query on Collaboration Group fails only in live code, not in Execute Anonymous Window or Query Tab of Developer Console

Simple SOQL Query fails to yield results in live Apex code, but succeeds in Developer Console Execute Anonymous Window and in Query Editor Tab.

public void importProgramCollaborationGroupTable(){
   List<CollaborationGroup> allCollabGroups = [SELECT Id, Name FROM CollaborationGroup];
}
Larry WasserLarry Wasser
Not sure if this will make a difference, but this was in the contact of a Batchable, Stateful Class.
Larry WasserLarry Wasser
Meant to write context not contact
AmitSoniAmitSoni
I would recommend you to use the UserInfo.getUserId() and print the user info in to lgs and check if the logged user has desired permission to fetch the CollaborationGroup data. 

Hope this will be helpful!!
Abhishek BansalAbhishek Bansal
Hi Larry,

This might be due to the missing permissions. The user that you are using to run the batch might not have accesss to those records. You have two options to make it work:
  1. Make your batch to run on without sharing mode, only if this is feasible.
  2. Give the neccessary permisisons to the user from which batch is scheduled or change the running user.
Let me know if you need any more help on this.

Thanks,
Abhishske Bansal.
SwethaSwetha (Salesforce Developers) 
HI Larry,

I see this is the same ask as https://salesforce.stackexchange.com/questions/318930/soql-query-on-collaboration-group-fails-only-in-live-code-not-in-execute-anonym 

Can you please share your inputs on whether the answer to it helped. If it does, please mark this answer as best to close the thread.Happy to answer any follow-up queries.Thank you
Larry WasserLarry Wasser
Hi Amit,

Thanks for your advice. The logs show what I was expecting. The Batch is being initiated in an Event Trigger, so the running user is autoproc@00d19000000fd64eag. Resulting logs follow below:

11:39:56.0 (398761710)|USER_DEBUG|[184]|DEBUG|UserInfo.getUserName():  autoproc@00d19000000fd64eag
11:39:56.0 (399519375)|SOQL_EXECUTE_BEGIN|[186]|Aggregations:0|SELECT Id, Name FROM CollaborationGroup
11:39:56.0 (399552908)|LIMIT_USAGE|[186]|SOQL|3|200
11:39:56.0 (399567797)|LIMIT_USAGE|[186]|AGGS|0|600
11:39:56.0 (402658123)|SOQL_EXECUTE_EXPLAIN|[186]|TableScan on CollaborationGroup : [], cardinality: 9, sobjectCardinality: 9, relativeCost 2.617
11:39:56.0 (402673607)|SOQL_EXECUTE_END|[186]|Rows:0

Additional ideas are welcome!
Larry 
 
Larry WasserLarry Wasser
Hi Abhishek,

Unfortunately, the behavior is the same regardless of whether the class is declared as with sharing or without sharing. 

I believe that I've set all the appropriate permissions, but may have missed something. Open to commonly overlooked permissions with respect to CollaborationGroups/Chatter Groups.

Thanks,
Larry 
Larry WasserLarry Wasser
Hi Swetha, 

Unfortunately, the recommendation on stack exchange did not provide forward progress. The guidance was similar to that provide by Abhishek above.

Kind regards,
Larry 
AmitSoniAmitSoni
Hi Larry,

Please check if Enable Chatter is checked for the profile of the user autoproc@00d19000000fd64eag under administritive permission.

Refer this link: https://help.salesforce.com/articleView?id=collab_pbr_profiles.htm&type=5 (https://help.salesforce.com/articleView?id=collab_pbr_profiles.htm&type=5)

Hope this will be helpful!!

 
Larry WasserLarry Wasser
Hi Amit,

Unfortunately, autoproc is a system user that cannot be edited. However, we did have success with this kind of activity outside of a batch context. I'm wondering if the combination of Batchable, Event Trigger, and CollaborationGroup is uncovering a strange issue.

Thanks.
Larry 
Larry WasserLarry Wasser
Update: 

The issue appears to be directly related to the Automated User (i.e. autoproc@....). 
Just filed a case with Salesforce support.

Larry