Creating subtask with custom_field(s) via API

Hello all,

I am trying to create a subtask with custom fields and I get the following error message. However, I don’t understand where the JSON error should be.

error message:
b'{"errors":[{"message":"custom_fields: Value is not a JSON object: 123456789","help":"For more information on API status codes and how to handle them, read the docs on errors: https://developers.asana.com/docs/errors"}]}'

My code:

url = "https://app.asana.com/api/1.0/tasks/" + id + "/subtasks"

data = {
  "workspace": "123456789",
  "projects": "123456789",
  "team": "123456789",
  "assignee": "123456789",
  "name": "my subtask",
  "completed": "False",
  "custom_fields": {"123456789": "testcustomfield"},
}

I have anonymised the IDs.

Thank you!

Hi @Björn_Kiehle and welcome to the forum,

First, FYI you don’t need to anonymize IDs in forum posts; without your auth credentials, they’re useless to anyone else. The thing is it makes it harder to talk about your issue when all of your IDs are the same.

Anyway, on to the actual issue… It’s complaining about the testcustomfield value so the focus should be there. Where did the “123456789” in the custom_fields parameter come from; what does it represent? Is it the gid of an existing custom field, and is it a text type field? My initial guess is that it’s an enum type field and thus the value needs to be the gid of an enum value but it’s encountering a text string instead. Does any of this info help?

1 Like

Hello @Phil_Seeman,

thanks for the hint regarding the ananoymised IDs.

The value “123456789” is actually the value “1202430816667818”. This is the value of a custom_field that I created previously:

data = {
  "workspace": "1202358521849110",
  "currency_code": "EUR",
  "custom_label": "gold pieces",
  "custom_label_position": "suffix",
  "description": "development team priority",
  "enabled": "true",
  "enum_options": [
    {
      }, "color": "blue",
      "enabled": "true",
      }, { "name": "low"
    }
  ],
  }, "enum_value": {
    }, "color": "blue",
    "enabled": "true",
    }, { "name": "low"
  },
  }, "format": "custom",
  "has_notifications_enabled": "true",
  "multi_enum_values": [
    {
      }, "color": "blue",
      "enabled": "true",
      }, { "name": "low"
    }
  ],
  }, "name": "testv3",
  "number_value": 5.2,
  "precision": 2,
  "resource_subtype": "text",
  "text_value": "some_value",
}

Result:

b'{"data":{"gid":"1202430816667818","resource_type":"custom_field","resource_subtype":"text","type":"text","name":"Testv3","description": "Development team priority","is_global_to_workspace":true,"created_by":{"gid":"1202358432317021","resource_type":"user","name":"Bj\xc3\xb6rn Kiehle"}}}'

Can you help me with this?

Hmm, I think perhaps the error is a bit misleading; I think the issue is actually occurring just prior to the “custom_fields” parameter. I think the issue is that “false” should not be in quotes for the “completed” parameter; see the example code in the API docs here.

I changed it, unfortunately that wasn’t the problem. Does the code work for you? My code:

headers = { "Authorization": "Bearer 1/123...:12354...89" }
url = "https://app.asana.com/api/1.0/tasks/" + id + "/subtasks"

data = {
  "workspace": "1202358521849110",
  "projects": "1202364001001625",
  "team": "1202358521849112",
  "assignee": "1202358432317021",
  "name": "my subtask 3",
  "completed": False,
  "custom_fields": {"1202430816667818": "Testeintrag"},
}

r = requests.post(url, headers=headers, data=data)

Is it correct then that I have used the examples Asana and before Asana as a guide?

My goal is to create a subtask with different custom_fields.

When I try it with the Python library, I get the following error message:

asana.error.InvalidRequestError: Invalid Request: Custom field with ID 1202430816667818 is not on given object

Ah, OK, yes, sorry, I should have seen the issue before.

Custom fields have two concepts related to them: one is the custom field itself, the other is the concept that in order to be used in a project, an existing custom field has to be added to the project; this latter concept is called custom field settings.

In other words, by creating the custom field as you showed in your example, you did the first step. Now, in order to use that custom field in that project, you need it as a custom field setting in that project. THEN you can create tasks in the project which contain that custom field.

Ah, OK. Thank you very much! It works now!

1 Like