Create a task with an existing custom Field

Hello asana community,

Currently I am trying to create a task with the api client, it works perfectly until I add the “custom_fields” to the data for the new task.

{‘custom_fields’: ‘{“781306366706886”: “781306366706887”}’,

‘name’: ‘Testing again and again’,

‘projects’: [781714307339766],

‘assignee’: ‘700596286189000’}

“781306366706886” is the custom field Id and the “781306366706887” enum Id.

I have been going around for days, search and tried multiple solutions to similar problems in this community and stack overflow, but none of them have worked.

I appreciate any inputs.

1 Like

Hmmm, your syntax looks right to me. Are you 100.0% sure those id values are correct, especially the enum id? That’s the id of the specific enum value you’re wanting to set, right?

1 Like

I just ran one of my unit tests that sets custom fields for a task, and grabbed the JSON I send to the Asana service:

{
  "name": "Testing",
  "assignee": "159356326117471",
  "custom_fields": {
    "743926788382115": "3.14",
    "744020393575926": "This is some text.",
    "743926788382110": "754688651720395"
  }
}

743926788382115 is the custom field ID for a number type custom field.
744020393575926 is the custom field ID for a text type custom field.
743926788382110 is the custom field ID for an enum type custom field, and 754688651720395 is the ID for one of the enum values that’s been defined for that field.

3 Likes

Thank you rob.
With the Data above, I get the following response:
Server Error: Oops! An unexpected error occurred while processing this request. The input may have contained something the server did not know how to handle. For more help, please contact api-support@asana.com and include the error phrase from this response.

And the custom_fields object is this:
{
“data”: {
“id”: 781306366706886,
“enum_options”: [
{
“id”: 781306366706887,
“color”: “aqua”,
“enabled”: true,
“name”: “Open”
},
{
“id”: 781306366706888,
“color”: “yellow”,
“enabled”: true,
“name”: “In progress”
},
{
“id”: 781306366706889,
“color”: “yellow-green”,
“enabled”: true,
“name”: “Closed”
}
],
“name”: “Status”,
“type”: “enum”
}
}

I’m afraid I’m stumped.

@Joe_Trollo, @Matt_Bramlage, any ideas? Does her error message text tell you anything about why she can’t add a custom field to a task? Her syntax looks correct to me.

@carolina, based on your original message,

It looks like you’re passing in a string to this field, when it should be an object. That is, you’re passing in

'custom_fields': '{"781306366706886": "781306366706887"}'

when you should be passing in

'custom_fields': {'781306366706886': '781306366706887'}

Can you confirm the format of the data you’re sending to the API?

Hi Joe,

I corrected the fields and now this is the new data:

{‘name’: ‘I am a new task’,
‘assignee’: ‘700596286189000’,
‘custom_fields’: {‘781306366706886’: ‘781306366706887’},
‘projects’: [781671016689811]
}

But the response now is:
'Invalid Request: Custom field with ID 781306366706886 is not on given object ’

I appreciate any help!

Based on that message, I suspect that the custom field 781306366706886 is not on the project 781671016689811. In Asana, you can only set a custom field on a task if that task is in a project that has that custom field (or is about to be created in a project that has that custom field).

1 Like

I can verify that there are no custom fields set on that project.

If you’re looking to do this in the API, you can POST a request to /projects/:project_id/addCustomFieldSetting, which will associate the custom field with the project. Then it should be possible to set the value of the custom field on the task as above.

Can you give me an example of the full request and the full body of an existing custom field?

I am trying also to create a task with an existing custom_fields

I never worked with custom fields, maybe @Diakoptis can help?

We’ve moved this discussion over to this other thread:

So If I created a new project, I cannot use the custom_fields already in the workspace?

You can use those custom fields. But you have to assign them to the new project before trying to attach them to tasks in that project. You can do this via the API using the method Matt_Bramlage showed above; or you can do it via the UI using the Manage Custom Fields option on the project menu.

I was able to do it using the client add_custom_field_setting function.
Thank you all for the dedicated responses :blush:!

1 Like

Hi Carolina,

Would you be able to share your code, I’m having the same problem you did.

Thanks,
Conor

