How to delay a second confirmation dialog until just before GlideRecord update in a UI Action?

ronro2
Tera Contributor

Hey guys! 

As the title says, how do I delay the second confirmation dialog until just before GlideRecord update in a UI Action? The second dialog is asking whether or not the user really wants to set the status to Closed Complete. 

The first dialog is telling the user that there are child activities that need to be closed first, before he/she can continue to close the parent. 

Here is what my UI Action looks like: 

 

function cancelOrder() {
  var errorMessages = [];

  // Kontrollera att användaren fyllt i orsaken
  var reason = g_form.getValue('cancellation_reason');
  g_form.setDisplay('cancellation_reason', true);
  g_form.setMandatory('cancellation_reason', true);

  if (!reason) {
    errorMessages.push('Du måste ange en orsak till annulleringen.');
  }

  // Kontrollera child tasks via GlideAjax
  var ga = new GlideAjax('x_vgll_decom.CancelOrderAJAX');
  ga.addParam('sysparm_name', 'checkChildTasks');
  ga.addParam('sysparm_sys_id', g_form.getUniqueValue());

  ga.getXMLAnswer(function(response) {
    if (response !== 'OK') {
      errorMessages.push('Avvecklingsärendet har fortfarande aktiva beställningar och går därför inte att avsluta');
    }

    if (errorMessages.length > 0) {
      errorMessages.forEach(function(msg) {
        g_form.addErrorMessage(msg);
      });
      return;
    }

    // Visa första bekräftelsen
    var confirmation = confirm("Beställningar i ärendets aktiviteter måste annulleras manuellt först");
    if (!confirmation) {
      return;
    }

    // Visa andra bekräftelsen
    var confirmation2 = confirm("Vill du verkligen annullera detta ärende?");
    if (!confirmation2) {
      return;
    }

    // Anropa servern för att annullera
    var gaCancel = new GlideAjax('x_vgll_decom.CancelOrderAJAX');
    gaCancel.addParam('sysparm_name', 'cancelOrder');
    gaCancel.addParam('sysparm_sys_id', g_form.getUniqueValue());

    gaCancel.getXMLAnswer(function(cancelResponse) {
      if (cancelResponse === 'OK') {
        g_form.clearModified();
        g_form.refresh();
      } else {
        g_form.addErrorMessage('Kunde inte annullera beställningen: ' + cancelResponse);
      }
    });
  });
}

 


Here is what my Script Includes looks like: 

 

var CancelOrderAJAX = Class.create();
CancelOrderAJAX.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

  // Kontrollera att alla child tasks är i state 3 eller 7
  checkChildTasks: function() {
    var sysId = this.getParameter('sysparm_sys_id');
    var taskGR = new GlideRecord('x_vgll_decom_decommission_task');
    taskGR.addQuery('parent', sysId);
    taskGR.addQuery('state', 'NOT IN', '3,7');
    taskGR.query();

    if (taskGR.hasNext()) {
      return 'Fel: Alla underliggande aktiviteter måste vara avslutade (state 3 eller 7)';
    }

    return 'OK';
  },

  // Avbryt ordern
  cancelOrder: function() {
    var sysId = this.getParameter('sysparm_sys_id');
    gs.info('CancelOrderAJAX: Försöker avbryta order med sys_id: ' + sysId);

    var gr = new GlideRecord('x_vgll_decom_decommission');
    if (!gr.get(sysId)) {
      gs.warn('CancelOrderAJAX: Kunde inte hitta posten med sys_id: ' + sysId);
      return 'Fel: kunde inte hitta posten';
    }

    if (!gr.canWrite()) {
      gs.warn('CancelOrderAJAX: Saknar rättigheter att uppdatera posten med sys_id: ' + sysId);
      return 'Fel: saknar rättigheter att uppdatera posten';
    }

    gr.state = 5;
    gr.decom_state = 'closed_abandoned';
    gr.update();

    gs.info('CancelOrderAJAX: Beställning avbruten för sys_id: ' + sysId);
    return 'OK';
  },

  type: 'CancelOrderAJAX'
});

 


The problem is that the second confirmation dialog appears right after the first one.

An alternate solution is to showcase both errorMessages (listed below) in the banner simultaniously/at the same time: 

-  errorMessages.push('Avvecklingsärendet har fortfarande aktiva beställningar och går därför inte att avsluta');

-  errorMessages.push('Du måste ange en orsak till annulleringen.');

In this case (alternate solution) I need only one dialogue. 

Thanks in advance!

1 ACCEPTED SOLUTION

@ronro2 it doesn't look like you applied the suggestion I provided. Your current script looks like this:

 

 // Visa första bekräftelsen
    var confirmation = confirm("Beställningar i ärendets aktiviteter måste annulleras manuellt först");
    if (!confirmation) {
      return;
    }

    // Nu först anropa andra bekräftelsen strax innan serverkallet
    var confirmation2 = confirm("Vill du verkligen annullera detta ärende?");
    if (!confirmation2) {
      g_form.addErrorMessage('Annulleringen avbröts av användaren.');
      return;
    }

 

