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

 

1 REPLY 1

Brad Bowman
Kilo Patron
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.