You need to sign in to do that
Don't have an account?
John Murphy 38
Apex trigger to calculate due dates for generated task list
I need to generate a project task list with due dates for each task The due dates are calcualted b taking the total # of days for the project and dividing by the number of tasks, so for a 90 day project with 3 tasks, each task would be 30 days apart, with teh first occuring 30 days after the project start date. The follwing code gives me the tasks, but each task has the same due date -Start_date +30 days. So I have something wrong in the For loop, but cna't figure out what it is. Can someone help?
trigger tasks on Project__c (after insert)
{List<Task> newTasks = new List<Task>();
for (Project__c proj : Trigger.New) {
Date DueDate = proj.Start_Date__c+30;
Date nextDueDate = DueDate;
for (Integer i = 0; i < proj.Number_of_Tasks__c; i++)
{
Integer taskDur = integer.valueOf(proj.Task_Duration__c*(i+1));
nextDueDate = DueDate.addDays(taskDur);
newTasks.add(new Task(
Subject ='Project Task - '+ (i+1),
Status = 'In progress',
Priority = 'High',
ActivityDate = nextDueDate,
WhatId = proj.id
));
}
//proj.Number_Of_Tasks__c = 0;
insert newTasks;
}
}
trigger tasks on Project__c (after insert)
{List<Task> newTasks = new List<Task>();
for (Project__c proj : Trigger.New) {
Date DueDate = proj.Start_Date__c+30;
Date nextDueDate = DueDate;
for (Integer i = 0; i < proj.Number_of_Tasks__c; i++)
{
Integer taskDur = integer.valueOf(proj.Task_Duration__c*(i+1));
nextDueDate = DueDate.addDays(taskDur);
newTasks.add(new Task(
Subject ='Project Task - '+ (i+1),
Status = 'In progress',
Priority = 'High',
ActivityDate = nextDueDate,
WhatId = proj.id
));
}
//proj.Number_Of_Tasks__c = 0;
insert newTasks;
}
}
Please use the below code i have made some more changes:
Integer TaskDuration = proj.Start_Date__c.daysBetween(proj.End_Date__c); // Check the Field API Names as you have here:
trigger tasks on Project__c (after insert)
{
List<Task> newTasks = new List<Task>();
for (Project__c proj : Trigger.New) {
Date StartDate = proj.Start_Date__c;
Integer TaskDuration = proj.Start_Date__c.daysBetween(proj.End_Date__c);
for (Integer i = 0; i < proj.Number_of_Tasks__c; i++)
{
//Integer taskDur = integer.valueOf(proj.Task_Duration__c*(i+1)); // Populate the value for Task_Duration__c field in project i think in your record it have value as 0 it should be 30 or some what . if you are using this line then remove the Integer TaskDuration.
Integer taskDur = integer.valueOf(TaskDuration*(i+1));
Date nextDueDate = StartDate.addDays(taskDur);
newTasks.add(new Task(
Subject ='Project Task - '+ (i+1),
Status = 'In progress',
Priority = 'High',
ActivityDate = nextDueDate,
WhatId = proj.id
));
}
}
if(newTasks.size() > 0 )
insert newTasks;
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj
All Answers
Please use the below code but please read the comments in the code and change as per the comments:
The reason you are having the same due date value is the proj.Task_Duration__c field return the value 0.check that field.
trigger tasks on Project__c (after insert)
{
List<Task> newTasks = new List<Task>();
for (Project__c proj : Trigger.New) {
//Date DueDate = proj.Start_Date__c+30; // if you adding the 30 days and below also use Task_Duration__c(value as 30) field then here it will give the start date as 60 days after project start date
Date DueDate = proj.Start_Date__c;
Integer TaskDuration = 30; // use like this Integer TaskDuration = proj.Task_Duration__c;
Date nextDueDate = DueDate;
for (Integer i = 0; i < proj.Number_of_Tasks__c; i++)
{
//Integer taskDur = integer.valueOf(proj.Task_Duration__c*(i+1)); // Populate the value for Task_Duration__c field in project i think in your record it have value as 0 it should be 30 or some what . if you are using this line then remove the Integer TaskDuration.
Integer taskDur = integer.valueOf(TaskDuration*(i+1));
nextDueDate = DueDate.addDays(taskDur);
newTasks.add(new Task(
Subject ='Project Task - '+ (i+1),
Status = 'In progress',
Priority = 'High',
ActivityDate = nextDueDate,
WhatId = proj.id
));
}
}
if(newTasks.size() > 0 )
insert newTasks;
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj
Use following code to acheive desired result:
Apex Class:
(please change object/field API names to suit your requirements)
Trigger:
If this helps solve your problem, please mark this as BEST ANSWER.
Error: Compile Error: Method does not exist or incorrect signature: void createTasksOnInsert(List<Project__c>) from the type ProjectTriggerHandler at line 6 column 37
Not sure what this means, exactly, that line of code looks good to me. Any ideas?
Once done, then create trigger. It will get saved.
I think the Task duration field you have as the formula field and do you have the start date and end date field then one small change only. If it is yes please use the below change:
Integer TaskDuration = proj.Start_Date__c.daysBetween(proj.End_Date__c); // Check the Field API Names as you have here:
trigger tasks on Project__c (after insert)
{
List<Task> newTasks = new List<Task>();
for (Project__c proj : Trigger.New) {
//Date DueDate = proj.Start_Date__c+30; // if you adding the 30 days and below also use Task_Duration__c(value as 30) field then here it will give the start date as 60 days after project start date
Date DueDate = proj.Start_Date__c;
Integer TaskDuration = proj.Start_Date__c.daysBetween(proj.End_Date__c); // use like this Integer TaskDuration = proj.Task_Duration__c;
Date nextDueDate = DueDate;
for (Integer i = 0; i < proj.Number_of_Tasks__c; i++)
{
//Integer taskDur = integer.valueOf(proj.Task_Duration__c*(i+1)); // Populate the value for Task_Duration__c field in project i think in your record it have value as 0 it should be 30 or some what . if you are using this line then remove the Integer TaskDuration.
Integer taskDur = integer.valueOf(TaskDuration*(i+1));
nextDueDate = DueDate.addDays(taskDur);
newTasks.add(new Task(
Subject ='Project Task - '+ (i+1),
Status = 'In progress',
Priority = 'High',
ActivityDate = nextDueDate,
WhatId = proj.id
));
}
}
if(newTasks.size() > 0 )
insert newTasks;
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj
Please use the below code i have made some more changes:
Integer TaskDuration = proj.Start_Date__c.daysBetween(proj.End_Date__c); // Check the Field API Names as you have here:
trigger tasks on Project__c (after insert)
{
List<Task> newTasks = new List<Task>();
for (Project__c proj : Trigger.New) {
Date StartDate = proj.Start_Date__c;
Integer TaskDuration = proj.Start_Date__c.daysBetween(proj.End_Date__c);
for (Integer i = 0; i < proj.Number_of_Tasks__c; i++)
{
//Integer taskDur = integer.valueOf(proj.Task_Duration__c*(i+1)); // Populate the value for Task_Duration__c field in project i think in your record it have value as 0 it should be 30 or some what . if you are using this line then remove the Integer TaskDuration.
Integer taskDur = integer.valueOf(TaskDuration*(i+1));
Date nextDueDate = StartDate.addDays(taskDur);
newTasks.add(new Task(
Subject ='Project Task - '+ (i+1),
Status = 'In progress',
Priority = 'High',
ActivityDate = nextDueDate,
WhatId = proj.id
));
}
}
if(newTasks.size() > 0 )
insert newTasks;
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj
trigger tasks on Project__c (after insert)
{
List<Task> newTasks = new List<Task>();
for (Project__c proj : Trigger.New) {
Date StartDate = proj.Start_Date__c;
Decimal TaskDuration = (proj.Start_Date__c.daysBetween(proj.End_Date__c)/(proj.Number_of_Tasks__c));
for (Integer i = 0; i < proj.Number_of_Tasks__c; i++)
{
Integer taskDur = integer.valueOf(TaskDuration*(i+1));
Date nextDueDate = StartDate.addDays(taskDur);
newTasks.add(new Task(
Subject ='Project Task - '+ (i+1),
Status = 'In progress',
Priority = 'High',
ActivityDate = nextDueDate,
WhatId = proj.id
));
}
}
if(newTasks.size() > 0 )
insert newTasks;
}
It should be possible for a user to enter the number of tasks (in the Number of Tasks field). When the project is saved standard Salesforce tasks should be automatically generated (i.e. if the user entered 3 then 3 tasks are created automatically). This can be done by means of an Apex trigger that should work something like as follows: