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.

Popup using catalog client script with user interaction

Community Alums
Not applicable

Hi,

I need to create an onLoad catalog client script on my catalog item that will pop up a message:

"do you want to continue?"

and under will be two buttons:

"Yes" and "No"

If the user will click on 'Yes' the pop up will be closed and he will stay at the same page.

If he will click 'No' then he needs to redirect to Service Portal homepage

1 ACCEPTED SOLUTION

M Iftikhar
Tera Sage

Hi @Community Alums,

 

To achieve this, follow these steps:

  • Create a Catalog Client Script on your catalog item.
  • Set the Type to onLoad.
  • In the script field, enter:
function onLoad() {
    // Use SPModal in Service Portal
    if (typeof spModal !== 'undefined') {
        spModal.open({
            title: "Confirmation",
            message: "Do you want to continue?",
            buttons: [
                { label: 'Yes', value: true, primary: true },
                { label: 'No', value: false }
            ]
        }).then(function(response) {
            if (response.value === false) {
                // Redirect to Service Portal home
                top.window.location = '/sp';
            }
        });
    } else {
        // Fallback for classic UI (not SP)
        var answer = confirm("Do you want to continue?");
        if (!answer) {
            top.window.location = "home.do";
        }
    }
}

On loading the Catalog Item on Service Portal:

MIftikhar_0-1763574039589.png

When click No it redirect to Service Portal homepage:

MIftikhar_1-1763574371083.png

Similarly, on loading the Catalog Item on Classic UI:

 

MIftikhar_5-1763574559693.png

When click No it redirect to ServiceNow Platform homepage:

MIftikhar_4-1763574518281.png

 

If my response helped, please mark it as the accepted solution so others can benefit as well.

 

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

View solution in original post

6 REPLIES 6

M Iftikhar
Tera Sage

Hi @Community Alums,

 

To achieve this, follow these steps:

  • Create a Catalog Client Script on your catalog item.
  • Set the Type to onLoad.
  • In the script field, enter:
function onLoad() {
    // Use SPModal in Service Portal
    if (typeof spModal !== 'undefined') {
        spModal.open({
            title: "Confirmation",
            message: "Do you want to continue?",
            buttons: [
                { label: 'Yes', value: true, primary: true },
                { label: 'No', value: false }
            ]
        }).then(function(response) {
            if (response.value === false) {
                // Redirect to Service Portal home
                top.window.location = '/sp';
            }
        });
    } else {
        // Fallback for classic UI (not SP)
        var answer = confirm("Do you want to continue?");
        if (!answer) {
            top.window.location = "home.do";
        }
    }
}

On loading the Catalog Item on Service Portal:

MIftikhar_0-1763574039589.png

When click No it redirect to Service Portal homepage:

MIftikhar_1-1763574371083.png

Similarly, on loading the Catalog Item on Classic UI:

 

MIftikhar_5-1763574559693.png

When click No it redirect to ServiceNow Platform homepage:

MIftikhar_4-1763574518281.png

 

If my response helped, please mark it as the accepted solution so others can benefit as well.

 

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

Hi @Community Alums,

 

If this solution helped, please consider marking it as accepted solution to support the community.

 

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

Community Alums
Not applicable

@M Iftikhar is there any way that it wont ask twice if u want to leave without saving the changes?

because after you click that you want to leave it asks again if u want to leave without saving the changes

 

agrod_0-1764593122830.png

 

Community Alums
Not applicable

@M Iftikhar hi but if you click No it asks again if you want to leave without the changes. is it possible that it will not asks twice?