1

I have to create functionality to automatically add new users to a chatter group and follow it. I'm working from Bob Buzzard's example from here.

I want to have a custom setting store the group names so I can add/remove groups to auto-follow on demand without code changes so I created a list custom setting called "Chatter_Auto_Follow__c" with a custom text field called "Chatter Group".

My problem is that I cannot figure out how to query for a list of the names and then pass that into a CollaborationGroup query for the ids.

Here is a snipit that I'm working with:

// Find the group name from the custom setting
    Chatter_Auto_Follow__c setting = Chatter_Auto_Follow__c.getall().values();
    String groups = setting.Chatter_Group__c;

// Find the Chatter group Ids from the names of the custom setting
    List<CollaborationGroup> chatterGroups = [SELECT id, Name FROM CollaborationGroup WHERE Name in :groups];    

When I try to compile I get the error on my collaborationgroup query

IN operator must be used with an iterable expression

What am I doing wrong?

1
  • String is not a "container" class such as array/list/map/etc. looks like you only have a single group and not multiple groups if that's the case you can just where name = :groups or you need to change groups to List<String> and populate the list with multiple values
    – Halfwarr
    Apr 7, 2017 at 19:16

1 Answer 1

1

The getAll of the custom setting method returns a Map of the name to its custom setting.

You will need to iterate over them and populate a collection to include in your query:

Chatter_Auto_Follow__c[] settings = Chatter_Auto_Follow__c.getall().values();
Set<String> groups = New Set<String>();

for(Chatter_Auto_Follow__c cs : settings)
    groups.add(cs.Chatter_Group__c);

List<CollaborationGroup> chatterGroups = [SELECT id, Name FROM CollaborationGroup WHERE Name in :groups];

You could simplify it by just using the Name field of the custom setting to store the group name then you could simply do:

List<CollaborationGroup> chatterGroups = [SELECT id, Name 
  FROM CollaborationGroup 
  WHERE Name in :Chatter_Auto_Follow__c.getall().keySet()];
3
  • I'm a huge fan of simplifying. Because my field "Chatter_group__c" is the same as the name if I go this route I don't even need the field right? Just add with the name only? Apr 7, 2017 at 19:22
  • @DanWooding - Right. Only problem is that you cannot change the "Name" label so it does not make it user friendly....Also, Name is Unique in relation to the custom setting. You cannot have two records with the same Name
    – Eric
    Apr 7, 2017 at 19:23
  • Okay, I think in this use case I might be okay with uniqueness. Thanks. Apr 7, 2017 at 19:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .