Custom app fails to load intermittently

Hi Everyone

Hope you’re doing well. Right now, I’m building a custom app for a Freshdesk Support Desk. This app is activated in the location new_ticket_background and calls for a third API when a series of characters are inserted in a custom field. The app works fine except that on some occasions, when the instance is still charging at a backend level, the app doesn’t detect the event ‘ticket.customFieldChanged’ when I change the values inside the custom field.

Here is the code of the app.js field:

var client;

(async function init() {
client = await app.initialized();
//client.events.on(‘ticket.customFieldChanged’, eventCallBack);
client.events.on(‘app.activated’, infoGet);
})();

//async function getData(event) {
// console.log(“custom field changed”);
// var customField = event;
// var numero_serie = customField.cf_nmero_de_serie.new;

// console.log(numero_serie);

//}

async function yourFunction() {
console.log(‘now’)

function sleep(ms) {
return new Promise(resolve => {
let startTime = Date.now()
let currentTime = null
do {
currentTime = Date.now()
} while (currentTime - startTime < ms);
resolve()
});
}

await sleep(5000)

$(document).ready ( function() {
app.initialized().then(function(client) {
window.client = client;
client.events.on(“ticket.customFieldChanged”, eventCallBack);
});
});

console.log(‘5 secs later’)
var anumero_serie = customField.cf_nmero_de_serie;
console.log(anumero_serie);

var eventCallBack = function (event) {

console.log("PRUEBA");
var customField = event.data; 
var numero_serie = customField.cf_nmero_de_serie.new;  
var total_n_length = numero_serie.length; 
console.log(total_n_length);
var anumero_serie = customField.cf_nmero_de_serie;
console.log(anumero_serie);

if(total_n_length < 17) { 
  try {
    client.interface.trigger("showNotify", {
       type: "danger",
       title: "Danger",
       message: "Debe ingresar 17 caracteres o mas"
       /* The "message" should be plain text */
   });

} catch (error) {
// failure operation
console.error(error);
}
} else {

  var url = "URL";

  axios.get(url).then(resp => {

  var jsonResponse = resp.data;
  var message = resp.data.success
  
    if(message == false) {

      client.interface.trigger("showConfirm", {
        title: "Datos no encontrados",
        message: "¿Desea realizar otra busqueda?", saveLabel: "Si", cancelLabel: "No"
      
  
  
  
      }).then(function(result) {

        var decission = result.message; 

        if(decission == "Si") { 

          client.interface.trigger("setValue", {id: "cf_nmero_de_serie", value: []}); 
          client.interface.trigger("setValue", {id: "cf_nmero_de_motor", value: []});
          client.interface.trigger("setValue", {id: "cf_modelo", value: []});   // modelo 
          client.interface.trigger("setValue", {id: "cf_color", value: []});  // Color
          client.interface.trigger("setValue", {id: "cf_cilindraje", value: []});  // Cilindraje
          client.interface.trigger("setValue", {id: "cf_ao", value: []}); // A;o
          client.interface.trigger("setValue", {id: "cf_marca", value: []}); // Marca 


        } else if(decission == "No") {

          client.interface.trigger("setValue", {id: "cf_motor", value: u_MMOTOR}); // numero de motor
          client.interface.trigger("setValue", {id: "cf_modelo", value: u_modelo});   // modelo 
          client.interface.trigger("setValue", {id: "cf_color", value: u_color});  // Color
          client.interface.trigger("setValue", {id: "cf_cilindraje", value: u_cilindraje});  // Cilindraje
          client.interface.trigger("setValue", {id: "cf_ao", value: u_anno}); // A;o
          client.interface.trigger("setValue", {id: "cf_marca", value: u_marca}); // Marca 


        }


       });


    } else {
      var data = jsonResponse.data; 
      var firstItem = data[0];
        var u_MSERIE = firstItem.u_MSERIE;
        console.log(u_MSERIE);
        var u_MMOTOR = firstItem.u_MMOTOR;
        var u_modelo = firstItem.u_MModelo;
        var u_color = firstItem.u_MColor;
        var u_cilindraje = firstItem.u_MCilindros;
        var u_anno = firstItem.u_MAno; 
        var u_marca = firstItem.u_MMarca; 
    
        client.interface.trigger("setValue", {id: "cf_nmero_de_motor", value: u_MMOTOR}); // numero de motor
        client.interface.trigger("setValue", {id: "cf_modelo", value: u_modelo});   // modelo 
        client.interface.trigger("setValue", {id: "cf_color", value: u_color});  // Color
        client.interface.trigger("setValue", {id: "cf_cilindraje", value: u_cilindraje});  // Cilindraje
        client.interface.trigger("setValue", {id: "cf_ao", value: u_anno}); // A;o
        client.interface.trigger("setValue", {id: "cf_marca", value: u_marca}); // Marca 
    }

});


}

}
}

async function infoGet() {
await yourFunction();
await test();
}

Thanks