0

I have a trigger on one of the custom objects which helps me to create a task based on one of the fields( active = true) in the custom object. I am able to achieve this requirement, but I have one more field(pick list) which holds to a list of public group names and whenever I create/edit my custom object with the above criteria, it should create the tasks to all users who are in the public group.

The code which I have written is,

trigger triggerTaskImplemetationon on Staff__c (before update) {

    List<Task> taskList = new List<Task>();
    List<Staff__c > jobList = trigger.New;

    List<GroupMember> GMlist = new List<GroupMember>();

    for(Staff__c jobObj: jobList){
        if(jobObj.status == true){
            taskList.add(new Task(
                ActivityDate = Date.today(),
                WhatID = jobObj.Id,
                Status = 'Open',
                type = 'Other'
            ));
        }
    }
    insert taskList;

}

I have gone through the websites and forums and we can get the list of users based on below details

List<GroupMember> GMlist = new List<GroupMember>();

I am not sure how to implement in my code. Any suggestion would be helpful

Update:

I am able to read the users from public groups now and able to create tasks to the users in that group. Below is the updated code

trigger triggerTaskImplemetationonJob on Staff__c (before update) {

List<Task> taskList = new List<Task>();

List<Staff__c> jobList = trigger.New;
String groupNames = jobList[0].Group__c;

list<GroupMember> gm = [select group.name,group.DeveloperName,UserOrGroupId from GroupMember where group.DeveloperName =: groupNames]; 
List<Task> tasks = new List<Task>();

system.debug('developerName ::' + group.DeveloperName); 
system.debug('*****group****'+gm);

for(Staff__c jobObj : jobList){
    if(gm != null && gm.size() > 0){
        for(GroupMember g : gm){
            taskList.add(new Task(
                ActivityDate = Date.today(),
                WhatID = jobObj.Id,
                ownerID =  g.UserOrGroupId,
                Status = 'Open',
                type = 'Other',
                Priority = 'Normal'
            ));
        }
    }
}

if(taskList.size() > 0)
    insert taskList;

}

But the problem is, the Group__c is a multi picklist field where user can select multiple groups at the same time and save the record. when we select multiple groups, the group names will come as A;B and the soql returns null and it wont create tasks after that.

Please let me know how to fix it.

1 Answer 1

0

The simple fix is as follows - turn the multi-select field into a list and use a SOQL IN operator

String[] groupNames = jobList[0].Group__c != null
      ? jobList[0].Group__c.split(';')
      : new List<String>();

list<GroupMember> gm = [select group.name,group.DeveloperName,UserOrGroupId
                          from GroupMember where group.DeveloperName IN : groupNames];

That said, and an exercise left to the OP, is your trigger is not bulkified - one has to design this for an update of n Staff__c, not just a single object jobList[0]. Please consult the myriad resources on bulkifying triggers

2
  • Thank you..I thought the same split method.. but I thought I may get any other options also.
    – KSL
    Commented Oct 25, 2016 at 0:58
  • I will fix that and bulkify the code as well.. sorry for using [0]... Thanks again
    – KSL
    Commented Oct 25, 2016 at 0:58

You must log in to answer this question.

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