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.

spModal with buttons: How to define functionality when a button is clicked?

Smith Johnson
Kilo Sage

Hello,

I have an spModal with two buttons. I would like to display a message to the user when the "Agree" button is selected, and a different message when the "Disagree" button is selected. In addition, if the user closes the modal by clicking the "X" icon, no message should appear. How to make this work?

spModal.open({
		"title" : "Terms&Conditions",
		"message" : message,
		"buttons" : [
			{label: "Agree" , primary: true},
			{label: "Disagree" , cancel: true}
		],
		"backdrop" : "static",
		"keyboard": false
})


Thanks,
Smith.

 

1 ACCEPTED SOLUTION

@Smith Johnson 

this worked for me, enhance further

spModal.open({
        title: 'Custom Action Required',
        message: 'Please choose an action:',
        buttons: [{
            label: 'Agree',
            value: 'agree',
            primary: true // Makes this button visually prominent
        }, {
            label: 'Disagree',
            value: 'disagree'
        }]
    }).then(function(result) {
        // This function executes when a button is clicked
        if (!result.primary) {
            alert('You clicked disagree!');
        } else {
            alert('You clicked agree!');
        }
    });

Output:

sp modal open with custom buttons.gif

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@Smith Johnson 

try this

spModal.open({
    "title": "Terms & Conditions",
    "message": message,
    "buttons": [
        { label: "Agree", primary: true },
        { label: "Disagree", cancel: true }
    ],
    "backdrop": "static",
    "keyboard": false,
    "callback": function(result) {
        if (result === "Agree") {
            // Show message for Agree
            alert("You agreed to the terms.");
        } else if (result === "Disagree") {
            // Show message for Disagree
            alert("You disagreed to the terms.");
        }
        // No message if modal is closed with "X" (result will be undefined or null)
    }
});

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello @Ankur Bawiskar ,

unless I miss something, this didnt work. I also put some log statements, but I don't see anything in the console.

spModal.open({
        "title": "Terms & Conditions",
        "message": "message",
        "buttons": [{
                label: "Agree",
                primary: true
            },
            {
                label: "Disagree",
                cancel: true
            }
        ],
        "backdrop": "static",
        "keyboard": false,
        "callback": function(result) {
            if (result === "Agree") {
				console.log("triggered");
                // Show message for Agree
                alert("You agreed to the terms.");
            } else if (result === "Disagree") {
                // Show message for Disagree
				console.log("triggered 2");
                alert("You disagreed to the terms.");
            }
			console.log("triggered 3");
            // No message if modal is closed with "X" (result will be undefined or null)
        }
});

 

@Smith Johnson 

this worked for me, enhance further

spModal.open({
        title: 'Custom Action Required',
        message: 'Please choose an action:',
        buttons: [{
            label: 'Agree',
            value: 'agree',
            primary: true // Makes this button visually prominent
        }, {
            label: 'Disagree',
            value: 'disagree'
        }]
    }).then(function(result) {
        // This function executes when a button is clicked
        if (!result.primary) {
            alert('You clicked disagree!');
        } else {
            alert('You clicked agree!');
        }
    });

Output:

sp modal open with custom buttons.gif

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader