Notes
public class GroupUtility {
public static Group getGroupByName(String groupName) {
List<Group> groupList = [
SELECT Id, Type, Name
FROM Group
WHERE Name = :groupName
LIMIT 1
];
return groupList.isEmpty() ? null : groupList[0];
}
public static Group createNewGroup(String groupName) {
Group newGroup = new Group(Name = groupName);
insert newGroup;
return newGroup;
}
public static Group createNewQueue(String queueName) {
Group newQueue = new Group(Name = queueName, Type = 'Queue');
insert newQueue;
return newQueue;
}
public static Group createNewGroupWithSObjects(String groupName, String groupType, List<String> sObjectNames) {
Group newGroup = new Group(
Name = groupName,
Type = groupType == 'Queue' ? 'Queue' : null
);
insert newGroup;
if (!sObjectNames.isEmpty()) {
List<QueueSobject> queueSobjectList = new List<QueueSobject>();
for (String sObjectName : sObjectNames) {
queueSobjectList.add(new QueueSobject(
QueueId = newGroup.Id,
SobjectType = sObjectName
));
}
insert queueSobjectList;
}
return newGroup;
}
public static Group createNewGroupIfNotExisting(String groupName) {
Group existingGroup = getGroupByName(groupName);
if (existingGroup == null) {
Group newGroup = new Group(Name = groupName);
insert newGroup;
return newGroup;
}
return existingGroup;
}
}