Update custom field drop down in a task through API

First time using this API today.

I have an existing custom field dropdown in my Tasks table, in ASANA. When my program runs, I want to be able to update the value of that dropdown to a new value. Any advice on how to approach this?

Here is my custom field object: "custom_fields":[ { "gid":"xxx", "enabled":true, "enum_options":[ { "gid":"xxx", "color":"yellow", "enabled":true, "name":"READY", "resource_type":"enum_option" }, { "gid":"xxx", "color":"red", "enabled":true, "name":"ON HOLD", "resource_type":"enum_option" }, { "gid":"xxx", "color":"orange", "enabled":true, "name":"DATA ISSUE", "resource_type":"enum_option" }, { "gid":"229014069447456", "color":"yellow-green", "enabled":true, "name":"WAITING ON FC", "resource_type":"enum_option" }, { "gid":"xxx", "color":"hot-pink", "enabled":true, "name":"WAITING ON CLIENT", "resource_type":"enum_option" }, { "gid":"xxx", "color":"purple", "enabled":true, "name":"In Progress", "resource_type":"enum_option" }, { "gid":"xxx", "color":"red", "enabled":true, "name":"Forfeited", "resource_type":"enum_option" }, { "gid":"1199952868879785", "color":"yellow-orange", "enabled":true, "name":"SOW Clarification", "resource_type":"enum_option" }, { "gid":"xxx", "color":"green", "enabled":true, "name":"Waiting on Final Proof", "resource_type":"enum_option" } ], "enum_value":{ "gid":"xxx", "color":"yellow", "enabled":true, "name":"READY", "resource_type":"enum_option" }, "name":"Status", "created_by":{ "gid":"xxx", "name":"xxx", "resource_type":"user" }, "display_value":"READY", "resource_subtype":"enum", "resource_type":"custom_field", "type":"enum" } ]

In my program, I parse out the option I want to set it to from the enum_options area and with that data, update enum_value object to those values. I also update display_value to the appropriate name from my parsed out enum_options.

While the object looks correct, once I PUT it through the ASANA API, I get a success status but no change.

I have been following this “Update a task” page to build this process out. I hope someone can help me! Update a task

Thanks in advanced

ps. I changed all gid to xxx

I’ve also reviewed this previous issue but it did not work for me. My guess is because I’m trying to update a dropdown while this person has a text/number field.

Hi @anon45230431 and welcome to the forum!

Don’t try to set display_value, Asana will set that for you automatically. See if it works when you remove that.

If not…

You say one thing that gives me pause:

update enum_value object to those values.

It’s the “those values” I’m concerned about - what do you mean by that? You should only be setting enum_value to one value.

If none of the above helps, then please post the exact code you’re using to do the update, so we can see if we spot any issues. Also, you don’t have to worry about excluding the gids; without someone having your OAuth token or Personal Access Token (which are things you should definitely never, ever post!), a gid in your account isn’t going to do anybody else any good.

1 Like

Thanks for the quick reply!

What I was trying to explain was, I take one of the dropdown options from enum_options and copy the values (ie: gid, color``, enabled, name, and resource_type) to 'enum_value, overwriting the existing values in there.

It looks like enum_value represents the selected option in the dropdown so I assumed updating that and pushing that updated object to the update task endpoint would work, but no luck.

Here is the code snippet. This function was added to an existing C# console app. I use RestSharp to make REST API calls.

Apologies, it’s a bit sloppy, as I just dumped all of this into one big function

// build api request to get task by task ID
var apiRequest = new RestRequest($"/tasks/{taskID}");

// make request and return
var apiResponse = client.Get(apiRequest);

