API & getting multiple custom fields


#1

Hey folks,

So I’m trying to use Google Sheets/Scripts to get information about all of the custom fields associated with tasks in a project in Asana. I can get the first custom field but am unable to get any of the other ones. Any help would be greatly appreciated.

function myFunction() {
var bearerToken = “XXXXXXXXXXXXXXXXXXXXXX”;
var requestUrl = “https://app.asana.com/api/1.0/projects/615653281333293/tasks? opt_fields=id,assignee,assignee_status,created_at,completed,completed_at,due_on,name,custom_fields,custom_fields,custom_fields&limit=25”

 var headers = {
  "Authorization" : bearerToken
};

var reqParams = {
  method : "GET",
  headers : headers,
  muteHttpExceptions: true
};
 
  
  var response = UrlFetchApp.fetch(requestUrl, reqParams);
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets()
  var sheet = ss.getSheetByName("99 Problems"); 
  sheet.getRange('A3:Z').clearContent();

  Logger.log(response.getContentText());
 
  var dataAll = JSON.parse(response.getContentText());
  var dataSet = dataAll.data;
  
  var rows = [],
      data;
  
  do{
     var i = 0;
     for (i; i < dataSet.length; i++)
     {
       data = dataSet[i];
       rows.push([data.id, data.name, data.assignee, data.created_at, data.completed_at, data.completed,data.due_on, data.custom_fields,data.custom_fields])  
     }

#2

Maybe you will get luckier using the “Explorer” tab on https://asana.com/developers/api-reference/tasks
That could help understand how it works!

Bastien
Asana Certified Pro, consultant, author and developer


#3

Thanks for the suggestion. Sadly I can’t find any information there about getting multiple custom fields there. I’m not trying to call each task individually, rather as a group from a project. I can get the first custom field from each task just fine.


#4

What @Bastien_Siebman was suggesting - a good idea! - is to run your request (i.e. projects/615653281333293/tasks? opt_fields=id,assignee,assignee_status,created_at,completed,completed_at,due_on,name,custom_fields,custom_fields,custom_fields although you don’t need custom_fields 3 times) in Explorer and see what you get back for custom fields.

It’s really odd that you would only get one - they come together as an array. Are you sure the other custom fields have been added to that particular project? My recollection is I believe you’ll only get back custom fields which exist in the particular project you’re querying.


#5

@Phil_Seeman Thanks for clarifying! I’ve been looking at the explorer and don’t see a way to get custom fields from it. Am I just missing it somewhere? I figured out how to add it to the url by trial and error. If they are coming as an array then the error might be in how I’m pushing the data to google sheets.


#6

@Phil_Seeman Your bit about the custom fields coming as an array totally solved it. Thanks! I needed to be pushing data.custom_fields[0], data.custom_fields[1] ect.

@Bastien_Siebman Thanks for pointing me in a good direction!