How to create a Trello card with custom fields from google scripts?

203 Views Asked by At

I need to create a card with custom fields in trello from google script. I can create the card, I can read the custom fields, but I can't set a value to a custom field.

function addCard() {
  var key = '---------------';
  var token = 'xxxxxxxxxxxx';
  var listId = 'yyyyyyyyyyy';
  var boardId = 'zzzzzzzzzzz';
  var cardName = 'Card nou 11';
  var cardDesc = 'Card description';
  var customFieldId = 'xxxxx';
  var customFieldValue = '8';

  // Obțineți ID-ul câmpului personalizat
  var urlCustomFields = 'https://api.trello.com/1/boards/' + boardId + '/customFields?key=' + key + '&token=' + token;
  var customFieldsResponse = UrlFetchApp.fetch(urlCustomFields);
  var customFields = JSON.parse(customFieldsResponse.getContentText());
  var customFieldId = '';
  for (var i = 0; i < customFields.length; i++) {
    if (customFields[i].name == 'Cantitate') {
      customFieldId = customFields[i].id;
      break;
    }
  }
  Logger.log(customFieldId);

  // Adăugați cardul cu câmpul personalizat
  var url = 'https://api.trello.com/1/cards?key=' + key + '&token=' + token + '&idList=' + listId + '&name=' + encodeURIComponent(cardName) + '&desc=' + encodeURIComponent(cardDesc) + '&idCustomField/' + customFieldId + '=' + encodeURIComponent(customFieldValue);
  Logger.log(url);
  var options = {
    'method' : 'post'
  };

  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
  
}

The card is created but without any value in the custom field.

1

There are 1 best solutions below

0
TheMaster On

It is not possible to create card with custom field values. You have to create card first in a API call, get the created card's ID, wait a minute or two, and then add custom fields in an another API call to the created card.

Reference:

https://community.developer.atlassian.com/t/how-to-add-custom-field-options-alongside-post-add-card/33010