How to set custom_fields that has an enum_value us

2019-09-18 09:41发布

问题:

I'm trying to set a custom_fields of type enum_value in a task that I'm creating with a POST HTTP request.

I managed to set a custom_field of type number but I'm having issue with the custom_fields of type enum_value

Questions:

Here's what I did so far:

1- I created the custom_fields that I want to populate on asana, I can set custom_fields of type number but not the ones of type enum_value( see picture attached)

Here's my code (I tried different implementations to set the custom_fields that were incorrect) :

  var task = {
      data: {
        assignee: "me",
        workspace: "1234567", 
        projects: "9876543",
        parent: null,
        custom_fields: {
          "1234567898": 333,  // this works
          "98765": "Public" // this custom field holds an enum_values, this implementation doesn't work
        },
        notes: "Test notes" 
      }
    }

回答1:

It looks like you put the name of the enum_value instead of the id. Here is an example of a PUT/POST request and response:

# Request
curl --request PUT -H "Authorization: Bearer <personal_access_token>" \
https://app.asana.com/api/1.0/tasks/1001 \

    -d
    '{
      "data": {
        "custom_fields":{
          "124578":"439"
        }
      }
    }'

    # Response
    {
      "data": {
        "id": 1001,
        "name": "Hello, world!",
        "completed": false,
        "...": "...",
        "custom_fields": [
          {
            "id": 124578,
            "name": "Priority",
            "type": "enum",
            "enum_value": {
              "id": 439,
              "name": "High",
              "enabled": true,
              "color": "red"
            }
          },
          "~..."
        ]
      }
    }

It's admittedly a bit buried, but if you look in the Custom Fields section of the getting started documentation, there is an example of creating custom fields under "Accessing Custom Field values on Tasks".