Not able pickup oldValue for confirm dialog box
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2024 07:41 AM
function onChange(control, oldValue, newValue, isLoading) {
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2024 07:54 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2024 08:05 AM - edited 12-10-2024 08:23 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2024 09:08 AM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2024 09:07 PM
@AJ M
It is picking the oldValue as newValue and template in description is changing