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.

Error Messages in Service Portal Record Producer

Robbie Lacivita
Tera Guru

I have adapted the Pending User Registrations plugin for our needs to have new users register with our instance.

I have a business rule that runs to validate if a user already exists in the sys_user table, or if they exist in the user_registration_request table. If they already exist, it aborts the insert and displays an error message. This all works as intended. I've pasted the code below.

However, when a user submits this request on the record producer I created on the service portal, the error message flashes too quickly to read before going away. Is there a way to make this more prominent for the users on the portal so they understand their request failed and why?

/*
 * Make sure we aren't creating duplicate registrations or user records
 * Service-now.com
 */
validateRegistration();

function validateRegistration() {

    //abort if user already exists
    if (userExists(current.u_edipi)) {
        gs.addErrorMessage(gs.getMessage("A user account for '{0}' already exists in the system",current.u_edipi));
        current.setAbortAction(true);
        return;
    }
    
    //abort if registration already exists
    if (registrationExists(current.u_edipi)) {
        gs.addErrorMessage(gs.getMessage("A registration for '{0}' has already been requested and is in a pending state",current.u_edipi));
        current.setAbortAction(true);
        return;
    }
}

function registrationExists(edipi) {
    var reg = new GlideRecord("user_registration_request");
    reg.addQuery("u_edipi", edipi);
    reg.addQuery("state", "pending");
    reg.query();
      gs.print("registrationExists query: " + reg.getEncodedQuery() + " = " + reg.getRowCount());
       return reg.hasNext();
}

function userExists(edipi) {


    var usr = new GlideRecord("sys_user");
    usr.addQuery("u_edipi", edipi);
    usr.query();
   gs.print("userExists query: " + usr.getEncodedQuery() + " = " + usr.getRowCount());
    return usr.hasNext();
}

Thanks,

 

Robbie

2 REPLIES 2

Brian Lancaster
Kilo Patron

You may want to talk a look at this post.

Robbie Lacivita
Tera Guru

I tried to do this in a different way using a script include/client script, but it doesn't do the validation. I'm not sure what I'm missing:

Client Script:

onSubmit

function onSubmit() {
	//Type appropriate comment here, and begin script below
	
	var edipi = g_form.getValue('u_edipi');
		
	var ga = new GlideAjax('ValidateEDIPI');
	ga.addParam('sysparm_name', 'initialize');
	ga.addParam('sysparm_edipi', edipi);
	ga.getXML(validate);
	
	function validate(response) {
		var answer = response.responseXML.documentElement.getAttribute('answer');
		if(answer == 'true'){
			var msg = confirm('A user with this EDIPI already exists. Please enter a unique EDIPI.');
			return false;
		}
		else{
			return true;
		}
	}
}

 

Script Include:

Client callable = true

var ValidateEDIPI = Class.create();
ValidateEDIPI.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	initialize: function() {
		
		var edipi = this.getParameter('sysparm_edipi');
		
		var reg = new GlideRecord("user_registration_request");
		reg.addQuery("u_edipi", edipi);
		reg.addQuery("state", "pending");
		reg.query();
		
		var usr = new GlideRecord("sys_user");
		usr.addQuery("u_edipi", edipi);
		usr.query();
		
		if(reg.next() || usr.next()){
			return 'true';
		}
		else return 'false';
	}
	
	
});

 

Thanks,

 

Robbie