1

I am new to salesforce can anyone help me with the trigger. I have tried it but not able to fulfill the requirement. Please help me any suggestions are appreciated. Requirement: A company wants to do collaboration using chatter group for project. Whenever Enable Collaboration checkbox is checked a respective chatter group should be created. Note: Only one chatter group should exist for a Project.

Trigger:

trigger ChatterGroupCreation on Project__c (after insert,after update) {

if(Trigger.isAfter && Trigger.isInsert){
    ChatterGroupCreationHandler.isProjectInsert(Trigger.new);
}
if(Trigger.isAfter && Trigger.isUpdate){
    ChatterGroupCreationHandler.isProjectUpdate(Trigger.new,Trigger.oldMap);
}

Handler:

public class ChatterGroupCreationHandler {
static set<Id> projecthavingchatter=new set<Id>();
public static void isProjectInsert(List<Project__c> listProject){
    System.debug('listProject'+listProject);
    List<CollaborationGroup> chattergroup=new List<CollaborationGroup>();
    for(Project__c proj:listProject){
        if(proj.Enable_Collaboration__c==true){
            CollaborationGroup myGroup = new CollaborationGroup();
            myGroup.Name=proj.Name+proj.Id;
            myGroup.CollaborationType='Public';                   
            chattergroup.add(myGroup);
            projecthavingchatter.add(proj.Id);
        }
    }
    System.debug('projecthavingchatter'+projecthavingchatter);
    System.debug('chatter'+chattergroup);
    insert chattergroup;
}


public static void isProjectUpdate(List<Project__c> listProject,Map<Id,Project__c>oldMapProject){
    System.debug('projecthavingchatter'+projecthavingchatter);
    List<CollaborationGroup> chattergroup=new List<CollaborationGroup>();
    for(Project__c proj:listProject){
        Project__c oldProj=oldMapProject.get(proj.Id);
        if(oldProj.Enable_Collaboration__c==false && proj.Enable_Collaboration__c==true && !projecthavingchatter.contains(proj.Id)){

            CollaborationGroup myGroup = new CollaborationGroup();
            myGroup.Name=proj.Name+proj.Id;
            myGroup.CollaborationType='Public';
            chattergroup.add(myGroup);
        }
    }
    system.debug('chatter update'+chattergroup);
    try{
        insert chattergroup;
    }
    catch(Exception e){

    }
}

}

The problem is if I have created 2-3 projects of same name then I cannot create a different chatter group for each. And also whenever I turn the checkbox from false to true in update it should check if there is chatter group created or not. Please help me. I have updated the code based on your suggestions can anyone tell me if its correct or not

4
  • You can create a flag / checkbox field to mark whenever a project's chatter group is created. (when the first checkbox is checked and the chatter group creation checkbox is not for example)
    – Atlas Can
    Nov 14, 2019 at 10:16
  • If I add flag the in bulk it will not work
    – maria
    Nov 14, 2019 at 12:02
  • Are you sure this is what they want. Chatter groups have some limitations. A user can only be a member of 300 groups and a company can have "only" 30.000 groups. There is a chatter feed at the top of a project. Can that not be used? Nov 14, 2019 at 13:15
  • No the I have to use chatter groups only . But I am not able to check it in update whether it has created chatter or not
    – maria
    Nov 14, 2019 at 13:52

1 Answer 1

1

You have to make the chatter group unique, for example

myGroup.Name = proj.Name + ' - ' + proj.Id;

In such a case you'll be able to create chatter groups for the projects with the same name.


For handling update operation you there is 2 option:

1) on update operation just collect all the unique combinations for a chatter group name (project name + Id) and query chatter groups with these names -> see what groups already exist and create missing ones

public static void isProjectUpdate(List<Project__c> listProject,Map<Id,Project__c>oldMapProject){

    Map<String, Project__c> chatterGroupNameToProject = new Mapm<String, Project__c>();

    for (Project__c p : listProject) {
        Project__c oldProj = oldMapProject.get(p.Id);
        if (oldProj.Enable_Collaboration__c == false && p.Enable_Collaboration__c == true && !projecthavingchatter.contains(p.Id)) {
            String quniqueChatterGroupName = p.Name + ' - ' + p.Id;
            chatterGroupNameToProject.put(quniqueChatterGroupName, p);
        }
    }

    if (chatterGroupNameToProject.isEmpty()) {
        return;
    }

    for (CollaborationGroup cg : [SELECT Name FROM CollaborationGroup WHERE Name IN :chatterGroupNameToProject.keySet()]) {
        // Means that group already exist
        chatterGroupNameToProject.remove(cg.Name);
    };

    List<CollaborationGroup> chattergroups = new List<CollaborationGroup>();
    for (Project__c proj : chatterGroupNameToProject) {
        CollaborationGroup myGroup = new CollaborationGroup();

        String quniqueChatterGroupName = p.Name + ' - ' + p.Id;

        myGroup.Name = proj.Name+proj.Id;
        myGroup.CollaborationType = 'Public';
        chattergroups.add(myGroup);
    }
    system.debug('chatter update'+chattergroups);
    if (!chattergroups.isEmpty()) {
        insert chattergroups;
    }
}

2)You could use Database.SaveResult[] insertResults = Database.insert(chatter, false); method with false, if the group name already exists - the group will not be created(see docs). But here you have to process the result of the insert call insertResults, to see if there are any error except the error for chatter group that the name is not unique

public static void isProjectUpdate(List<Project__c> listProject,Map<Id,Project__c>oldMapProject){

    List<CollaborationGroup> chattergroups = new List<CollaborationGroup>();

    for (Project__c p : listProject) {
        Project__c oldProj = oldMapProject.get(p.Id);
        if (oldProj.Enable_Collaboration__c == false && p.Enable_Collaboration__c == true && !projecthavingchatter.contains(p.Id)) {
            CollaborationGroup myGroup = new CollaborationGroup();

            String quniqueChatterGroupName = p.Name + ' - ' + p.Id;

            myGroup.Name = proj.Name+proj.Id;
            myGroup.CollaborationType = 'Public';
            chattergroups.add(myGroup);
        }
    }

    if (!chattergroups.isEmpty()) {
        Database.SaveResult[] insertResults = Database.insert(chattergroups, false);

        for (Database.SaveResult res : insertResults) {
            if (!res.isSuccess()) {
                for (Database.Error e : res.getErrors()) {
                    // it means that the error is not related to a duplicate name and you have to handle it
                    if (e.getStatusCode() != StatusCode.DUPLICATE_VALUE) {
                        // handle the error
                    }
                }
            }
        }
    }
}
3
  • Can you please explain the update logic . Actually I am not getting what are you trying to say
    – maria
    Nov 14, 2019 at 13:15
  • @maria I've updated the answer with the examples Nov 15, 2019 at 13:17
  • Thank you for your help
    – maria
    Nov 18, 2019 at 6:23

You must log in to answer this question.

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