In this script, you ask for confirmation 1 and then confirmation 2. Confirmation 2 is not dependent on confirmation 1. In both instances, you are checking for the false presence of the confirmation, and then doing nothing. Instead, you need to check for the true presence of confirmation 1, and then do confirmation 2.

 

This would look something like this:

 

// Visa första bekräftelsen
var confirmation = confirm("Beställningar i ärendets aktiviteter måste annulleras manuellt först");
if (confirmation) { //changed to look for TRUE condition
  // Nu först anropa andra bekräftelsen strax innan serverkallet
  var confirmation2 = confirm("Vill du verkligen annullera detta ärende?");
  if (confirmation2) { //changed to look for TRUE condition
    //call function here for TRUE confirmation
  } else { //this is if confirmation 2 is not true
    g_form.addErrorMessage('Annulleringen avbröts av användaren.');
    return;
  } else { //this is if confirmation 1 is not true
    //do something
  }
}

 

 

 

 

View solution in original post

3 REPLIES 3

jcmings
Mega Sage

You would need to move some code slightly, so that confirmation2 only appears if confirmation1 is true.

 var confirmation = confirm("Beställningar i ärendets aktiviteter måste annulleras manuellt först");
    if (confirmation) {
      // Visa andra bekräftelsen
      var confirmation2 = confirm("Vill du verkligen annullera detta ärende?");
    }

    

 

And then if confirmation2 is true, you can call whatever script is needed. If false, add the error message. 

ronro2
Tera Contributor

Like this @jcmings ? It didn't work. They still come right after each other: 

function cancelOrder() {
  var errorMessages = [];

  // Kontrollera att användaren fyllt i orsaken
  var reason = g_form.getValue('cancellation_reason');
  g_form.setDisplay('cancellation_reason', true);
  g_form.setMandatory('cancellation_reason', true);

  if (!reason) {
    errorMessages.push('Du måste ange en orsak till annulleringen.');
  }

  // Kontrollera child tasks via GlideAjax
  var ga = new GlideAjax('x_vgll_decom.CancelOrderAJAX');
  ga.addParam('sysparm_name', 'checkChildTasks');
  ga.addParam('sysparm_sys_id', g_form.getUniqueValue());

  ga.getXMLAnswer(function(response) {
    if (response !== 'OK') {
      errorMessages.push('Avvecklingsärendet har fortfarande aktiva beställningar och går därför inte att avsluta');
    }

    if (errorMessages.length > 0) {
      errorMessages.forEach(function(msg) {
        g_form.addErrorMessage(msg);
      });
      return;
    }

    // Visa första bekräftelsen
    var confirmation = confirm("Beställningar i ärendets aktiviteter måste annulleras manuellt först");
    if (!confirmation) {
      return;
    }

    // Nu först anropa andra bekräftelsen strax innan serverkallet
    var confirmation2 = confirm("Vill du verkligen annullera detta ärende?");
    if (!confirmation2) {
      g_form.addErrorMessage('Annulleringen avbröts av användaren.');
      return;
    }

    // Anropa servern för att annullera
    var gaCancel = new GlideAjax('x_vgll_decom.CancelOrderAJAX');
    gaCancel.addParam('sysparm_name', 'cancelOrder');
    gaCancel.addParam('sysparm_sys_id', g_form.getUniqueValue());

    gaCancel.getXMLAnswer(function(cancelResponse) {
      if (cancelResponse === 'OK') {
        g_form.clearModified();
        g_form.refresh();
      } else {
        g_form.addErrorMessage('Kunde inte annullera beställningen: ' + cancelResponse);
      }
    });
  });
}

 

@ronro2 it doesn't look like you applied the suggestion I provided. Your current script looks like this:

 

 // Visa första bekräftelsen
    var confirmation = confirm("Beställningar i ärendets aktiviteter måste annulleras manuellt först");
    if (!confirmation) {
      return;
    }

    // Nu först anropa andra bekräftelsen strax innan serverkallet
    var confirmation2 = confirm("Vill du verkligen annullera detta ärende?");
    if (!confirmation2) {
      g_form.addErrorMessage('Annulleringen avbröts av användaren.');
      return;
    }

 

In this script, you ask for confirmation 1 and then confirmation 2. Confirmation 2 is not dependent on confirmation 1. In both instances, you are checking for the false presence of the confirmation, and then doing nothing. Instead, you need to check for the true presence of confirmation 1, and then do confirmation 2.

 

This would look something like this:

 

// Visa första bekräftelsen
var confirmation = confirm("Beställningar i ärendets aktiviteter måste annulleras manuellt först");
if (confirmation) { //changed to look for TRUE condition
  // Nu först anropa andra bekräftelsen strax innan serverkallet
  var confirmation2 = confirm("Vill du verkligen annullera detta ärende?");
  if (confirmation2) { //changed to look for TRUE condition
    //call function here for TRUE confirmation
  } else { //this is if confirmation 2 is not true
    g_form.addErrorMessage('Annulleringen avbröts av användaren.');
    return;
  } else { //this is if confirmation 1 is not true
    //do something
  }
}