Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Dialog box is not prompted when UI action is clicked.

Rocky5
Kilo Sage

Hello Experts,

 

I am using below script in UI Action to show a pop up confirmation with different verbiage based on the current state. but when clicked on button no action is being taking place.

Script:

function onClick() {
    var myActionCallbackOK = function() {
        submitNow();
    };
    // Definition of the callback function for Cancel button
    var myActionCallbackCancel = function() {
        cancelNow();
    };

    if (current.state == '2') {
        var dlgBody = 'Are you sure you want to approve the Task?';
        var dlgClass = typeof GlideModal != 'undefined' ? GlideModal : GlideDialogWindow;
        var dlg = new dlgClass('glide_confirm_standard');
        dlg.setPreference('title', dlgBody);
        dlg.setPreference('onPromptComplete', myActionCallbackOK.bind(this));
        dlg.setPreference('onPromptCancel', myActionCallbackCancel.bind(this));
        dlg.render();
    } else {
        var pdlgBody = 'PR will be removed. Are you sure you want to approve the ABR Task?';
        var pdlgClass = typeof GlideModal != 'undefined' ? GlideModal : GlideDialogWindow;
        var pdlg = new pdlgClass('glide_confirm_standard');
        pdlg.setPreference('title', pdlgBody);
        pdlg.setPreference('onPromptComplete', myActionCallbackOK.bind(this));
        pdlg.setPreference('onPromptCancel', myActionCallbackCancel.bind(this));
        pdlg.render();
    }

    function submitNow() {
        g_form.setValue('state', '6');
        g_form.save();
    }
}
function cancelNow() {
    // Closes the window by default, can be empty
}
 
 
Any input on where I am going wrong?
 
Thanks,
Rocky.
3 REPLIES 3

Community Alums
Not applicable

Hi,

Can you try with the below code and test again.

 

function onClick() {
// Definition of the callback function for OK button
var myActionCallbackOK = function() {
submitNow();
};
// Definition of the callback function for Cancel button
var myActionCallbackCancel = function() {
cancelNow();
};

var dlgBody = 'Are you sure you want to approve the Task?';
if (current.state == '2') {
dlgBody = 'Are you sure you want to approve the Task?';
} else {
dlgBody = 'PR will be removed. Are you sure you want to approve the ABR Task?';
}

var dlgClass = window.GlideModal || GlideDialogWindow;
var dlg = new dlgClass('glide_confirm_standard');
dlg.setTitle(dlgBody);
dlg.setPreference('onPromptComplete', myActionCallbackOK);
dlg.setPreference('onPromptCancel', myActionCallbackCancel);
dlg.render();

function submitNow() {
g_form.setValue('state', '6');
g_form.submit();
}

function cancelNow() {
// Closes the window by default, can be empty
}
}

 

Please mark this helpful if it works.

 

Hello,

I tried it, no luck. any other input?

 

Thanks,

Rocky.

Ankur Bawiskar
Tera Patron
Tera Patron

@Rocky5 

your UI action is client side; so current won't work

please use g_form.getValue() to get the state value and then compare

function onClick() {
    var myActionCallbackOK = function() {
        submitNow();
    };
    // Definition of the callback function for Cancel button
    var myActionCallbackCancel = function() {
        cancelNow();
    };

    if (g_form.getValue('state').toString() == '2') {
        var dlgBody = 'Are you sure you want to approve the Task?';
        var dlgClass = typeof GlideModal != 'undefined' ? GlideModal : GlideDialogWindow;
        var dlg = new dlgClass('glide_confirm_standard');
        dlg.setPreference('title', dlgBody);
        dlg.setPreference('onPromptComplete', myActionCallbackOK.bind(this));
        dlg.setPreference('onPromptCancel', myActionCallbackCancel.bind(this));
        dlg.render();
    } else {
        var pdlgBody = 'PR will be removed. Are you sure you want to approve the ABR Task?';
        var pdlgClass = typeof GlideModal != 'undefined' ? GlideModal : GlideDialogWindow;
        var pdlg = new pdlgClass('glide_confirm_standard');
        pdlg.setPreference('title', pdlgBody);
        pdlg.setPreference('onPromptComplete', myActionCallbackOK.bind(this));
        pdlg.setPreference('onPromptCancel', myActionCallbackCancel.bind(this));
        pdlg.render();
    }

    function submitNow() {
        g_form.setValue('state', '6');
        g_form.save();
    }
}

function cancelNow() {
    // Closes the window by default, can be empty
}

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