We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Help with onLoad modal in catalog client script

dannyprime
Tera Guru

I have a requirement to have a modal on load to present a message to the requester when they load a catalog item.

 

Most times they are likely going to select the positive option and want to proceed, however I need to cover the chance that they don't want to proceed. So I want to take them back to either their previous page or to a set url if they cancel.

 

We are on Zurich and this is on Employee Centre 

 

Below is what I currently have in a catalog client script.

 

Applies to - A Catalog Item

Active is true

UI Type - ALL

Type - onLoad

Applies on Catalog Item view - True

 

function onLoad() {
    //Type appropriate comment here, and begin script below

    if (typeof spModal !== 'undefined') {

        if (g_scratchpad.isFormValid) {
            return true;
        }

        var actionName = g_form.getActionName();

        spModal.open({
            backdrop: 'static',
            keyboard: false,
            message: 'Please select cancel if you want to go back',
            title: 'This is a test',
            buttons: [{
                    label: 'Cancel',
                    cancel: true
                },
                {
                    label: 'Proceed',
                    primary: true
                }
            ]
        }).then(
            function onAgree(result) {
                g_scratchpad.isFormValid = true;
                g_form.submit(actionName);
                return true;
            },
            function onDisagree(reason) {
                setTimeout(function() {
                    window.location.href = 'https://www.testing.com';
                }, 0);
                return false;
            }
        );

        g_scratchpad.isFormValid = false;
        return false;

    } else {
        var gm = new GlideModal();
        gm.setTitle('Error');
        gm.renderWithContent('Something has gone wrong Prompt');
    }
}

 

Would love some help please.

0 REPLIES 0