0

Background:
I need some sort of "Category" option for our Collaboration Groups for our community, and Salesforce doesn't offer custom fields or categories of any sort on CollaborationGroup object.


To combat this problem, I've created a Custom Object (Community_Group_Category__c) with the following fields:

  1. Category__c (Picklist)
  2. Active__c (Boolean)
  3. Collaboration_Group_Id__c (Text, 18 digits unique case sensitive, holds related CollaborationGroup Id)
    • Could not be lookup since CollaborationGroup isn't available for lookup relationships.

I'm trying to create a map of type Map<String,List<CollaborationGroup>> where Category__c from Community_Group_Category__c is the String key, and the list of CollaborationGroup is any group related to that Category__c (using Collaboration_Group_Id__c on Community_Group_Category__c object).

I've been thinking about this for a few hours and keep changing my map structures to try to accommodate this but am having trouble visualizing how to get my intended end result. Thought maybe someone else can help stoke this fire and get me on the right track.


Here is my latest attempted code, I've built a Map<String,List<Community_Group_Category__c>> but now I'm unsure of how (if at all) I can use this map to my advantage while looping through CollaborationGroup and attempting to build another map of type <String,List<CollaborationGroup>> where the String is Category__c from my custom object - The Id's that I want to match to CollaborationGroups while looping are in the .values() of the catNameToCatMap map collection:

@AuraEnabled 
public static Map<String, List<CollaborationGroup>> getCategories(){
Set<String> groupCatSet = new Set<String>();
Map<String,List<Community_Group_Category__c>> catNameToCatMap = new Map<String,List<Community_Group_Category__c>>();
Map<String,List<CollaborationGroup>> catNameToGroupMap = new Map<String,List<CollaborationGroup>>();

for(Community_Group_Category__c cgc : [SELECT Id,Name,Active__c,Category__c,Collaboration_Group_Id__c FROM Community_Group_Category__c]){
    if(cgc.Active__c){
        if(!catNameToCatMap.containsKey(cgc.Category__c)){
            catNameToCatMap.put(cgc.Category__c,new List<Community_Group_Category__c>());
        }
        catNameToCatMap.get(cgc.Category__c).add(cgc);
    }            
}
System.debug('MM catNameToCatMap KeySet: ' + catNameToCatMap.keySet());
System.debug('MM catNameToCatMap Values: ' + catNameToCatMap.values());


for(CollaborationGroup cg : [SELECT Id,Name,MemberCount,CollaborationType,Description,CanHaveGuests,NetworkId FROM CollaborationGroup WHERE CollaborationType = 'Public']){
    // How Should I build a Map of cgc.Category__c,List<CollaborationGroup>> ??  
}
0

You can do it by mapping collaboration group id to category and looping through collaboration group and creating a map of collaboration group id and list of collaboration group.I am assuming Collaboration_Group_Id__c is a unique field and it refers to the id of the collaboration group,

@AuraEnabled 
public static Map<String, List<CollaborationGroup>> getCategories(){

Map<String,string> categoryCGroupMap  = new Map<String,string>();
Map<String,List<CollaborationGroup>> catNameToGroupMap = new Map<String,List<CollaborationGroup>>();

for(Community_Group_Category__c cgc : [SELECT Id,Name,Active__c,Category__c,Collaboration_Group_Id__c FROM Community_Group_Category__c]){
    if(cgc.Active__c&&cgc.Category__c!=null){
    //Create a map of collaboration group id and category
       categoryCGroupMap.put(cgc.Collaboration_Group_Id__c,cgc.Category__c);
    }            
}



for(CollaborationGroup cg : [SELECT Id,Name,MemberCount,CollaborationType,Description,CanHaveGuests,NetworkId FROM CollaborationGroup WHERE CollaborationType = 'Public']){
  //Check whether categoryCGroupMap contains the collaboration group id
  if(categoryCGroupMap.containskey(cg.id))
  {
  //check if the map already contains a category
    if(catNameToGroupMap.containskey(categoryCGroupMap.get(cg.id))
    {
        List<CollaborationGroup> cglist= catNameToGroupMap.get(categoryCGroupMap.get(cg.id));
        cglist.add(cg);
        catNameToGroupMap.put(categoryCGroupMap.get(cg.id),cglist);
    }
    else
    {
        List<CollaborationGroup> cgList2=new List<CollaborationGroup>();
        cgList2.add(cg);
        catNameToGroupMap.put(categoryCGroupMap.get(cg.id),cgList2);
    }
  }
}
}

But instead of id you can use collaboration group name and make it as an unique field on the newly created custom object as there is no need to update the id in the custom object when deploying to production

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.