Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to show oldValue and newValue in alert everytime if I change state field on incident form?

NishantDhole
Mega Guru

Hi,

 

Please find below scenario: 

On Incident form,

if state field old value is New|1 and I change it to inProgress|2, then it should popup an alert as old value : New|1 and new value: inProgess|2. 

 

 Now, if I change state field New|1 again then it should be alerted like oldValue: inProgress|2 and newValue : New|1

 

Note: Form should not be saved. It should be achieved by without saving form.

3 ACCEPTED SOLUTIONS

Hiteish22
Tera Expert

For above use case please find below solution :

 

We need to write on load client script on incident table :

function onLoad() {
    //Type appropriate comment here, and begin script below
    var oldStateValue = g_form.getValue('state');

    function showAlert() {
        var newStateValue = g_form.getValue('state');
        alert('Old State: ' + oldStateValue + '\nNew State: ' + newStateValue);
        oldStateValue = newStateValue;
    }
    g_form.getControl('state').addEventListener('change', showAlert);
}

View solution in original post

Hiteish222
Kilo Guru

Hi @NishantDhole ,

For above use case please find below solution :

 

We need to write on load client script on incident table :

function onLoad() {
    //Type appropriate comment here, and begin script below
    var oldStateValue = g_form.getValue('state');

    function showAlert() {
        var newStateValue = g_form.getValue('state');
        alert('Old State: ' + oldStateValue + '\nNew State: ' + newStateValue);
        oldStateValue = newStateValue;
    }
    g_form.getControl('state').addEventListener('change', showAlert);
}

View solution in original post

@NishantDhole if that is the case then yeah you can go with the below code:

 

 

function onLoad() {
    // Capture the initial value of the state field
    var oldStateValue = g_form.getValue('state');

    // Define the function to display the alert with old and new state values
    function showAlert() {
        var newStateValue = g_form.getValue('state');
        alert('Old State: ' + oldStateValue + '\nNew State: ' + newStateValue);
        oldStateValue = newStateValue; // Update the old state value to the current state
    }

    // Add an event listener to the state field to detect changes
    g_form.getControl('state').addEventListener('change', showAlert);
}

 

 

 

…………………………………………........................................................................................
Mark it helpful 👍and Accept Solution !! If this helps you to understand.

…………………………………………........................................................................................

View solution in original post

8 REPLIES 8

Satishkumar B
Giga Sage
Giga Sage

Hi @NishantDhole for your scenario i would prefer using OnChange client script as it would give you alert immediatly once you change the state.
try using below code:

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === oldValue) {
        return;
    }
    var oldLabel = g_form.getOption('state', oldValue).text;
    var newLabel = g_form.getOption('state', newValue).text;

    alert('Old Value: ' + oldLabel + '\nNew Value: ' + newLabel);
}

 

 

SatishkumarB_0-1722527884977.png

@Rohit99 your code looks good but it would give you backend name of the choice.

…………………………………………........................................................................................
Mark it helpful 👍and Accept Solution !! If this helps you to understand.

…………………………………………........................................................................................

 

Hi @Satishkumar B ,

 If I want old value only once then i would have used it. but my requirement was different. I am change both value multiple times so old value and new value also changing simultaneously. And as per your solution old value remain same. if i change state field value multiple time then old value also should change. 

 

Consider scenario:

 

case 1)The state field has the value inProgress, now if change the state field value to on hold 

in above case 1 : alert should populate like old value: inprogress or 2 and new Value : onhold or 3.

 

case2) now on same incident if change the state field value to new  without loading and saving the form then

 

alert should populate like : old value : onhold or 3 and new value : new or 1

 

 

In your case it will always show same old value.

@NishantDhole if that is the case then yeah you can go with the below code:

 

 

function onLoad() {
    // Capture the initial value of the state field
    var oldStateValue = g_form.getValue('state');

    // Define the function to display the alert with old and new state values
    function showAlert() {
        var newStateValue = g_form.getValue('state');
        alert('Old State: ' + oldStateValue + '\nNew State: ' + newStateValue);
        oldStateValue = newStateValue; // Update the old state value to the current state
    }

    // Add an event listener to the state field to detect changes
    g_form.getControl('state').addEventListener('change', showAlert);
}

 

 

 

…………………………………………........................................................................................
Mark it helpful 👍and Accept Solution !! If this helps you to understand.

…………………………………………........................................................................................

Hi @Satishkumar B ,

 

I got it already from @Hiteish222 . Thank you for your help.