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

Ugh, I forgot to change it back. Originally I was using return gr.active;. I'll change it back and test again.

Thank you!

 

apjohn2
Mega Sage

My colleague found it. I had a typo:

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