function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ManderMander 

Task Template Updates

A few years ago we had someone create task templates and the trigger to create tasks on a custom object we call projects.  It worked well at the time, but I need to make it more flexible.  

 

Right now, it creates a task and assigns it to the Project Manager.  I'd like for it to look at a Task category (or something like it) field to determine what type of task it is and then assign it to the user assign to that role.  For example, we need to generate a task to the CAD Tech.  I'd like the trigger to look for the category: CAD and find the CAD Tech assigned (a lookup contact field on the project) to the project and assign the task to that person.  

 

Additionally, the current Task Template trigger makes the due date of the task = Start Date of the project.  I'd like it to be more flexible like tasks are if they're created by workflows: Trigger Date + 15, etc.  

 

Does anyone know how I could do that?

 

 

/*********************************************************************************
Name : createTasksForProjects 
Created Date : 7 Jan 2010
modified Date : 25 Jan 2010
Usages : If Create task is set to true 
         1)Then we need to fetch Task Templates having the region and project type.
         2)Create task for all the templates having same values for projects.
*********************************************************************************/
trigger createTasksForProjects on pse__Proj__c (After insert) {
   
    Set<String> projectRecordType = new Set<String>();
    Set<String> projectId = new Set<String>();
    List<pse__Proj__c> projectList = new List<pse__Proj__c>();
    //Create set of Region and Project Type.
    List<pse__proj__c> newProjectList = [Select Id ,Name ,CreateTask__c,RecordTypeId,RecordType.Name,pse__Project_Manager__c,CAD_Tech__c,Programmer__c,Lead_Tech__c,CAD_Hours__c,pse__Start_Date__c from pse__proj__c where id in: Trigger.New];
    For(pse__Proj__c proj : newProjectList){
       if(proj.CreateTask__c == true){
         System.Debug('Record Type :'+proj.RecordTypeId);
         if(proj.RecordTypeId!=null){
            System.Debug('Record Type Name:'+proj.RecordType.Name);
            projectRecordType.add(proj.RecordType.Name);
         }
       }
    }
    List<Task> tasksToCreate = new List<Task>();
    //If region and project type exists.
    if(projectRecordType.size() > 0){
        
        //Fetch all task templates which holds same region and Project Type.
        For(List<Task_Template__c> taskTemplates : [Select id,Order__c,Project_Type__c,Assigned__c,Region_del__c,Subject__c,Name,Project_Record_Type__c From  Task_Template__c Where Project_Record_Type__c in : projectRecordType ]){
            For(Task_Template__c taskTemp : taskTemplates ){
                For(pse__Proj__c proj :newProjectList){
                    System.Debug('project Record Type '+proj.RecordType.Name+'\n Template project type'+taskTemp.Project_Record_Type__c);
                   //Create Task.
                   if(proj.RecordTypeId!=null && proj.RecordType.Name == taskTemp.Project_Record_Type__c){
                        if(proj.CreateTask__c == true && proj.pse__Project_Manager__c != null){
                            Task task = new Task();
                            task.Subject = taskTemp.name; 
                            task.WhoID = proj.pse__Project_Manager__c;                  
                            task.ActivityDate = proj.pse__Start_Date__c;
                            task.WhatID = proj.id;
                            task.Order__c = taskTemp.Order__c;
                            task.type = 'Other';
                            tasksToCreate.Add(task);
                            //If size more then 998 then need to insert list and clear it as it can hold only 1000 records.
                            if(tasksToCreate.size()== 999){
                                insert tasksToCreate;
                                tasksToCreate.clear();
                            }
                            projectId.add(proj.Id);                             
                        }
                    }
                }
            }            
        }
        //If size greater then 0 then insert task.
        if(tasksToCreate.size()> 0){
            insert tasksToCreate;
        }
        for(pse__Proj__c proj:Trigger.New){
            if(projectId.contains(proj.Id)){
                pse__Proj__c projObj = new pse__Proj__c(Id=proj.Id);
                projObj.alreadyCreateTaskFlag__c = true;
                projectList.add(projObj);
                if(projectList.size()== 999){
                       update projectList;
                       projectList.clear();
                }
            }
        }
        if(projectList.size()>0){
            update projectList;
        }
    }

}

