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> chatter=newchattergroup=new List<CollaborationGroup>();
for(Project__c proj:listProject){
if(proj.Enable_Collaboration__c==true){
CollaborationGroup myGroup = new CollaborationGroup();
myGroup.Name=proj.Name;Name+proj.Id;
myGroup.CollaborationType='Public';
chatterchattergroup.add(myGroup);
}projecthavingchatter.add(proj.Id);
}
}
System.debug('chatter'+chatter'projecthavingchatter'+projecthavingchatter);
System.debug('chatter'+chattergroup);
insert chatter;chattergroup;
}
public static void isProjectUpdate(List<Project__c> listProject,Map<Id,Project__c>oldMapProject){
System.debug('projecthavingchatter'+projecthavingchatter);
List<CollaborationGroup> chatter=newchattergroup=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)){
proj.addError('Already created');
CollaborationGroup myGroup = new CollaborationGroup();
myGroup.Name=proj.Name;Name+proj.Id;
myGroup.CollaborationType='Public'; //can be 'Public' or 'Private'
chatterchattergroup.add(myGroup);
}
}
system.debug('chatter update'+chattergroup);
try{
insert chattergroup;
}
system.debugcatch('chatterException update'+chattere);{
insert chatter;
}
}
}
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