Adding parent task to an existing task

As part of my full script, I’m parsing through an Excel export of JIRA data and building the tasks in OmniPlan using the appleScript API. I’ve figured out most of the task configurations, but I’m struggling to find an example of setting the parent of an existing task to a group. Since my script is pretty long, I’m only including the section that relates to either finding an existing task (called sprintTask) or creating a new sprintTask. I then verify that it’s of type “group task” and try to set the parent task of the existingTask to sprintTask. While you won’t see existingTask declared in the code snippet, please note that it is properly set as a task. Any help would be appreciated. The only examples I can find online of adding child tasks under a group show making new tasks directly under the group. That won’t work for what I need. I will need to add existing tasks. The last line of the script snippet is the line that fails

         --Get any existing tasks with the JIRA_Sprint set to taskValue
                    --Note:  This indicates that the JIRA_Sprint exists and can be a parent for the existingTask
                    --Note:  If there is not a task with the JIRA_Sprint, it will be set to missing value
                    local sprintTask
                    set sprintTask to lookup value taskValue in "JIRA_Sprint"
                    
                    --If a parent exists and has the custom key JIRA_Sprint set to taskValue, do nothing
                    if sprintTask is not missing value then
                        --Debug
                        my logTasks(sprintTask)
                        log "FOUND SPRINT:  " & taskValue
                        
                    else
                        log "SPRINT NOT FOUND"
                        
                        --Make a new task to add properities
                        --Note:  Set the taskValue as the name here just to get it populated.
                        --For later:  set newTask to make new task at front document with properties {name:taskValue}
                        set sprintTask to make new task with properties {name:taskValue}
                        
                        --Local values
                        local oldData2
                        local newData2
                        
                        --Get previous custom data
                        copy custom data of sprintTask to oldData2
                        
                        --Add the JIRA_Sprint custom field on the task to the end of the custom data field
                        --Note:  Setting custom data of sprintTask will clear all other custom data fields
                        set newData2 to {JIRA_Sprint:taskValue} & oldData2
                        
                        --Update the custom data fields of the newTask
                        --Note:  If JIRA_Sprint does not exist, it will be created.  If it does exist, it will be updated.
                        --Note:  JIRA_Sprint is a compile time variable.  Applescript does not allow dynamic record generation using variables for key.
                        set custom data of sprintTask to newData2
                    end if
                    
                    --Get the task type of the sprintTask
                    local sprintType
                    set sprintType to task type of sprintTask
                    
                    --Debug
                    log "Sprint task type:  " & sprintType
                    
                    --If the sprintTask is not a group, set it to be a group
                    if sprintType is not "group task" then
                        --Debug
                        log "Task is not a group:  " & sprintType
                        
                        set task type of sprintTask to group task
                    end if
                    
                    --Add the existingTask as a child of sprintTask
                    set parent task of existingTask to sprintTask

Snippet of final event log section and resulting error:

tell application “OmniPlan”
(FOUND SPRINT: Sprint 4)
get task type of task id 45 of scenario 1 of project of document id “pX_TARDIgnX”
(Sprint task type: group task)
(Task is not a group: group task)
set task type of task id 45 of scenario 1 of project of document id “pX_TARDIgnX” to group task
set parent task of task id 34 of scenario 1 of project of document id “pX_TARDIgnX” to task id 45 of scenario 1 of project of document id “pX_TARDIgnX”
Result:
error “OmniPlan got an error: Can’t set parent task of child task to task id 45 of scenario 1 of project of document id “pX_TARDIgnX”.” number -10006 from parent task of child task

After some more tinkering, I figured it out. The code I wrote works once I change:

--Add the existingTask as a child of sprintTask
set parent task of existingTask to sprintTask

To the following:

--Add the existingTask as a child of sprintTask
move existingTask to end of tasks of sprintTask

Leaving this here as a reference for anyone else who may need it.

2 Likes