Create a popup message upon login

jcpasia
Kilo Contributor

Hi,

I would like to know if anyone know the step by step procedure on how I can create a login popup message from scratch?

I wanted the users to agree to our "Terms and Conditions" upon logging in the instance and store their responses in ServiceNow. 

 

I found some articles from SNGuru although it requires downloading a dialog which is no longer available.

 

Thank you in advance for the feedback!

1 ACCEPTED SOLUTION

Willem
Giga Sage
Giga Sage

You can save the result either in a table or for example as a user preference. I modified the ServiceNow Guru script below:

function termsOK() {
    //Gets called if the 'OK' dialog button is clicked
    //Make sure terms have been accepted
    var terms = gel('accept_terms').value;
    if (terms != 'true') {
        //If terms are false stop submission
        alert('Please accept the terms and conditions to continue your order.');
        return false;
    }
    //If accept checkbox is true do this...
    GlideDialogWindow.get().destroy(); //Close the dialog window
    if (!getPreference("user_accepted")) {
        setPreference("user_accepted", "true")
    }
}

function termsCancel() {
    //Redirect gets called if the 'Cancel' dialog button is clicked
    if ($('cancel_url').value != 'null') {
        window.location = $('cancel_url').value;
    }
    else {
        window.location = 'home.do'; //Redirect to default homepage
    }
}

View solution in original post

18 REPLIES 18

jcpasia
Kilo Contributor

Thank you @Willem !

 

Hoping you can provide the part where it stores the responses to a custom table.

 

And prevent it from showing again once the user has agreed to the terms already.

Hi @jcpasia 

You can add a function to the sessionUtil script include. Something like the 'saveResult' function below. You can add whatever fields and values you want to set of course:

var sessionUtil = Class.create();
sessionUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	handleSession: function(){
		var session = gs.getSession();
		if(session.getClientData('popup_triggered')){
			return false;
		}
		session.putClientData('popup_triggered', true);
		return true;
	},

	saveResult: function(){
		var grCustom = new GlideRecord('custom table');
		grCustom.addQuery('field', true);
		grCustom.addQuery('user_field', gs.getUserID());
		grCustom.query();
		if (!grCustom.hasNext()) {
			grCustom.initialize();
			grCustom.field = true;
			grCustom.user_field = gs.getUserID();
			grCustom.insert();

		}
	},
	type: 'sessionUtil'
});

 

In the Client Script of the UI Page you can call the function to save the result:

function termsOK() {
    //Gets called if the 'OK' dialog button is clicked
    //Make sure terms have been accepted
    var terms = gel('accept_terms').value;
    if (terms != 'true') {
        //If terms are false stop submission
        alert('Please accept the terms and conditions to continue your order.');
        return false;
    }
    //If accept checkbox is true do this...
    GlideDialogWindow.get().destroy(); //Close the dialog window
    var ga = new GlideAjax('global.sessionUtil');
    ga.addParam('sysparm_name', 'saveResult');
    ga.getXML(function (serverResponse) {
        var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
        console.log(answer);
    });
}

jcpasia
Kilo Contributor

Hi @Willem !

 

It is now working on my end, I just wanted to know if we can prevent the users from receiving the dialog box again on their next log in if they already agreed to the terms?

Hi,

 

Then you need to query your custom table in the Script include 'handleSession' as well. The Script Include will look like this:

var sessionUtil = Class.create();
sessionUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    handleSession: function () {
        var session = gs.getSession();
        if (session.getClientData('popup_triggered')) {
            return false;
        }
        session.putClientData('popup_triggered', true);
        var grCustom = new GlideRecord('custom table');
        grCustom.addQuery('field', true);
        grCustom.addQuery('user_field', gs.getUserID());
        grCustom.query();
        if (grCustom.hasNext()) {
            return false;
        }
        return true;
    },

    saveResult: function () {
        var grCustom = new GlideRecord('custom table');
        grCustom.addQuery('field', true);
        grCustom.addQuery('user_field', gs.getUserID());
        grCustom.query();
        if (!grCustom.hasNext()) {
            grCustom.initialize();
            grCustom.field = true;
            grCustom.user_field = gs.getUserID();
            grCustom.insert();

        }
    },
    type: 'sessionUtil'
});

jcpasia
Kilo Contributor

Thank you @Willem !

 

I noticed that the remove dialog close icon is not working in the script..

 

var showTerms = function () {
    var dialog = new GlideDialogWindow('terms_and_conditions_dialog'); //Render the dialog containing the UI Page 'terms_and_conditions_dialog'
    dialog.setTitle('ServiceNow Acceptable Use Policy'); //Set the dialog title
    dialog.setSize(650, 600); //Set the dialog size
    dialog.removeCloseDecoration(); //Remove the dialog close icon
    dialog.setPreference('cancel_url', 'logout.do'); //Set optional cancel redirect URL
    dialog.render(); //Open the dialog
}