• John Murphy 38
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 5
    Replies
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;

}
}
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;

}
}
Hi,
Though i have coded the with the same specification as asked in the challenge but then also challenge failing.
Not sure why this is happening. The issue is actually the code or Trailhead is not working fine.

I am a beginner so facing issue in identifying it.
Need help.

Thanks in advance.