I have the same problem. I already checked that the custom field really belongs to the project. Here is the function in Google Apps Script that I use.

 function createTask (taskName, description,wsId, projectId, assigneeId, fieldId, fieldoptionId, due, token) {  

var bearerToken = "Bearer " + token;
var payload = {
“custom_fields”: { fieldId : fieldoptionId },
“assignee” : assigneeId,
“notes” : description,
“workspace”: wsId,
“name” : taskName,
“projects” : projectId,
“due_on”: due,
“collaborators”: assigneeId
};
var options = {
“method” : “POST”,
“headers” : {“Authorization”: bearerToken},
“payload” : payload,
“muteHttpExceptions”: true
};
try {
var url = “https://app.asana.com/api/1.0/tasks/”;
var result = UrlFetchApp.fetch(url, options);
var reqReturn = result.getContentText();
var createdTaskId = JSON.stringify(JSON.parse(reqReturn).data.id);
}
catch (e) {
Logger.log(e);
}
return createdTaskId;
}

And then I have a test function which feeds that function with example IDs that really exist. However, the console throws the error:

TypeError: Cannot read property ‘id’ of undefined

The function works fine when I delete the line with “custom_fields”. So the error must appear exactly here. I don’t know if it is relevant, but my editor does not color “fieldId” (which he usually only does with unknown variables) in that line.

I have been troubleshooting for hours. Actually, my initial goal was to use “insert_after” and/or put the task in a given section (I have read the documentation and posts about this here in the forum), but this also produced errors, so I thought the easiest workaround is a custom field.

Hello everyone,
I’m sorry to re-open the case here, but I have some struggles with posting a task with a custom field.

When I get on this url: https://app.asana.com/api/1.0/custom_fields/1181175364270501/
I get this Json:
{
“data”: {
“gid”: “1181175364270501”,
“name”: “Hash”,
“resource_subtype”: “text”,
“resource_type”: “custom_field”,
“type”: “text”,
“is_global_to_workspace”: true
}
}

So my field named “hash” is detected and has the id “1181175364270501”.
But when I try to create a task with this body:
{
“data”: {
“custom_fields”:{
“1181175364270501”: “test”
},
“html_notes”: “Mittens really likes the stuff from Humboldt.”,
“name”: “Buy catnip”,
“project”: “1181175364270497”,
“resource_subtype”: “default_task”,
“workspace”: “17325551900806”
}
}

I get this error:
“message”: “Custom field with ID 1181175364270501 is not on given object”

My custom field is a text field and I can see the field in my project (both with a get to the api and in the interface)

Thanks in advance,

Antoine.

You’re sure that it’s the exact same custom field gid and not another field that has the same name? Because that error means Asana doesn’t find that custom field gid attached to that project…

Yes, if I try to get on this url:
https://app.asana.com/api/1.0/projects/1181175364270497/custom_field_settings
I get the list of all the custom fields in my project:

{
“data”: [
{
“gid”: “1181175364270505”,
“custom_field”: {
“gid”: “1181175364270504”,
“name”: “Nombre d’alarmes”,
“precision”: 0,
“resource_subtype”: “number”,
“resource_type”: “custom_field”,
“type”: “number”
},
“is_important”: true,
“parent”: {
“gid”: “1181175364270497”,
“name”: “Sauron Alarmes”,
“resource_type”: “project”
},
“project”: {
“gid”: “1181175364270497”,
“name”: “Sauron Alarmes”,
“resource_type”: “project”
},
“resource_type”: “custom_field_setting”
},
{
“gid”: “1181199391030898”,
“custom_field”: {
“gid”: “1181175364270501”,
“name”: “Hash”,
“resource_subtype”: “text”,
“resource_type”: “custom_field”,
“type”: “text”
},
“is_important”: true,
“parent”: {
“gid”: “1181175364270497”,
“name”: “Sauron Alarmes”,
“resource_type”: “project”
},
“project”: {
“gid”: “1181175364270497”,
“name”: “Sauron Alarmes”,
“resource_type”: “project”
},
“resource_type”: “custom_field_setting”
}
]
}

Because I’m looking for the “hash” custom_field, I see his id is 1181175364270501 and I verify by getting on the url:
https://app.asana.com/api/1.0/custom_fields/1181175364270501/

and that works:

{
“data”: {
“gid”: “1181175364270501”,
“name”: “Hash”,
“resource_subtype”: “text”,
“resource_type”: “custom_field”,
“type”: “text”,
“is_global_to_workspace”: true
}
}