Not able pickup oldValue for confirm dialog box

Joyal Robert
Tera Contributor
I created a catalog client script for onchange script for variable "u_technical_help_sub_category"

function
onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var descriptionValue = g_form.getValue('u_description_html');
    if (descriptionValue) {
        var previousValue = oldValue;
     
        if ((newValue === 'Issue' || newValue === 'Access' || newValue === 'Information Request' || newValue === 'Documentation' || newValue === 'Feature Request')) {
            // Ask for user confirmation before clearing the description
            var userConfirmation = confirm('Selecting the Sub-Category will clear the description. Do you want to proceed?');

            if (userConfirmation) {
                // If the user confirms, set the new subcategory value
                g_form.setValue('u_technical_help_sub_category', newValue);
            } else {
                // If the user cancels, revert back to the old subcategory and restore the description
                g_form.setValue('u_technical_help_sub_category', previousValue);
                g_form.addInfoMessage("Reverted to old value: " + previousValue);
                g_form.setValue('u_description_html', descriptionValue); // Restore the description value
            }
        }
    }
}

But it is not capturing the old value whatever choice before the selection of newValue . It is always capturing the none value in the portal
We need to capture the previous value while cancelling the popup

Regards,
Joyal Robert
9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

@Joyal Robert 

oldValue means the value that was last saved to database and not the value before the most recent change

Here is the workaround

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

    // Use g_scratchpad to store the previous value
    if (typeof g_scratchpad.previousValue === 'undefined') {
        g_scratchpad.previousValue = oldValue;
    }

    var descriptionValue = g_form.getValue('u_description_html');
    if (descriptionValue) {
        var previousValue = g_scratchpad.previousValue;

        if (['Issue', 'Access', 'Information Request', 'Documentation', 'Feature Request'].includes(newValue)) {
            // Ask for user confirmation before clearing the description
            var userConfirmation = confirm('Selecting the Sub-Category will clear the description. Do you want to proceed?');

            if (userConfirmation) {
                // If the user confirms, set the new subcategory value
                g_form.setValue('u_technical_help_sub_category', newValue);
            } else {
                // If the user cancels, revert back to the old subcategory and restore the description
                g_form.setValue('u_technical_help_sub_category', previousValue);
                g_form.addInfoMessage("Reverted to old value: " + previousValue);
                g_form.setValue('u_description_html', descriptionValue); // Restore the description value
            }
        }

        // Update g_scratchpad with the new value
        g_scratchpad.previousValue = newValue;
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

I tried this it is asking twice the dialog box and it capturing the previous value in subcategory sometime it is capturing the none value and also the description template is changing as per the new value

A few tweaks

  • Prevent double trigger
  • Always save something to g_scratchpad, not just when we have a description
  • Clean previousValue
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var descriptionValue = g_form.getValue('u_description_html');
    if (descriptionValue) {
		// If previousValue has been saved, use it. Otherwise, leave it blank.
        var previousValue = g_scratchpad.previousValue ? g_scratchpad.previousValue : "";
		
		// Prevent double triggers
		if(newValue == previousValue)
			return;

        if (['Issue', 'Access', 'Information Request', 'Documentation', 'Feature Request'].includes(newValue)) {
            // Ask for user confirmation before clearing the description
            var userConfirmation = confirm('Selecting the Sub-Category will clear the description. Do you want to proceed?');

            if (userConfirmation) {
                // If the user confirms, set the new subcategory value
                g_form.setValue('u_technical_help_sub_category', newValue);
				
            } else {
				newValue = previousValue; // reverts newValue so the right value is saved to the g_scratchpad
                // If the user cancels, revert back to the old subcategory and restore the description
                g_form.setValue('u_technical_help_sub_category', previousValue);
                g_form.addInfoMessage("Reverted to old value: " + previousValue);
                g_form.setValue('u_description_html', descriptionValue); // Restore the description value
            }
        }
    }
	
	// Update g_scratchpad with the new value, always
	g_scratchpad.previousValue = newValue;
}

 

Joyal
Tera Contributor

@AJ M 

It is picking the oldValue as newValue and template in description is changing