Current fields on the Task Template object are:

Field Label                       API Name                             Data Type

Assigned                         Assigned__c                        Lookup(User)

Order                                Order__c                               Number(3, 0)

Project Record Type     Project_Record_Type__c   Picklist

Project Type                    Project_Type__c                  Picklist

Region                             Region_del__c                    Lookup(Region)

Subject                            Subject__c                            Text(100)

 

Thanks!

Mander

Best Answer chosen by Admin (Salesforce Developers) 
MellowRenMellowRen

Mander

 

Flitting through and realised that the second part, the date, is quite easy (if I have interpreted it correctly).

 

To set the task date to 15 days after the trigger date you need to replace this line:

 

task.ActivityDate = proj.pse__Start_Date__c;

 with:

 

task.ActivityDate = datetime.now();
task.ActivityDate = task.ActivityDate.addDays(15);

 

[You may get away with  task.ActivityDate = datetime.now().addDays(15); , can't remember if APEX allows an instance method straight after a static creation method.]

 

You mentioned you want to  make this “more flexible” but didn't really mention how (ie by what logic). If I assume you want different templates to have different start dates then you could add a number field to your Task Template object which represents the number of days after the trigger. Better yet, let’s take it one step further and say that a positive number is days after the trigger date whereas as a zero or a negative number is the number of days before the project start date. Assuming the new field is called Start Date Delta (Start_Date_Delta__c) then your trigger code would then be:

 

task.ActivityDate = (taskTemp.Start_Date_Delta__c < 1 ? proj.pse__Start_Date__c : datetime.now());
task.ActivityDate = task.ActivityDate.addDays(taskTemp.Start_Date_Delta__c);

 

Flexible enough? Remember to make this work you need to add the field to Task Template object and add it to the SELECT part of the SOQL query in your trigger code.

 

Hopefully someone else can give you pointers for the Project Manager stuff as I got to go.

 

Good luck,

MellowRen

 

 

All Answers

MellowRenMellowRen

Mander

 

Flitting through and realised that the second part, the date, is quite easy (if I have interpreted it correctly).

 

To set the task date to 15 days after the trigger date you need to replace this line:

 

task.ActivityDate = proj.pse__Start_Date__c;

 with:

 

task.ActivityDate = datetime.now();
task.ActivityDate = task.ActivityDate.addDays(15);

 

[You may get away with  task.ActivityDate = datetime.now().addDays(15); , can't remember if APEX allows an instance method straight after a static creation method.]

 

You mentioned you want to  make this “more flexible” but didn't really mention how (ie by what logic). If I assume you want different templates to have different start dates then you could add a number field to your Task Template object which represents the number of days after the trigger. Better yet, let’s take it one step further and say that a positive number is days after the trigger date whereas as a zero or a negative number is the number of days before the project start date. Assuming the new field is called Start Date Delta (Start_Date_Delta__c) then your trigger code would then be:

 

task.ActivityDate = (taskTemp.Start_Date_Delta__c < 1 ? proj.pse__Start_Date__c : datetime.now());
task.ActivityDate = task.ActivityDate.addDays(taskTemp.Start_Date_Delta__c);

 

Flexible enough? Remember to make this work you need to add the field to Task Template object and add it to the SELECT part of the SOQL query in your trigger code.

 

Hopefully someone else can give you pointers for the Project Manager stuff as I got to go.

 

Good luck,

MellowRen

 

 

This was selected as the best answer
ManderMander

MellowRen, this was really helpful and pretty close to what I was hoping for.

 

I actually wanted it to be trigger date +/- the value of the field.  So using your tip came up with this:

task.ActivityDate = (taskTemp.Days__c < 1 ? proj.pse__Start_Date__c.addDays((integer.valueof(taskTemp.Days__c))):date.today().addDays(integer.valueof(taskTemp.Days__c)));

 If the value of the Days is positive, it returns trigger date + the value of the field.  

 If the value of the Days is negative, it returns start date - the value of the field.  

 

Thanks for your help!