How do I set the value of a Trello card's custom field using the Trello API?

I am trying to set the value of a custom field on a card using the Trello API, according to the documentation here.

I keep getting a response: 400 Bad Request "Invalid custom field item value."

I've documented my troubleshooting process here.

1 answer

1 accepted

2 votes
Oscar Triscon Community Champion Mar 23, 2018

You need to send a request

PUT 1/cards/CARD_ID/customFields/FIELD_ID/item

where CARD_ID and FIELD_ID are the concrete ids of your card and custom field, and with a payload of:

{ "value": { "number": "2" } }

Thank you for the response. I've verified that I'm using the correct card ID and Field ID in the path, as you described. 

I'm also trying this in Javascript, so my payload looks like this: 

var url = 'https://api.trello.com/1/card/' + CARD_ID + '/customField/' + FIELD_ID + '/item?key=' + TRELLO_API_KEY + '&token=' + TRELLO_API_TOKEN;

var customFieldResource = {};
customFieldResource.idCustomField = FIELD_ID; /* also tried the ID of the customFieldItem here */
customFieldResource.modelType = 'card';
customFieldResource.value = { "number": 2 };

var requestBody = {};
requestBody.method = 'PUT';
requestBody.contentType = 'application/json';
requestBody.payload = JSON.stringify(customFieldResource);

var response = UrlFetchApp.fetch(url, requestBody);

I know the structure of my resource and request body work because I successfully make many other calls to the Trello API using these object structures.

Oscar Triscon Community Champion Mar 24, 2018

You path looks correct, but I think you're passing too much stuff in the payload. Try changing your payload to be only:

var customFieldResource = {};
customFieldResource.value = { "number": 2 };

Hey Oscar,

Although contrary to the documentation saying the fields are required, I've tried omitting modelType and idCustomField from the payload, but it produced the same error. 

Then I tried omitting the 

requestBody.payload = JSON.stringify(customFieldResource);

and instead using:

requestBody.value = JSON.stringify({ "number": 2 });

and that produced a new error for me: 

 Invalid value for custom field type

But then again, it produced that same error if I left out payload and ContentType altogether.

Oscar Triscon Community Champion Mar 25, 2018

This begs the question: is the custom field you're trying to set of type "number", or is it another type?

Oscar Triscon Community Champion Mar 25, 2018

Actually, I just realized something. Try setting the value (albeit a number) as a string. Like this:

requestBody.value = JSON.stringify({ "number": "2" });

Yes, the field is of type number. To confirm, when I make a GET request for the custom field by it's id, it returns this custom field object:

{
"id":"5ab13cdb8acaabe576xxxxx",
"idModel":"54ccee71855b401132xxxxx",
"modelType":"board",
"fieldGroup":"837a643fd25acc835affc227xxxxxxxxxxxxxxxxxxxx",
"name":"Test Number Field",
"pos":16384,
"type":"number"
}

I tried 

requestBody.value = JSON.stringify({ "number": "2" });


but got the same error:

Invalid value for custom field type

I also tried adding the quotes when it was part of the payload object too:

customFieldResource.value = { "number": "2" }; 


But that produced the original error: 

Invalid custom field item value

Also, I tried changing the field's type from Number to Text in the Edit Power-up Settings UI, and then changing "number" to "text" and changing the 2 to a "test" string, but it produced the same error.

Oscar Triscon Community Champion Mar 25, 2018

Try this:

var response = UrlFetchApp.fetch(url, {
method: 'PUT',
contentType: 'application/json',
payload: JSON.stringify({ value: { number: "2" } })
});

I'd like to chime in with a 'me too'. I'm getting this problem whatever I try, same as Mike, including on the API docs 'try it tool'.

Also, as there are 2 'idCustomField' params with different descriptions - I'm really not sure where those values could come from so I think that could do with being made more clear.

I'll report back if I have any success.

Thanks

Ok, so playing around with the JS of the 'Try it' tool, I was able to get the parameters right for CLEARING a custom field:

xhr.open("PUT", "https://api.trello.com/1/card/' + CARD_ID + '/customField/' + FIELD_ID + '/item?idCustomField=' + FIELD_ID + '&modelType=card&value&key=' + TRELLO_API_KEY + '&token=' + TRELLO_API_TOKEN)

 note the null 'value' parameter.

THIS WORKS. The value is cleared from the custom field, so I'm confident the ID's etc. are all fine.

What DOESN'T WORK, is trying to update or set that value. I've tried every variation of encoding etc. the value as mentioned in the docs I can think of. E.g.

"value": {"number": 3} // encoded: %7B%22number%22%3A999%7D

fails with a 400 "Invalid custom field item value."

I've spent several hours on this now and I'm pretty stuck, so any help would be great!

Oscar Triscon Community Champion Mar 26, 2018

Hi Andy,

No encoding is necessary, just pass the value in the payload instead of the query string:

var xhr = new XMLHttpRequest();
xhr.open('PUT', 'https://api.trello.com/1/card/' + CARD_ID + '/customField/' + FIELD_ID + '/item?key=' + TRELLO_API_KEY + '&token=' + TRELLO_API_TOKEN, true);
xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
xhr.send(JSON.stringify({"value": {"number": "3"}}));
xhr.onload = function () { ... }

Wow, ok, so that does work. Thanks Oscar.

I got sent on the wrong path by the Try It tool - it encodes the value and puts itin as a query string parameter.

Thanks again.

Thanks Oscar!

Also, looks like Trello just updated their documentation page to remove the extraneous query params idCustomField and modelType, and they also added a note to always send strings as values (like for boolean and number values).

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jun 20, 2018 in Trello

Get inspired with Trello & earn free Gold

A blank Trello board can be both a beautiful and intimidating thing. How many lists should I have? Is creating 10 labels overkill? How can I best track my progress? Is there a way...

16,770 views 0 14
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you