Unable to create a subtask with a pre-defined name, and a comment of a formlink

I am using the below script action code, and it seems to work partially to retrieve task name and task ID but ends with an error while creating the sub-task.

const run = async () => {
  try {
    if (!task_gid) {
      throw new Error('task_gid is undefined.');
    }

    log(`Starting simpler subtask creation approach for parent: ${task_gid}`);

    // Get parent task name
    const parentTask = await tasksApiInstance.getTask(task_gid, { opt_fields: 'name' });
    const parentTaskName = parentTask.data.name;
    log(`Parent task name: ${parentTaskName}`);

    // Try the most basic subtask creation possible
    const subtaskPayload = {
      name: `Checklist: ${parentTaskName}`,
    };
    
    log('Using simplest possible subtask creation with payload:', JSON.stringify(subtaskPayload));

    // Try the direct createSubtaskForTask method with minimal payload
    const subtask = await tasksApiInstance.createSubtaskForTask(task_gid, subtaskPayload);

    log('Subtask creation response:', JSON.stringify(subtask, null, 2));
    
    if (!subtask || !subtask.data || !subtask.data.gid) {
      throw new Error('Subtask creation failed or response is malformed.');
    }
    
    const subtaskGid = subtask.data.gid;
    log(`Subtask created successfully with GID: ${subtaskGid}`);

    // Add the comment to the subtask
    const formLink = 'https://form.asana.com/?form_id=your_form_id'; // REPLACE THIS
    
    const commentResponse = await storiesApiInstance.createStoryForTask(subtaskGid, {
      text: `Please complete the form at the following link: ${formLink}`
    });
    
    log('Comment added successfully!');
    log('Script completed!');

  } catch (error) {
    log(`Error: ${error.message}`);
    if (error.stack) log(`Stack: ${error.stack}`);
  }
};

run();

Starting simpler subtask creation approach for parent: 1210560044714222
Parent task name: test task
Using simplest possible subtask creation with payload: {"name":"Checklist: test task"}
Error: Cannot read properties of undefined (reading 'hasOwnProperty')
Stack:     at run (eval.js:22)

Finished in 367 ms.

I tried all possible ways and it always ends up with this error. Could some experts here help me figure this out please?

Cheers

Hi @adi_pro,

FYI I moved your post to the English Forum > Developers & API forum section where we discuss programming matters.

I haven’t used the Asana Node SDK but looking at the source on Github, it appears the body parameter comes first, followed by the task gid, which is the opposite of what you have:

createSubtaskForTaskWithHttpInfo(body, task_gid, opts) {
            throw new Error("Missing the required parameter 'body' when calling createSubtaskForTask");
            throw new Error("Missing the required parameter 'task_gid' when calling createSubtaskForTask");
    createSubtaskForTask(body, task_gid, opts) {
        return this.createSubtaskForTaskWithHttpInfo(body, task_gid, opts)

Thank you, tried the change that was recommended and still see similar issue.

Here is the code

const run = async () => {
  try {
    if (!task_gid) {
      throw new Error('task_gid is undefined.');
    }

    log(`Starting task update for task: ${task_gid}`);

    // Get parent task name
    const parentTask = await tasksApiInstance.getTask(task_gid, { opt_fields: 'name' });
    const parentTaskName = parentTask.data.name;
    log(`Task name: ${parentTaskName}`);

    // Define field GIDs
    const taskNameFieldGid = "1210555925716689";  // Task Name (text type)
    const taskIdFieldGid = "1210555925716691";    // Task ID (number type)
    
    log(`Using field GIDs: Task Name = ${taskNameFieldGid}, Task ID = ${taskIdFieldGid}`);

    // Create update payload for custom fields
    const updatePayload = {
      custom_fields: {
        [taskNameFieldGid]: parentTaskName,
        [taskIdFieldGid]: parseInt(task_gid, 10) || task_gid
      }
    };
    
    log('Updating custom fields with proper parameter order...');
    // CORRECT PARAMETER ORDER: body first, then task_gid
    try {
      const updatedTask = await tasksApiInstance.updateTask(updatePayload, task_gid);
      log('Custom fields updated successfully!');
    } catch (updateError) {
      log(`Error updating custom fields: ${updateError.message}`);
      if (updateError.stack) log(`Stack: ${updateError.stack}`);
    }

    // Create subtask with correct parameter order
    const subtaskPayload = {
      name: `Checklist: ${parentTaskName}`
    };
    
    log('Creating subtask with proper parameter order...');
    try {
      // CORRECT PARAMETER ORDER: body first, then task_gid
      const subtask = await tasksApiInstance.createSubtaskForTask(subtaskPayload, task_gid);
      
      if (!subtask || !subtask.data || !subtask.data.gid) {
        throw new Error('Subtask creation failed or response is malformed.');
      }
      
      const subtaskGid = subtask.data.gid;
      log(`Subtask created successfully with GID: ${subtaskGid}`);
      
      // Add the comment to the subtask
      const formLink = 'https://form.asana.com/?form_id=your_form_id'; // REPLACE THIS
      
      const commentPayload = {
        text: `Please complete the form at the following link: ${formLink}`
      };
      
      // CORRECT PARAMETER ORDER: body first, then task_gid
      const commentResponse = await storiesApiInstance.createStoryForTask(commentPayload, subtaskGid);
      log('Comment added successfully!');
    } catch (subtaskError) {
      log(`Error with subtask: ${subtaskError.message}`);
      if (subtaskError.stack) log(`Stack: ${subtaskError.stack}`);
    }

    log('Script completed!');

  } catch (error) {
    log(`Error: ${error.message}`);
    if (error.stack) log(`Stack: ${error.stack}`);
  }
};

run();

Here is the log

Starting task update for task: 1210560044714222
Task name: test task
Using field GIDs: Task Name = 1210555925716689, Task ID = 1210555925716691
Updating custom fields with proper parameter order...
Error updating custom fields: Cannot read properties of undefined (reading 'hasOwnProperty')
Stack:     at run (eval.js:31)

Creating subtask with proper parameter order...
Error with subtask: Cannot read properties of undefined (reading 'hasOwnProperty')
Stack:     at run (eval.js:46)

Script completed!
Finished in 376 ms.

Not sure if it is a limitation of script actions or something else which I am not aware of.

Ah, sorry I missed this before: you need to wrap your payload in a data: { } structure. See this post for the correct syntax: Unable to update custom field value in a task using the node library - #3 by Scott_Beeson

1 Like

Hey, tried with the exact reference post just a little while ago and it actually worked.

Thank you so much. Looks like order plays a role to make it work correctly.

1 Like

Excellent, good to hear!

Thank you, also related to the same process that I am trying to streamline.

Any idea if I can allow the user to pick a task from a list of existing tasks through Asana form?

Because I am trying to allow the user to select a task and update it but through a form.

No, that’s not a capability currently available on Asana forms.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.