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.

Script Include always gives me 'false' when looking up caller_id.active value

apjohn2
Mega Sage

Gist/Background: Want to simply add a field message in Incident record, onLoad or onChange, under the "Customer" (caller_id) field, if the State is neither Closed nor Resolved, and if the "Customer" is inactive.

 

To accomplish this, I modified an existing (custom) Script Include called "userDetails", adding a "getActiveState" function. I use that to fetch the user's active value.

 

I then call this Script Include from both onLoad and an onChange client scripts. I'm using alerts to help me troubleshoot since you can't write gs.log statements from client-side scripts.

 

For some reason, the Script Include returns 'false' no matter what the user's active value is. I would really appreciate if someone can review these and let me know what I might be doing wrong. I've included the pertinent Script Include function and one of the two client scripts.

 

Script Include "userDetails":

 

var userDetails = Class.create();
userDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getActiveState: function () {
		
		var gr = new GlideRecord("sys_user");
		gr.addQuerey("sys_id", this.getParameter('sysparam_id'));
		gr.query();
		gr.next();
		return 'false';		
	},
	
	//irrelevant functions removed

    type: 'userDetails'
});

 

 

Client Script "Warn if end-user is inactive (onLoad)":

Table: Incident

 

function onLoad() {
    //This script calls the "userDetails" script include
    var activeState = new GlideAjax('userDetails');
    activeState.addParam('sysparm_name', 'getActiveState'); //[parameter],[function] in the script include to use	
    activeState.addParam('sysparam_id', g_form.getValue('caller_id')); //pass the Customer's sys_id value
    activeState.getXML(getAnswer);
}

function getAnswer(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	alert(answer);
	var incState = g_form.getValue('state');
	if (answer == 'false' && incState != 6 && incState != 7) {
		g_form.showFieldMsg('caller_id','WARNING (OL): The selected Customer is INACTIVE and cannot receive notifications.','error');
	}
}

 

1 ACCEPTED SOLUTION

apjohn2
Mega Sage

My colleague found it. I had a typo:

  gr.addQuerey("sys_id", this.getParameter('sysparam_id'));

View solution in original post

6 REPLIES 6

RaghavSh
Mega Patron

Make below changes in your script include and client remains same:

 

 

getActiveState: function () {
		
		var gr = new GlideRecord("sys_user");
		gr.addQuerey("sys_id", this.getParameter('sysparam_id'));
               gr.addEncodedQuery('active=false');
	      gr.query();
		if(gr.next())
{
		return 'false';	
}
else{
return 'true';
}	
	},

 


Raghav
MVP 2023
LinkedIn

Sagar Pagar
Tera Patron

Hi @apjohn2,

 

Try this updated Script Include -

 

var userDetails = Class.create();
userDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getActiveState: function() {

		var gr = new GlideRecord("sys_user");
		gr.addQuerey("sys_id", this.getParameter('sysparam_id'));
		gr.query();
		if (gr.next()) {
			if (gr.active == "true") {
				return "true";
			} else {
				return "false";
			}
		}
	},

	//irrelevant functions removed

	type: 'userDetails'
});

 

 

Thanks,
Sagar Pagar

The world works with ServiceNow

I plugged that in and it still returns false. Will try the other respondent's suggestion now. Thank you.

Michael Jones -
Giga Sage

In your getActiveState function, you are actually returning false with the statement: 

 

return 'false';

 

You would want something more like this: 

 

   getActiveState: function () {
		
		var gr = new GlideRecord("sys_user");
		gr.get(this.getParameter('sysparam_id'));
                    if(gr.active) {
                      return 'true';
                      }
		return 'false';		
	},

 

(Removed an extra bracket in the code, sorry)

You need to check if the active flag is true and return true, otherwise you return false. 

I hope this helps!

 

 

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!