Issue in Configuration of spModal

Jeck Manalo
Tera Guru

Hello Everyone

I have a code where I use an spModal.

 

If I click "Approve" the value will be Set to Approve in the variable field.

If I click "Reject" the value will be Set to Rejected in the variable field.

 

I am having an issue that when I click the "X" in spModal or click outside, I still getting a value "Rejected" my goal is that this should not pass this value and stays the value into "Pending".

 

 

function onLoad() {
    var status = g_form.getValue('user_testing');
 
     if (status == 'Pending') {
         spModal.open({
             title: 'Do you want to approve or reject?',
             message: 'Please confirm your decision for this request.',
             buttons: [{
                     label: 'Approve',
                     primary: true
                 },
                 {
                     label: 'Reject',
                     primary: true
                 },
             ]
         }).then(function() {
             g_form.setValue('user_testing', 'Approved');
             g_form.save();
         }, function() {
             g_form.setValue('user_testing', 'Rejected');
             g_form.save();
         });
     }
 }
1 ACCEPTED SOLUTION

Shruti D
Kilo Sage

Hello @Jeck Manalo,

Update your code like below

function onLoad() {
    var status = g_form.getValue('user_testing');

    if (status == 'Pending') {
        spModal.open({
            title: 'Do you want to approve or reject?',
            message: 'Please confirm your decision for this request.',
            buttons: [
                {
                    label: 'Approve',
                    value: 'approved',  // replace backend value
                    primary: true
                },
                {
                    label: 'Reject',
                    value: 'rejected',  // replace backend value
                    primary: false
                }
            ]
        }).then(function(result) {
            if (result) {
                g_form.setValue('user_testing', result);
                g_form.save();
            }
            // No action if modal dismissed
        });
    }
}


Please Mark Correct ✔️
 if this solves your query and also mark Helpful 👍 if you find my response worthy based on the impact.

Regards,
Shruti

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@Jeck Manalo 

where are you using this modal?

on catalog form?

share some screenshots

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
Tera Patron
Tera Patron

@Jeck Manalo 

try this

function onLoad() {
    var status = g_form.getValue('user_testing');

    if (status == 'Pending') {
        spModal.open({
            title: 'Do you want to approve or reject?',
            message: 'Please confirm your decision for this request.',
            buttons: [
                { label: 'Approve', primary: true },
                { label: 'Reject', primary: true }
            ]
        }).then(function(response){
            // Only act on explicit button clicks, not modal dismiss
            if (typeof response === 'undefined') {
                // Modal was closed (X, outside, ESC) — do nothing
                return;
            }
            if (response.button.label === 'Approve') {
                g_form.setValue('user_testing', 'Approved');
                g_form.save();
            } else if (response.button.label === 'Reject') {
                g_form.setValue('user_testing', 'Rejected');
                g_form.save();
            }
        });
    }
}

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

I tried this code but not work.

 

JeckManalo_0-1758094100105.png