0

I am working to assign a list of tasks to a user when they are added to a group. In this case there are 59 tasks that would be assigned when a user got added to a group named Test.

The task is an sobject named "Objectives__c" and I am having trouble getting this to work when adding a new user to the specified group.

The overall goal here is simply to take the detail of the original objectives, and create them again with the new user being added to the group as the owner, the day that the trigger occurs as the "start date" and the status as 'Not Started'

Also - the visibility check is a checkbox that refers to the objective as being public, that is why it is set to true. However, if this will not work I can remove it.

Any assistance is appreciated!

trigger ObjectiveTrigger on Objectives__c (after insert, after update) {

List<Group> grpToAssign = [SELECT Id FROM Group where Name = 'Onboarding' LIMIT 1];

List<Objectives__c> listTask = new List<Objectives__c>();

for(Objectives__c newObj : Trigger.new){
    
    if(newObj.Visibility__c == true){
        
        Objectives__C obj = new Objectives__c();
        
        obj.OwnerId = grpToAssign.get(0).Id;
        obj.Status__c = 'Not Started';
            }
        }
        if(listTask.size() > 0)
        {
        insert listTask;
        }

1 Answer 1

1

If you're needing to create Objectives__c records whenever Users are being added to a Group, the approach would be to create a trigger on the GroupMember object rather than the Objectives__c object. Unfortunately, Salesforce does not allow triggers on the GroupMember object at this time, so you likely would need to create a scheduled batch job that looks for GroupMember additions/changes to the Group and create the Objectives__c within said job, where applicable.

As a side note, you should avoid inserting or updating records for the same object as the trigger, as is the case here where Objectives__c records are being inserted in an after insert/update Objectives__c trigger. This can easily create an "infinite loop", although Salesforce will halt this process early on due to exceeding governor limits.

1
  • Thank you @Nathan W . Do you think it would be beneficial to assign the task based on the role compared to the group they are in? So when running the trigger I would run it on User and specify the role to insert the objectives? Feb 11, 2021 at 18:57

You must log in to answer this question.

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