I am trying to batch create asana tasks via Zapier. As a non-developer who understands some of the principles, I have gotten most of the way towards creating an Batch API request for these.
Though I am getting a “You should specify one of workspace, parent, projects” error despite having the project ID in there.
Can anyone help?
Thanks
Code below
const postUrl = "https://app.asana.com/api/1.0/batch";
const authToken = "token"; // Replace with your actual authentication token
// Split the 'id' string into an array of individual ids
const id = inputData.id.split(",");
const name = inputData.name.split(",");
const due_on = inputData.due_on.split(",");
const projects = inputData.projects;
const assignee_section = inputData.assignee_section;
const resource_subtype = inputData.resource_subtype;
const relative_path = inputData.relative_path;
const method = inputData.method;
const workspace = inputData.workspace;
const team_gid = inputData.team_gid;
let actions = [];
for (let i = 0; i < id.length; i++) {
let codeObject = {
projects: [projects],
workspace: workspace,
team_gid: team_gid,
method: method,
relative_path: relative_path,
assignee_section: assignee_section,
resource_subtype: resource_subtype,
name: name[i],
due_on: due_on[i],
};
actions.push(codeObject);
}
let output;
// Use the `fetch` API to send a request to the specified endpoint
fetch(postUrl, {
method: 'POST',
headers: {
Authorization: `Bearer ${authToken}`,
accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
data: { actions },
}),
})
.then(response => response.json())
.then(data => {
// Process the response data as needed
console.log('Response from server:', data);
output = {
response: data,
};
// Use the `callback` function to signal completion4
// Pass null as the first argument to indicate no error
// Pass the `output` object as the second argument to pass data to the next step
return callback(null, output);
})
.catch(error => {
// Handle errors
console.error('Error:', error);
return callback(error, null);
});
Hi @Jack_Dimond-Whyles,
A couple of things…
team_gid
is not a valid task property, take that out. Also you don’t need to specify a team; you’re specifying a project which lives in a team.
method
and relative_path
are not valid task properties, take those out.
If it still doesn’t work after removing the above, see if you can capture and post here the value of codeObject
so we can see the actual data being sent to Asana.
Thanks, have taken the team out.
Relative_path and method I have put in on the guidance of the parallel request page for the batch API.
https://developers.asana.com/reference/createbatchrequest
This has brought me very close to the point that I am getting an error on the project object type:
const postUrl = "https://app.asana.com/api/1.0/batch"; // Replace with your actual API endpoint
const authToken = "2/1204862057955926/1206158881949677:822956821d9d5d1464bcf4f9712d2abe"; // Replace with your actual authentication token
// Split the 'id' string into an array of individual ids
const id = inputData.id.split(",");
const name = inputData.name.split(",");
const due_on = inputData.due_on.split(",");
const projects = inputData.projects;
const assignee_section = inputData.assignee_section;
const resource_subtype = inputData.resource_subtype;
const relative_path = inputData.relative_path;
const method = inputData.method;
let actions = [];
for (let i = 0; i < id.length; i++) {
let codeObject = {
method: method,
relative_path: relative_path,
data: {
projects: projects,
due_on: due_on[i],
name: name[i]
}
};
actions.push(codeObject);
}
let output;
// Use the `fetch` API to send a request to the specified endpoint
fetch(postUrl, {
method: 'POST',
headers: {
Authorization: `Bearer ${authToken}`,
accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
data: {
actions: actions // Corrected the structure here
}
}),
})
.then(response => response.json())
.then(data => {
// Process the response data as needed
console.log('Response from server:', data);
output = {
response: data,
};
// Use the `callback` function to signal completion
// Pass null as the first argument to indicate no error
// Pass the `output` object as the second argument to pass data to the next step
return callback(null, output);
})
.catch(error => {
// Handle errors
console.error('Error:', error);
return callback(error, null);
});
Getting this error on each object, which makes me think I am very close:
projects: [0]: Not the correct type
Oh, sorry, you’re right about those parameters; I haven’t used the Batch API.
projects
needs to be a JSON array and while we can’t tell unless you post the actual data you’re submitting, it doesn’t look like it is from your code.
In other words:
{
"data": {
"name": "My task",
"projects": [
"123456789"
],
"due_on": "2023-12-25"
}
}
Thanks. Yeah its tricky as I’m passing it through Zapier.
I’ve since hard coded the project ID and it worked fine, so just need to figure out how to transform the input in data.
Thanks for the help!
I’ve figured out how to produce my code object.
Here it is. It is all coming down to the projects object type not being accepted. When I hard code the gid into the zap its fine, but when I carry through the out from Zapier it doesn’t accept the type.
{
"method": "post",
"relative_path": "/tasks",
"data": {
"projects": "1206157904677685",
"due_on": "2024-01-03T00:00:00Z",
"name": "University of Cambridge Students' Union Email Solus",
"resource_subtype": "milestone"
}
}
{
"method": "post",
"relative_path": "/tasks",
"data": {
"projects": "1206157904677685",
"due_on": "2024-01-03T00:00:00Z",
"name": "Swansea Students' Union Email Solus",
"resource_subtype": "milestone"
}
}
{
"method": "post",
"relative_path": "/tasks",
"data": {
"projects": "1206157904677685",
"due_on": "2023-12-14T00:00:00Z",
"name": "Sheffield Hallam University Students' Union Email Solus",
"resource_subtype": "milestone"
}
}
{
"method": "post",
"relative_path": "/tasks",
"data": {
"projects": "1206157904677685",
"due_on": "2024-01-03T00:00:00Z",
"name": "University of Cumbria Students' Union Email Solus",
"resource_subtype": "milestone"
}
}
{
"method": "post",
"relative_path": "/tasks",
"data": {
"projects": "1206157904677685",
"due_on": "2023-12-14T00:00:00Z",
"name": "Coventry Students' Union Email Solus",
"resource_subtype": "milestone"
}
}