Unable to update custom field value in a task using the node library

I had this endpoint which was properly creating a new task from a template and returning the task name and URL. It was working great. But now I want to update the value for a custom field. I have assigned the custom field to the project. When I try this though, it always says “Cannot read properties of undefined (reading ‘hasOwnProperty’)”. Note that it actually does create the task still.

Here is output with the error, including debugging lines which seem to confirm the data is right.

Updating task with GID: 1208024429069956
Custom field options: { data: { custom_fields: { ‘1171036591936079’: ‘1201731779181897’ } } }
Error in API request: Cannot read properties of undefined (reading ‘hasOwnProperty’).

And here is the full code. Thanks in advance for looking.

router.post(‘/asanaCloneTaskTemplate’, isAuthenticated, async (req, res) => {
console.log("Request body: " + JSON.stringify(req.body));
const templateGid = “1207980272508937”;
const opts = {
‘body’: {
“data”: {
“name”: req.body.projectName
}
}
};

try {
    const cloneResult = await asanaTaskTemplatesApiInstance.instantiateTask(templateGid, opts);
    const newTaskGid = cloneResult.data.new_task.gid;

    const customFieldOpts = {
        "data": {
            "custom_fields": {
                "1171036591936079": req.body.customerNameGid
            }
        }
    };
    console.log("Updating task with GID:", newTaskGid);
    console.log("Custom field options:", customFieldOpts);
    
    await asanaTasksApiInstance.updateTask(newTaskGid, customFieldOpts);
    console.log('Custom field updated successfully.');

    let detailOpts = { 'opt_fields': "permalink_url" };
    const detailResult = await asanaTasksApiInstance.getTask(newTaskGid, detailOpts);
    
    res.json({
        permalink_url: detailResult.data.permalink_url,
        name: cloneResult.data.new_task.name
    });
} catch (error) {
    console.error("Error in API request:", error.response ? JSON.stringify(error.response.body) : error.message);
    res.status(500).json({ error: error.message });
}

});

I messed up the code formatting and I can’t seem to edit the post… :frowning:

I got it working. asanaTasksApiInstance.updateTask is backwards from asanaTaskTemplatesApiInstance.instantiateTask.

This works:

        const updateBody = {
            "data": {
                "custom_fields": {
                    "1171036591936079": req.body.customerNameGid
                }
            }
        };
        
        const updateResult = await asanaTasksApiInstance.updateTask(updateBody, newTaskGid);
1 Like

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