Asana Scripts - writing a value in a custom field

Hi Asana community,

I am creating a rule which is triggered by a simple condition, when due date of a task has changed. The action is a script which is writing a text into a custom field which exists in the project. But after running the script the custom field remains empty. What is wrong?
Example:
Custom field - ‘DebugLog’
Text" - “Hello from the rule!"

Here is the script:
async function run({ event, api }) {
const task = event.task;
const client = api.asana;

// Get full task details including custom fields
const taskDetails = await client.tasks.getTask(task.gid, { opt_fields: [‘custom_fields’] });

// Find the DebugLog field by name
const debugField = taskDetails.custom_fields.find(f => f.name.trim() === ‘DebugLog’);

if (debugField) {
// Write a simple message to the DebugLog field
await client.tasks.updateTask(task.gid, {
custom_fields: {
[debugField.gid]: “Hello from the rule!”
}
});
}
}

Hi @Bronislav_Kvasnicka,

(FYI I moved your post to the English Forum > Developers & API forum section since it’s API-coding-related.)

Neither of the above scripts is correct. Here’s the script to accomplish what you want:

async function run() {
  const task = await tasksApiInstance.getTask(task_gid);
  const customField = task.data.custom_fields.find((field) => field.name === "DebugLog");
  if (customField !== undefined) {
    await tasksApiInstance.updateTask(
      {
        data: {
          "custom_fields": {
            [customField.gid] : "Hello from the rule!"  
          },
        },
      },
      task_gid
    );
  }
}

// To function properly, the script must end with returning a Promise via run().
run();
    
4 Likes

Thank you Phil. This is obvious! Now the script works correctly!

1 Like