Custom field data not saving after code is run



Gepostet 28. Apr. 2023

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();

0

2

2 Kommentare

Hi, I think the problem is that you are using the ticket.save() method inside the ticketFields:changed event 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 from CodeIT

var 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'];
        if (customField !== 'Yes') {
          client.set('ticket.customField:custom_field_360000000000', 'Yes');
          updated = true; // set flag to true
        }
      });
    }
  }
});

client.on('ticket.save', function() {
  if (updated) { // check flag before saving
    return true;
  } else {
    return false;
  }
});

1


Hey Jesse 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. :)

 

 

1


Melden Sie sich an, um einen Kommentar zu hinterlassen.

Sie finden nicht, wonach Sie suchen?

Neuer Post