// if status code is okay - update task
if (apiResponse.StatusCode == System.Net.HttpStatusCode.OK)
{
	// parse response object to object
	var api_data = Json.Decode(apiResponse.Content, typeof(object));

	// map custom_fields
	var custom_fields = api_data.data.custom_fields;

	// find enum options for Status object. Status Object gid = 229014069447452
	var status_object = api_data.data.custom_fields;
					
	// index of status object so we can update it later
	int status_object_index = 0;

	if (api_data.data.custom_fields?.Length > 0)
	{
		for (int i = 0; i < custom_fields.Length; i++)
		{
			if (custom_fields[i].gid == "229014069447452")
			{
				status_object = custom_fields[i];
				status_object_index = i;
				break;
			}
		}
	}

	// in progress option values
	AsanaEnumValue asanaEnumValue = new AsanaEnumValue();

	// loop through options to find "in progress" status
	var enum_options = status_object.enum_options;
	foreach (var option in enum_options)
	{
		if (option?.name?.ToLower() == "in progress")
		{
			asanaEnumValue.GID = option.gid;
			asanaEnumValue.Color = option.color;
			asanaEnumValue.Enabled = option.enabled;
			asanaEnumValue.Name = option.name;
			asanaEnumValue.ResourceType = option.resource_type;
			break;
		}
	}

	// bool to check if data was updated properly
	bool saveTask = false;

	// if option is found then update enum_value
	if (!string.IsNullOrEmpty(asanaEnumValue.GID))
	{
		//see if enum_value exists
		if (status_object.enum_value == null)
		{
			status_object.enum_value = new
			{
				gid = asanaEnumValue.GID,
				color = asanaEnumValue.Color,
				enabled = asanaEnumValue.Enabled,
				name = asanaEnumValue.Name,
				resource_type = asanaEnumValue.ResourceType
			};
		} 
		else
		{
		// update existing object
			status_object.enum_value.gid = asanaEnumValue.GID;
			status_object.enum_value.color = asanaEnumValue.Color;
			status_object.enum_value.enabled = asanaEnumValue.Enabled;
			status_object.enum_value.name = asanaEnumValue.Name;
			status_object.enum_value.resource_type = asanaEnumValue.ResourceType;
		}
		status_object.display_value = asanaEnumValue.Name;

		// code was hide - we are good to save
		saveTask = true;
	}

	if (saveTask)
	{
		// set new value
		api_data.data.custom_fields[status_object_index] = status_object;

		// post data back to asana
		var apiPostRequest = new RestRequest($"/tasks/{taskID}", Method.PUT, DataFormat.Json);
		//var test = Json.Encode(status_object);
		//apiPostRequest.AddJsonBody((object)data);

		var status_object_json = Json.Encode(status_object);
		var update_request_json = "{\"custom_fields\":{\"229014069447452\":" + status_object_json + "}}";

		apiPostRequest.AddJsonBody(new
		{
			data = update_request_json
		});
		//apiPostRequest.Body = test;

		//apiPostRequest.AddParameter("application/json", Json.Encode(api_data), ParameterType.RequestBody);

		// make request and return
		var apiPostResponse = client.Execute(apiPostRequest);
						
		response.IsValid = true;
		response.ResponseMessage = "Complete";
	}
}

Ah, OK, that won’t work. The only thing about a custom field on a task that you can update is its gid and its value (in this case of an enum custom field, the value will be the gid of its enum_value).

While other properties of the custom field are returned in a GET response, you can’t send those back in the update as you’re trying to do.

So for example:

Say I have an enum custom field called “Priority” whose gid is 123456. This custom field has two enum values defined for it:

High, whose gid is 567890
Low, whose gid is 246000

Now I can do a task update:

{
  "data": {
    "custom_fields": {
      "123456": "567890"
    },
    "name": "Buy catnip",
  }
}

Here I am updating the task “Buy catnip”. In this call, I am telling Asana that this task has the “Priority” custom field attached to it (gid 123456), and that the enum value for that custom field on this task is the “High” value (gid 567890).

That’s the only thing about a custom field that I can set in the “Update a task” endpoint. If I want to change any properties of the custom field, I have to use the “Update a custom field” endpoint (Update a custom field), and if I want to change anything about one of this custom field’s enum options, I have to use the “Update an enum option” endpoint (Update an enum option).

Hopefully the above is clear; let me know if not.

4 Likes

Thank you!

Your explanation was very helpful and helped me solve my issue.

I rewrote some code as so:

// post data back to asana
var apiPostRequest = new RestRequest($"/tasks/{taskID}", Method.PUT, DataFormat.Json);

var status_update_json = JsonConvert.SerializeObject(new
{
	data = new {
		custom_fields = new
		{
			statusGID = "847883598614827"
		}
	}
});

status_update_json = status_update_json.Replace("statusGID", "229014069447452");

apiPostRequest.AddJsonBody(status_update_json);

// make request and return
var apiPostResponse = client.Execute(apiPostRequest);
1 Like

This topic was automatically closed after 6 days. New replies are no longer allowed.