My script is returning a null value, I don't understand why

Harvey
Tera Contributor

Hello! Currently in the process of learning ServiceNow development, working through a course on Udemy. Here is the script the instructor has. I wrote it line by line, but my script keeps returning "null". Advise? 

 

UI Page:

 

var checkIncident = setInterval(function() {
processRequest();
}, 5000);

function processRequest () {
var ga = new GlideAjax('ServiceNow201GlideAjax');
ga.addParam('system_name', 'getIncidentStatus');
ga.addParam('sysparm_incident_number', 'INC0000003');
ga.getXML(ajaxProcessor);
}

function ajaxProcessor(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
console.log('Status: ' + answer);
}

 

 

Script Include (client callable):

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

getIncidentStatus: function() {
var incidentNumber = this.getParameter('sysparm_incident_number');
if(!gs.nill(incidentNumber)) {
var incidentGR = new GlideRecord('incident');
incidentGR.get('number', incidentNumber);
return incidentGR.state.getDisplayValue();
} else {
return 'No incident was found.';
}

},

sayHello: function() {
return "Hello world!";
},

type: 'ServiceNow201GlideAjax'
});

1 ACCEPTED SOLUTION

Vaibhav127
Tera Guru

Hi,

the parameter is incorrect in line ga.addParam('system_name', 'getIncidentStatus'); it should be sysparm_name

View solution in original post

3 REPLIES 3

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Harvey  

 

I found small spelling mistake in nil().

 

Script Include (client callable):

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

getIncidentStatus: function() {
var incidentNumber = this.getParameter('sysparm_incident_number');
if(!gs.nil(incidentNumber)) {
var incidentGR = new GlideRecord('incident');
incidentGR.get('number', incidentNumber);
return incidentGR.state.getDisplayValue();
} else {
return 'No incident was found.';
}

},

sayHello: function() {
return "Hello world!";
},

 

 

Also I am assuming that you also have Incident with number 'INC0000003' . Else user correct Incident Number based on your instance.

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Vaibhav127
Tera Guru

Hi,

the parameter is incorrect in line ga.addParam('system_name', 'getIncidentStatus'); it should be sysparm_name

DrewW
Mega Sage
Mega Sage

There are a number of possibilities on why you are getting a NULL.  So to troubleshoot I would add a console.log(response) statement to your ajaxProcessor function.  That will dump out the whole object and Firefox and Chrome will allow you to poke thru it to see if you are getting what you are looking for.

 

Then I would add some gs.log() statements to your script include so you can see what its receiving and what your query is returning.  Don't forget to add a source to the log statement so you can find them easily, assuming this is not a scoped app.  Scoped app you will need to use gs.warn, error or info.