Previously, I had a Asana AI rule to populate week numbers for this project, but due to the AI credit issue, I tried to pivot as per How to Save your AI Studio Credits with Script Actions (and go even further) .
- So I tried to get gemini to generate a script for me to automatically populate the week number field for me instead.
-
/**
- Asana Script to automatically populate a custom field with a week number range based on due and start dates.
- This script is designed to be run from within Asana’s scripting environment.
- It uses the predefined
tasksApiInstanceandtask_gidto fetch and update task data. - Requirements:
-
- A custom field of type “Text” must be created in your Asana project to store the output string.
-
- The GID of your custom field must be set in the
customFieldGidconstant.
*/
- The GID of your custom field must be set in the
// IMPORTANT: The GID of your custom field for the week number range.
const customFieldGid = “xx”;/**
- Helper function to calculate the ISO week number from a JavaScript Date object.
- @param {Date} date The date object to calculate the week number for.
- @returns {number | null} The ISO week number, or null if the input is not a valid date.
*/
function getIsoWeekNumber(date) {
if (!date || isNaN(date.getTime())) {
return null;
}
const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
const dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
}
/**
- Main script function.
- This function is automatically called by the Asana scripting environment.
*/
async function run() {
log(“Starting script to populate week number range for task:”, task_gid);
try {
// 1. Fetch the task details, requesting specific fields to improve performance.
const task = await tasksApiInstance.getTask(
task_gid,
{
opt_fields: [
‘name’,
‘due_on’,
‘start_on’,
‘custom_fields’
]
}
);
const taskData = task.data;log("Task name:", taskData.name); // 2. Check if the task has a due date. This is the minimum requirement. if (!taskData.due_on) { log("Task has no due date. Skipping."); return; } // 3. Parse the dates and calculate the week numbers. const dueDate = new Date(taskData.due_on); const dueWeek = getIsoWeekNumber(dueDate); let startWeek; // 4. Handle the case where there is no start date. if (!taskData.start_on) { log("No start date found. Using due date's week number for the start of the range."); startWeek = dueWeek; } else { const startDate = new Date(taskData.start_on); startWeek = getIsoWeekNumber(startDate); } // 5. Construct the output string. let weekRangeString = ""; if (startWeek !== null && dueWeek !== null) { weekRangeString = `Week ${startWeek} - Week ${dueWeek}`; log(`Calculated week range string: "${weekRangeString}"`); } else { log("Could not calculate week numbers. Skipping."); return; } // 6. Prepare the payload to update the task's custom field. const updateData = { data: { custom_fields: {} } }; updateData.data.custom_fields[customFieldGid] = weekRangeString; // 7. Update the task using the Asana API client. await tasksApiInstance.updateTask(task_gid, updateData); log("Task successfully updated with the week number range.");} catch (error) {
// 8. Handle any potential API errors.
log(“An error occurred during script execution:”, error.message);
}
}// To function properly, the script must end with a call to run().
run(); -
Starting script to populate week number range for task: xx
Task name: xx
No start date found. Using due date’s week number for the start of the range.
Calculated week range string: “Week 40 - Week 40”
An error occurred during script execution: Cannot read properties of undefined (reading ‘hasOwnProperty’)
Finished in 333 ms.
This custom field was generated by Asana AI during the initial migration from google sheets in Asana. It impacted the identification of the custom field GID at first.
Not sure whether this issue is also linked.
Would be great if i can get some guidance as well.