How to get old value in On Submit Client Script

rajasekharteja
Tera Guru

Hello All,

 

I got a requirement where we have list of Risk fields on Change Form based on those questions Risk has to be calculated and it's getting calculated.

Now the problem is I need to show alert message to the user when user updates the related Risk fields and saves the form, I have written the below On Submit client script and it's showing the message but I need to show the message only when user updates the Risk Assessment values from one value to other but not on when user updates from Empty to Something(i.e., on Initial update) so please guide me on checking if the previous is empty or not.

 

On Submit Client Script:

 

function onSubmit() {

    var currentState = g_form.getValue('state'); // Returns the value as a string (sys_id or number)
    if (currentState == -3 || currentState == -4) {
        var field1 = g_form.getControl('u_will_the_change_affect_any_business_critical_application_service_critical_site');
        var field2 = g_form.getControl('u_which_regions_are_impacted');
        var field3 = g_form.getControl('u_how_many_users_could_be_impacted_during_the_implementation_of_the_change'); //Get the 'Short description' field control
        var field4 = g_form.getControl('u_how_difficult_it_is_to_roll_back_the_change_to_its_original_state');

        var field5 = g_form.getControl('u_has_testing_been_completed_for_this_chang');
        var field6 = g_form.getControl('u_is_downtime_required');
        var field7 = g_form.getControl('u_in_which_environment_will_this_change_be_implemented');
        var field8 = g_form.getControl('u_has_this_type_of_change_been_deployed_before_without_any_impact');


        //See if the 'changed' attribute on either field is true
        if (field1.changed || field2.changed || field3.changed || field4.changed || field5.changed || field6.changed || field7.changed || field8.changed) {
           alert("Risk Values on the form have changed.\nPlease Click on Calculate Risk Button to calculate the Risk");
            return true;
        }
    }
}
 
FYI: Client is not ready to calculate Risk using Save so we are doing through UI Action.
 
Thanks,
Raja

 

2 REPLIES 2

Brad Bowman
Kilo Patron

The easy way is to do this onChange instead of onSubmit, as the oldValue object is then made available.  Another approach is to create a Display Business Rule on the table, writing the value when the record loads to the scratchpad.  The script for this is simply

(function executeRule(current, previous /*null when async*/ ) {
    g_scratchpad.shortdescription = current.request_item.short_description;
})(current, previous);

or you can get more advanced if needed.  In the Client Script then, you can use something like

var oldvalue = g_scratchpad.shortdescription;

to access this field value as of when the form loaded.

 

kaushal_snow
Giga Sage

@rajasekharteja ,

 

Oldvalue in a client script only reflects the value from when the form loaded and does not give you the previous value after the user has already changed the field once or more.....so in an onsubmit script you cannot directly check a field previous (intermediate) value to compare against the latest value.......and the typical workaround used is to capture the initial loaded value early (for example by storing it in a hidden field or via g_scratchpad in an onload script).......and then in your onsubmit compare the current g_form.getValue(...) to that stored original.......

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Technical Consultant - ServiceNow Class of Legends 2025