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

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);
}

Hi @Hiteish22 ,

  I have tried this solution and its working.

Thanks!

 

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);
}

Rohit99
Mega Sage

Hi @Hiteish22
you can achieve this requirement using onChange Client Script.

Rohit99_0-1722493466549.png

 

 

 

Please mark my response as correct and helpful if it helped solved your question.

 

Thanks,

Rohit Suryawanshi