Avoid to update ticket

Hi guys,

Hope you are doing well. Right now Im building a custom app for freshservice and I need to avoid to update the ticket when the status is equal to 3. Im trying to use the intercept method but its not working. Can you please give me a hand? This is the code:

document.onreadystatechange = function() {
  if (document.readyState === 'interactive') renderApp();

  function renderApp() {
    var onInit = app.initialized();

    onInit
      .then(function getClient(_client) {
        window.client = _client;
        client.events.on('app.activated', renderContactName, { intercept: true });
        client.events.on("ticket.propertiesUpdated", eventCallBack, { intercept: true });
        client.events.on("ticket.statusChanged", propertiesUpdatedCallback, {
          intercept: true
        });
      })
      .catch(handleErrors); // Corrected the function name here
  }
};

var handleErrors = function(e) { // Simplified error handler function signature
  console.error("Error occurred: ", e.message); // Log the error message to console
};

function renderContactName() { 
  console.log("APP INICIADA ");
  let changeEvents = [
    'ticket.priorityChanged',
    'ticket.statusChanged',
    'ticket.groupChanged',
    'ticket.agentChanged',
    'ticket.typeChanged', // Corrected typo here
    'ticket.propertiesUpdated'
  ];
  changeEvents.forEach(function(click) {
    client.events.on(click, function(event) {
      let eventName = event.type;
      let { old: prevVal, new: newVal } = event.helper.getData();
      console.log(eventName, prevVal, newVal);
      if (eventName == "ticket.statusChanged" && newVal == "3") {
        console.log("PRUEBA DETECTCION DE STATUS LISTS");
        event.helper.fail(
          "Timer(s) running. Stop the timer(s) before proceeding to close the ticket."
        );
      }
    });
  });
}

function eventCallBack(event) { 
  let eventName = event.type;
  var data = event.helper.getData(); 
  var statusA = data.status.value; 
  console.log(statusA, "DATA EVENTO TICKET PROPERTIES UPDATED");
  if (statusA == '3') {
    event.helper.fail(
      "Timer(s) running. Stop the timer(s) before proceeding to close the ticket."
    );
  } else {
    console.log("TICKET NO CERRADO");
  }
}

function propertiesUpdatedCallback(event) {
  // Ensure you define this function or ensure it is defined elsewhere in your code
  console.log("Properties updated event received", event);
}

Thanks

Not entirely sure about Freshservice, but in Freshdesk the event.helper.getData(); function only returns the updated ticket fields. Since your eventCallBack function triggers on all ticket updates you’d have to get the status some other way (if it is not one of the changed values).

In Freshdesk I use this function for it:

 async function getTicketDetails() {
  try {
    const data = await client.data.get("ticket");
    // success output
    // data is {ticket: {"subject": "support needed for..",..}}
    console.log(data);
  } catch (error) {
    // failure operation
    console.log(error);
  }
}

I haven’t checked the Freshservice app documentation but I imagine it to be something fairly similar since Freshworks standardises quite much

Hello Thomas,

Regarding this matter, what I need to do is ensure that the ticket is not updated under certain conditions. Specifically, when the status is 3, the objective is to intentionally trigger an error for a brief period, allowing us to call a third-party API to update a custom field, and subsequently proceed with updating the ticket.

What do you think about that?

Hey @Andrea_Lopez_Vargas

Could you check your eventCallBack function.
The getData() should return a changedAttributes object.

function eventCallBack(event) {
  let eventName = event.type;
  console.log({ eventName });
  var data = event.helper.getData();
  console.log(data, data.changedAttributes.status[1]);
  // var statusA = data.status.value;
  // console.log(statusA, "DATA EVENTO TICKET PROPERTIES UPDATED");
  if (data.changedAttributes.status[1] == 3) {
    event.helper.fail("Timer(s) running. Stop the timer(s) before proceeding to close the ticket.");
  } else {
    console.log("TICKET NO CERRADO");
    event.helper.done();
  }
}

I tried and it worked. Hope it helps

Hi @Andrea_Lopez_Vargas,

Greetings!

Is the primary motivation behind developing the application centred around evading the update based on ticket status?
If this is the case you can check out the business rules…

Thank you.

What you should also keep in mind is that updates can also be done from other pages or even via API.
So agents do have the options to use scenarios or bulk update (from list-view) to update a ticket, and unfortunately there is no option to intercept such updates.