'Zendesk API: Update Multiple Tickets at once using PUT Method? How does it look like? Javascript/Google Script
I'm currently trying to do an API call to update multiple tickets at once using ticket IDs. These ticket IDs are stored on google Sheets since I have the script running there. I would appreciate someone sharing how to use the REST method and get it working! Thank you!
Problem: I'm not sure how to update multiple tickets using the following
If I try with 1 ticket ID, example PUT https://company.zendesk.com/api/v2/tickets/12345 This is from https://developer.zendesk.com/api-reference/ticketing/tickets/tickets/#update-ticket. This will only update 1 ticket, fair.
Original Code: PUT /api/v2/tickets/{ticket_id} I know to just replace {ticket_id} with a ticket number. If I put something like https://company.zendesk.com/api/v2/tickets/{ticket_id} it will throw me an error.
When it comes to multiple: (Source: https://developer.zendesk.com/api-reference/ticketing/tickets/tickets/#update-many-tickets). How do I write the code in a way I can read multiple ticket ids at once without having to repeat it 10 time if there are 10 tickets?
PUT https://company.zendesk.com/api/v2/tickets/update_many
Zendesk's example is saying it like this:
{
"tickets": [
{ "id": 1, "status": "solved" },
{ "id": 2, "status": "pending" }
]
}
Solution 1:[1]
When I saw your question, I thought that your goal might be able to be achieved by modifying this sample script. But, from your question, in your situation, I thought that when a sample modified script is shown, it might be useful for you and other users. So I posted a modified script instead of the duplicated question.
Prepare Spreadsheet:
This script retrieves the values from Spreadsheet. So, please set id and status to the columns "A" and "B" of the header row. And, please set the values of id and status.
Sample script:
Please copy and paste the following script to the script editor of Google Spreadsheet. And please set the variables.
function sample() {
var url = 'https://{subdomain}.zendesk.com/api/v2/tickets/update_many.json'; // Please set your URL.
var user = '###'; // Please set your user name.
var pwd = '###'; // Please set your passworkd.
var sheetName = "Sheet1"; // Please set the sheet name.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var [headers, ...rows] = sheet.getDataRange().getDisplayValues();
var tickets = rows.map(r => {
var temp = {};
headers.forEach((h, j) => {
if (r[j] != "") temp[h] = r[j];
});
return temp;
});
var options = {
'method': 'PUT',
'headers': { 'Authorization': "Basic " + Utilities.base64Encode(user + ':' + pwd) },
'payload': JSON.stringify({ tickets }),
'contentType': 'application/json',
'muteHttpExceptions': true
};
var response = UrlFetchApp.fetch(url, options);
console.log(response.getContentText());
}
When the values are retrieved from the Spreadsheet,
JSON.stringify({ tickets })is as follows.{ "tickets": [ {"id": "1", "status": "status1"}, {"id": "2", "status": "status2"}, {"id": "3", "status": "status3"}, , , , ] }
Note:
- I think that if your values of
userandpwdare invalid, an error occurs. At that time, please confirm your values again.
References:
- Batch updates of Tickets
- At
PUT /api/v2/tickets/update_many, the document saysAccepts an array of up to 100 ticket objects, or a comma-separated list of up to 100 ticket ids.. Please be careful about this.
- At
- Related thread.
- fetch(url, params)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tanaike |
