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
IoniaCorpIoniaCorp 

Create an additional Task based on the current task

I am trying to create an additional task if the status of the current task is "Left Message". The new task will be identical except its status will be "Not Started", and due date will be different one.
My sample code is as follows:

*****************
trigger CreateNewIfStatusChangedToLeftMessage on Task (after insert,after update) {

for (Task t : Trigger.new) {    
         
     if (t.Status=='Left Message')
     {                      
        Task task = new Task(
                   //WhoId = t.WhoId,
                  // Owner = t.Owner, 
                  // What = t.What,                                               
                   Description = t.Description,
                   Priority = t.Priority,
                   ReminderDateTime = System.now().addDays(2),
                   ActivityDate = System.Today(), //should be different from the current one
                   Status = 'Not Started',
                   Subject = t.Subject);
                 
                   insert task;           
                  
        System.debug('Additional Task Created') ;  
      }
      else
      {
         System.debug('Status: ' + 'Not changed.' ) ;    
      }
 
  }
}
***************************
From the debugging message  the additional task seems to be created. But I am unable to see the new task under the page where I started creating the task. I can see only one task created, not both of them.

Any suggestion will be appreciated.

Thank you.
werewolfwerewolf
Why did you comment out the who, what and owner?  Those are pretty key if you want to be able to see the task.  Also: turn on the Debug Log to have a look and see if the insert is failing.  If it is (for instance, you're neglecting to specify an OwnerId) it will tell you why.
IoniaCorpIoniaCorp
Thank you for the response.

I tried to use  Owner = t.Owner &  What = t.What but I got error Error: Compile Error: Field is not writeable: Owner at line 9 column 28 and so on.
I turned on the Debug Log and saw that the insert is not failing. Only problem is I can not see the additional task in the page. I can see only one instead of two - for the case of Status="Left Message" not for the Status = "Not Started"


werewolfwerewolf
Oh.  Owner is not a settable field.  OwnerId is.  What is not a settable field, WhatId is.
IoniaCorpIoniaCorp
Thank you very much  werewolf,
It solved my current problem - by making OwnerId and WhatId it works fine.