简体   繁体   English

SOQL-查询当前用户正在关注的用户列表

[英]SOQL - Querying for a list of users the current user is following

In my app I display a list of the current users. 在我的应用程序中,我显示当前用户的列表。 I request it like this: 我这样要求:

Attempt 1 尝试1

List<User> Following = [SELECT Id, Name, SmallPhotoUrl 
  FROM User 
  WHERE Id IN (
      SELECT ParentId 
      FROM EntitySubscription 
      WHERE SubscriberId = :UserInfo.getUserId()) 
    AND Id != :UserInfo.getUserId() 
  LIMIT 96];

This does exactly what it's supposed to when logged in as an admin but I found out that non-admins get an error: 这完全符合以管理员身份登录时的预期功能,但我发现非管理员会收到错误消息:

Implementation restriction: EntitySubscription only allows security evaluation for non-admin users when LIMIT is specified and at most 1000 实施限制:仅在指定LIMIT且最大为1000时,EntitySubscription才允许对非管理员用户进行安全评估

OK, no big deal, I'll just slap a LIMIT on there like so: 好的,没什么大不了的,我会像这样在上面打个限制:

Attempt 2 尝试2

List<User> Following = [SELECT Id, Name, SmallPhotoUrl 
  FROM User 
  WHERE Id IN (
      SELECT ParentId 
      FROM EntitySubscription 
      WHERE SubscriberId = :UserInfo.getUserId() 
      LIMIT 1000) 
    AND Id != :UserInfo.getUserId() 
  LIMIT 96];

Easy, right? 容易,对吗? WRONG. 错误。 This gives the following: 这给出了以下内容:

expecting a right parentheses, found 'LIMIT' 期望右括号,找到“ LIMIT”

OK... 好...

I then tried breaking it out like so: 然后,我尝试像这样将其分解:

Attempt 3 尝试3

List<EntitySubscription> sub = [SELECT ParentId 
  FROM EntitySubscription 
  WHERE SubscriberId = :UserInfo.getUserId() 
  LIMIT 1000];
List<Id> ids = new List<Id>();
for(EntitySubscription s : sub){
    ids.add(s.ParentId);
}
List<User> Following = [SELECT Id, Name, SmallPhotoUrl 
  FROM User 
  WHERE Id IN (:ids) 
    AND Id != :UserInfo.getUserId() 
  LIMIT 96]; 

I crossed my fingers and... 我交叉了手指,然后...

Invalid bind expression type of LIST<Id> for column of type Id

Hmm, I'd seen examples where this seemed to be possible, such as on the developerforce boards , so I'm at a bit of a loss now. 嗯,我看过一些可能的例子,例如在developerforce板上 ,所以现在我有点茫然。

So here we are. 我们在这里。 I need to select a list of user names and pictures that a particular user is following on Chatter. 我需要选择特定用户在Chatter上关注的用户名和图片的列表。 If there is a completely different way to go about it I'm open to it. 如果有完全不同的解决方法,我会接受的。

Try removing the parenthesis around the bind, ie change: 尝试删除绑定周围的括号,即更改:

WHERE Id IN (:ids) 

to: 至:

WHERE Id IN :ids

And also simplify the query by just making sure that your list of IDs doesn't contain the current user's ID: 通过确保您的ID列表不包含当前用户的ID,还可以简化查询:

List<EntitySubscription> sub = [SELECT ParentId 
  FROM EntitySubscription 
  WHERE SubscriberId = :UserInfo.getUserId() AND ParentId != :UserInfo.getUserId()
  LIMIT 1000];

Set<Id> ids = new Set<Id>();

for(EntitySubscription s : sub){
    ids.add(s.ParentId);
}

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM