how to show a pop up with yes or no rather that ok comformation button

raj765_32
Tera Contributor

hi can anyone help me with the code for scenario for 'When selecting Cancel in an Incident, please add a pop up window that asks, "Are you sure you want to cancel this incident?" Give a yes or no option. If yes, proceed with the cancel. If no, then force them to make a new selection.'

 

i have written below code but im not getting yes or no type :

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var value = g_form.getValue('incident_state');
if (value == 15) {
var answer = confirm('Are you sure for cancelling this incident');
if (answer == 'Yes') {
g_form.save();
} else {
g_form.setValue('incident_state', oldValue);
}


}
}

4 REPLIES 4

Aman Kumar S
Kilo Patron

Hi @raj765_32 

Instead of onChange, you should be doing onSubmit client script and add a check for cancel state:

 

var value = g_form.getValue('incident_state');
if (value == 15) {
var answer = confirm('Are you sure for cancelling this incident');
if (!answer) {
return false;
}

Best Regards
Aman Kumar

dgarad
Giga Sage

Hi @raj765_32 

 

Please refer below link 

https://www.servicenow.com/community/now-platform-forum/how-to-create-an-alert-box-with-quot-yes-quo...

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

shyamkumar VK
Kilo Patron

@raj765_32  , Not sure if this is Possible to change this button labels from cancel/No to yes or no  and as Aman Said you need to Design On submit not on change , so when user changes Incident State to Cancel and Save the Record it shows pop up to proceed with Cancel or Stay on the Same state 

 

if you really Want to have the Yes/NO button so you might need to create Custom UI Page for this 

 

Regards,

Shyamkumar 

Please mark this as helpful and accept as a solution if this resolves your Ask.
Regards,

Shyamkumar

Sohithanjan G
Kilo Sage
Kilo Sage

@raj765_32 

 

Your code looks mostly correct, but the issue lies in how you're checking the user's response to the confirmation prompt. The confirm() function returns a boolean value (true if the user clicks "OK" and false if the user clicks "Cancel"), not a string "Yes" or "No".

Here's the corrected version of your code:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    
    var value = g_form.getValue('incident_state');
    if (value == 15) {
        var answer = confirm('Are you sure you want to cancel this incident?');
        if (answer) {
            // If user clicks OK (true)
            g_form.save();
        } else {
            // If user clicks Cancel (false)
            g_form.setValue('incident_state', oldValue);
        }
    }
}

 

 

 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)