Google Apps Script & Custom Fields - please help!

custom-fields
api

#1

Hi all

I’m trying to create tasks and populate some custom fields while I’m at it, via Google Apps Script. I can make the task just fine!

But when it comes to updating the custom fields (below) - the API either throws an error or just ignores the update. I’m going crazy here!

function updatemetadata(){

var cardid = “437939707334305”;
var cust = {“custom_fields”: {437890709441475:“product type here”}}

var options = {
“method”: “PUT”,
“ContentType”:“application/json”,
“muteHttpExceptions” : true,
“headers”: {
“Authorization”: "Bearer " + PERSONAL_ACCESS_TOKEN
},
“payload”: JSON.stringify(cust)
}

Logger.log(options);

// using try to capture errors

try {
Logger.log(“fetching…”);
var url = “https://app.asana.com/api/1.0/tasks/” + cardid;
var result = UrlFetchApp.fetch(url, options);
var taskJSON = result.getContentText();
} catch (e) {
Logger.log(“ERROR” + e.message);
return null;
} finally {
Logger.log(taskJSON);
//return JSON.parse(taskJSON).data;
}
};

result:

[17-09-25 22:32:47:058 PDT] {headers={Authorization=Bearer 0/e0b4755f97f89a0748c07bc1f14702f6}, method=PUT, ContentType=application/json, payload={“custom_fields”:{“437890709441475”:“product type here”}}, muteHttpExceptions=true}
[17-09-25 22:32:47:059 PDT] fetching…
[17-09-25 22:32:53:493 PDT] {“data”:{“id”:437939707334305,“created_at”:“2017-09-26T04:32:26.722Z”,“modified_at”:“2017-09-26T04:48:32.546Z”,“name”:“Editorial Feature w/ IV 800 words [PI1062]”,“notes”:“yah”,“assignee”:{“id”:191437263563258,“name”:“Cam Nealie”},“parent”:null,“completed”:false,“assignee_status”:“inbox”,“completed_at”:null,“due_on”:null,“due_at”:null,“projects”:[{“id”:433662746966435,“name”:“Client number 1”}],“tags”:[],“workspace”:{“id”:16583788176280,“name”:“castleford.com.au”},“num_hearts”:0,“hearted”:false,“hearts”:[],“followers”:[{“id”:191437263563258,“name”:“Cam Nealie”}],“memberships”:[{“project”:{“id”:433662746966435,“name”:“Client number 1”},“section”:{“id”:433662746966436,“name”:“Pending”}}],“custom_fields”:[{“id”:437890709441452,“name”:“CP Month”,“type”:“text”,“text_value”:null},{“id”:437890709441454,“name”:“Product”,“type”:“text”,“text_value”:null},{“id”:437890709441456,“name”:“Price”,“type”:“number”,“number_value”:null},{“id”:437890709441458,“name”:“Unique ID”,“type”:“text”,“text_value”:null},{“id”:437890709441460,“name”:“Editorial Units”,“type”:“number”,“number_value”:null},{“id”:437890709441462,“name”:“Social Units”,“type”:“number”,“number_value”:null},{“id”:437890709441464,“name”:“Graphics Units”,“type”:“number”,“number_value”:null},{“id”:437890709441466,“name”:“Strategy Units”,“type”:“number”,“number_value”:null},{“id”:437890709441468,“name”:“PM Units”,“type”:“number”,“number_value”:null},{“id”:437890709441470,“name”:“Video Units”,“type”:“number”,“number_value”:null},{“id”:437890709441473,“name”:“Delivery URL”,“type”:“text”,“text_value”:null},{“id”:437890709441475,“name”:“Scribe ID”,“type”:“text”,“text_value”:null}]}}

It’s the last of the list of custom fields I’m trying to update right now! I’ve tried all kinds of JSON.stringify-ing to no avail.


#2

Can you please reformat your post using the “code” button in the editor?
code
40


#3

Hi there,

One thing that looks like it might be is that the JSON we look for needs to have a top-level “data” object. The goal we’re looking for is something like:

{
    "data": {
        "custom_fields":{ 437890709441475:“product type here” }
    }
}

What happens if you change

var cust = {437890709441475:"product type here"}

to

var cust = {"data":{"custom_fields": {437890709441475:"product type here"}}}

#4

Thank you! Yes this works, I ended up having to add that top level “data” bit.

So glad to have a resolution here, thanks.