Confirm Dialog box in Servicenow

KS86
Tera Contributor

Hi all,

I have one requirement.

I have one field called group on the catalog form. 

We have one MRVS, it contains filed group. Group field will be populated with value based on the Group field on the catalog form. 

 

Requirement -

Select group on the form, add values in MRVS. Then if we try to change the group field , it has to show one Confirm Message("If you change the group, it will clear the all rows in MRVS").

1. If user click "Ok", MRVS rows has to be cleared and Group has to show with new Group Value. (Working as expected now)

2. If user clicks on "Cancel", Group has to show the Old Group only. 
Currently #2 is not working as expected.

 

g_form.setValue('u_group', oldValue); // This is not working. 

Please let me know if you guys have any thoughts on this. Thanks!

 

8 REPLIES 8

assignment_group is name of the MRVS.

 

u_group is the field on the catalog form. So I have written Client Script on Catalog level.

@KS86 

try this

1) create hidden string variable to hold previous value i.e. u_prev_group 

2) then use this script, onchange of that variable

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isTemplate) {
        return;
    }

    if (isLoading) {
        g_form.setValue('u_prev_group', g_form.getValue('u_group') || '');
        window.groupChangeInProgress = false;
        return;
    }

    if (window.groupChangeInProgress) {
        window.groupChangeInProgress = false;
        return;
    }

    newValue = (newValue || '').toString();
    var previousGroup = (g_form.getValue('u_prev_group') || '').toString();
    var mrvsValue = g_form.getValue('assignment_group');
    var hasRows = mrvsValue && mrvsValue !== '[]';

    if (newValue === previousGroup) {
        return;
    }

    if (!hasRows) {
        g_form.setValue('u_prev_group', newValue);
        return;
    }

    if (confirm('If you change the group, it will clear all rows in MRVS.')) {
        g_form.clearValue('assignment_group');
        g_form.setValue('u_prev_group', newValue);
    } else {
        window.groupChangeInProgress = true;
        g_form.setValue('u_group', previousGroup);
    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Tanushree Maiti
Tera Patron

Hi @KS86 

 

Can you try with this:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    var mrvsValue = g_form.getValue('assignment_group'); 
    
    if (mrvsValue && mrvsValue != '[]') 
{
           var confirmChange = confirm("If you change the group, it will clear all rows in the MRVS.");
          if (confirmChange) 
{
               g_form.setValue('assignment_group', '[]');
        } else {
              g_form.setValue('u_group', oldValue);
        }
    }
}
 
Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Venky Kshatriy2
Mega Sage

Hi @KS86 

For this requirement, g_form.setValue('u_group', oldValue) alone usually does not work properly in a catalog onChange, because:

  1. setValue() triggers the onChange again
  2. The field has already changed in UI
  3. For catalog variables, reverting immediately sometimes fails unless you use a flag + small timeou

 Try this script

var isRevertingGroup = false;
var setTimer = '';

 

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

 

    if (isRevertingGroup) {
        isRevertingGroup = false;
        return;
    }

 

    var groupVar = 'u_group';
    var mrvsVar = 'u_mrvs';

 

    var mrvsValue = g_form.getValue(mrvsVar);
    var hasRows = false;

 

    try {
        hasRows = JSON.parse(mrvsValue || '[]').length > 0;
    } catch (e) {
        hasRows = mrvsValue && mrvsValue !== '' && mrvsValue !== '[]';
    }

 

    if (!hasRows)
        return;

 

    if (confirm("If you change the group, it will clear all rows in MRVS.")) {
        g_form.setValue(mrvsVar, '[]');
    } else {
        isRevertingGroup = true;
        clearTimeout(setTimer);
        setTimer = setTimeout(function() {
            g_form.setValue(groupVar, oldValue);
        }, 200);
    }
}

 


If this response addressed your question, please mark it as Helpful and accept it as the solution.