Client Script - alert shows two times

kandulek
Tera Contributor

Hi All,

 

I created the following Client Script to display an alert/popup message, whenever the problem record state changes to Fix in Progress. However, when mandatory fields are not yet filled in before transitioning to the next state, my alert/popup appears two times - before the info about the mandatory fields and after I fill them all in. Any suggestions what to change in the script, so that the alert/popup appears only once?

 

 

onChange Client Script:

 

 

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
	var alertShown = false;
    var RCA_state = 103; // value for Root Cause Analysis state
    var FIP_state = 104; // value for Fix in Progress state

    if (oldValue == RCA_state && newValue == FIP_state) {
        if (!alertShown) {
            if (!g_form.mandatoryCheck()) {
                alert(getMessage('My alert message'));
                alertShown = true;
            }
        }
    } else {
        if (newValue != FIP_state && alertShown) {
            alertShown = false;
        }
    }
}

 

 

 

 

1 ACCEPTED SOLUTION

Bert_c1
Kilo Patron

Try adding:

 

   if (isLoading || newValue === '') {
      return;
   }

   if (oldValue == newValue) {
      return;
   }

at the beginning of your function.

View solution in original post

6 REPLIES 6

jcmings
Mega Sage

I would focus on figuring out why your mandatory fields aren't being enforced.

 

You probably don't need an alertShown variable. Is that even being evaluated, given it's outside the function?

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    if (newValue == 104) {
        alert('message');
    }

}

 

 

Brad Bowman
Kilo Patron
Kilo Patron

Seems like you're over-complicating it.  What happens if you try this? 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var RCA_state = 103; // value for Root Cause Analysis state
    var FIP_state = 104; // value for Fix in Progress state

    if (oldValue == RCA_state && newValue == FIP_state) {
        alert('My alert message.');
    } 
}

If you need to add something back in you can

 

kandulek
Tera Contributor

Hi,

 

Thank you for your answers!

 

Actually, I started with the simple code to just add my alert, but then it was displayed 4 times...

 

That is why I added this flag to prevent it to be displayed multiple times, but still it displays 2 times, instead of just once.

The script will run every time the trigger field (State?) changes, so you might want to look at what is changing the field repeatedly in this process, or the steps of your test case is.  An onChange script does not trigger if you manually select the same choice value as there is no change, so it sounds like you are clicking on a UI Action or have another script to change the State, without checking if the state is already that value.

 

In the case of the out of box Resolve UI Action on the incident, you can modify the beginning of this script:

function resolveIncident() {
    //Set the 'Incident state' and 'State' values to 'Resolved', and display mandatory fields.
    g_form.setValue('state', 6);

 to check the value first:

function resolveIncident() {
    //Set the 'Incident state' and 'State' values to 'Resolved', and display mandatory fields.
    if (g_form.getValue('state') != 6) {
        g_form.setValue('state', 6);
    }