0

I am trying to do a simple Salesforce-Asana integration. I have many functions working, but I am having trouble with adding a tag to a workspace. Since I can't find documentation on the addTag method, I'm sort of guessing at what is required.

If I post the following JSON to https://app.asana.com/api/1.0/workspaces/WORKSPACEID/tasks:

{"data":{"name":"MyTagName","notes":"Test Notes"}}

The tag gets created in Asana, but with blank notes and name fields. If I try to get a bit more fancy and post:

{"data":{"name":"MyTagName","notes":"Test Notes","followers":[{"id":"MY_USER_ID"}]}}

I receive:

{"errors":[{"message":"Invalid field: {\"data\":{\"name\":\"MyTagName\",\"notes\":\"Test Notes\",\"followers\":[{\"id\":\"MY_USER_ID\"}]}}"}]}

I'm thinking the backslashes may mean that my request is being modified by the post, though debug output shows a properly formatted json string before the post.

Sample Code:

JSONGenerator jsongen = JSON.createGenerator(false);

jsongen.writeStartObject();
jsongen.writeFieldName('data');
jsongen.writeStartObject();
jsongen.writeStringField('name', 'MyTagName');
jsongen.writeStringField('notes', 'Test Notes');
jsongen.writeFieldName('followers');
jsongen.writeStartArray();
jsongen.writeStartObject();
jsongen.writeStringField('id', 'MY_USER_ID');
jsongen.writeEndObject();
jsongen.writeEndArray();
jsongen.writeEndObject();
jsongen.writeEndObject();

String requestbody = jsongen.getAsString();

HttpRequest req = new HttpRequest();
req.setEndpoint('https://app.asana.com/api/1.0/workspaces/WORKSPACEID/tags');
req.setMethod('POST');

//===Auth header created here - working fine===
req.setBody(requestbody);

Http http = new Http();
HTTPResponse res = http.send(req);

return res.getBody();

Any help appreciated. I am inexperienced using JSON as well as the Asana API.

Aurelio
  • 31
  • 7

4 Answers4

2

The problem was that I was posting to the wrong endpoint. Instead of workspaces/workspaceid/tags, I should have been using /tags and including workspaceid in the body of the request.

Aurelio
  • 31
  • 7
1

Aha, so you can add tags and even set followers despite the API not mentioning that you can or claiming that followers are read-only.

So to sum up for anyone else interested: POSTing to the endpoint https://app.asana.com/api/1.0/tags you can create a tag like this:

{ "data" : { "workspace": 1234567, "name" : "newtagname", "followers": [45678, 6789] } }

where 1234567 is your workspace ID and 45678 and 6789 are your new followers.

ron
  • 1,048
  • 7
  • 16
1

Since you posted this question, Asana's API and developer has introduced Tags. You documentation lays out the answer to your question pretty clearly:

https://asana.com/developers/api-reference/tags

Justin
  • 3,418
  • 3
  • 27
  • 37
Evan Hammer
  • 450
  • 4
  • 13
0

I'm a bit confused by your question. Your ask "how to add a tag" but the first half of your question talks about adding a task. The problem with what you describe there is that you are trying to set a task's followers but the followers field is currently read-only according to Asana's API documentation. That is why you are getting an error. You can not set followers with the API right now.

The second part of your question - with the sample code - does look like you are trying to add a tag. However, right now the Asana API does not support this (at least according to the API documentation). You can update an existing tag but you can't add one.

So, to sum up: at this time the API does not allow you to add followers to a task or to create new tags.

ron
  • 1,048
  • 7
  • 16