Custom field data not saving after code is run
When the app is ran, it updates the Total Disputes custom field in the ticket but does not save the changes after updated. When viewing the ticket in Views, it doesn't show the updated custom field, appearing as though it's not saved.
Is there a save or submit action that can be used in my code?
let hasRun = false;
function runOnce() {
if (!hasRun) {
hasRun = true;
const client = ZAFClient.init();
const ticket_fields = ['ticket.customField:custom_field_13110845437979','ticket.customField:custom_field_13007174759707']
client.get(ticket_fields).then(
function(data) {
const disputes_transactions = data['ticket.customField:custom_field_13110845437979'];
const amounts = disputes_transactions.match(/Amount:\s*\$?(\d+(?:[\.,']\d+)?)/g);
const total_dispute_amount = amounts.reduce((sum, amount) => sum + parseFloat(amount.replace(/Amount:|\$/g, '')), 0);
console.log('Disputes transactions:' + disputes_transactions);
console.log('Transaction Totals: ' + total_dispute_amount);
const totalDisputes = total_dispute_amount;
const ticketCustomFieldData = {
ticket: {
fields: [
{
id: 'custom_field_13007174759707',
value: totalDisputes
}
]
}
};
client.get('ticket.id').then(function(ticketData) {
const ticketId = ticketData['ticket.id'];
client.set('ticket.customField:custom_field_13007174759707', totalDisputes);
const settings = {
url: '/api/v2/tickets/' + ticketId + '.json',
type: 'PUT',
dataType: 'json',
data: ticketCustomFieldData
};
console.log(settings);
client.request(settings).then(
function(data){
console.log(data);
},
function(error) {
console.error('Error updating ticket:', error);
});
},
function(error) {
console.error('Error getting ticket ID:', error);
});
},
function(error) {
console.error('Error getting ticket fields:', error);
});
}
}
runOnce();
-
HeyJesse Spalding,
In general, when you make an update to a ticket field, you do then need to submit the ticket so it saves the changes you've made. Submitting the ticket normally requires the agent to also select a status at the same time.
Perhaps you will need to include a submit function into your code here where you are adding the status into the update you're making.
https://developer.zendesk.com/api-reference/ticketing/tickets/tickets/#update-ticket
I not a developer by nature but I hope this helps. :)
-
Hi, I think the problem is that you are using theticket.save()method inside theticketFields:changedevent handler. This will cause an infinite loop of saving and changing the ticket fields, which may prevent the custom field data from being saved properly. A possible solution is to use a flag variable to check if the custom field data has been updated before saving the ticket. For example:
Hope this helps,
Hanna fromCodeITvar updated = false; // flag variable
client.on('ticketFields:changed', function(e) {
if (e.fieldId === 'status') {
var status = e.value;
if (status === 'solved') {
client.get('ticket.customField:custom_field_360000000000').then(function(data) {
var customField = data['ticket.customField:custom_field_360000000000'];
如果(customField ! = = '是的'){
client.set('ticket.customField:custom_field_360000000000', 'Yes');
updated = true; // set flag to true
}
});
}
}
});
client.on('ticket.save', function() {
如果(更新){/ /切ck flag before saving
return true;
} else {
return false;
}
}); -
In your payloadticketCustomFieldData, you should changefieldstocustom_fields. That should the update issue.
However, the app seems to perform the update every time it is loaded since it will always register with has_run as false. So, every time the ticket is opened, the fields will be updated.
I believe your use case can benefit more from aZIS apprather than a ZAF one.
Pleasesign into leave a comment.
3 Comments