How to add custom field option using google scripts?

How would I do this:

var url = "https://api.trello.com/1/cards/{idCard}/customField/{idCustomField}/item?token={yourToken}&key={yourKey}";
var data = {value: { number: "42" }};
fetch(url, { body: JSON.stringify(data), method: 'PUT', headers: {'content-type': 'application/json'}})
.then((resp) => resp.json())
.then((data) => console.log(JSON.stringify(data, null, 2)))
.catch((err) => console.log(JSON.stringify(err, null, 2)))

In order to change the options in a pre-defined custom field on a card,
However I can’t use fetch on google scripts, I’m constrained to using google’s “UrlFetchApp.fetch”
class Class UrlFetchApp  |  Apps Script  |  Google Developers

I’ve attempted this:

var url = "https://api.trello.com/1/cards/" + cardId + "/customField/[CustomID]/item?token=[Token]&key=[Key]";
var data = {value: { "text": "42" }};
var payload = {"customField" : data};
var options = {"method" : "put",
               "payload" : payload};
UrlFetchApp.fetch(url,options);  

But I get this error But I get this error:
Request failed for https://api.trello.com returned code 400. Truncated server response: Invalid value for custom field type

Thank you in advance!

1 Like

I think this may have to do with the way that you’re payload is being passed in.

I don’t think that you need var payload = {"customField" : data}; Trello never looks for a customField key in the body of the request.

Can you try deleting the var payload = line and replacing your var options = ... with:

var options = {
  "method" : "put",
  "payload" : JSON.stringify(data),
  "contentType": "application/json"
};

You’ll notice in the fetch() examples here they specifically call out Convert the JavaScript object to a JSON string

1 Like

Thank you so much, it works!

1 Like