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.

GlideAjax returning null Date Time value

MBarrott
Mega Sage

I've created a onChange client script which will ultimately call a GlideAjax script include function. 

 

The end goal is that if someone selects a change request ticket for the reference field in my demand task form, this script will retrieve the planned end date (end_date) from the change_request table and automatically populate the due_date field. 

 

It appears that regardless of what I try to retrieve from the Script Include I'm consistently getting null back from the callback function.

 

CLIENT SCRIPT:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') 
   {
      return;
   }
	
	//Type appropriate comment here, and begin script below
	if(g_form.getDisplayBox('u_change_request').value != '')
	{
		alert('entered if');
		alert('passing over: ' + g_form.getDisplayBox('u_change_request').value);
		var value = g_form.getDisplayBox('u_change_request').value;
		
		var chgAjax = new GlideAjax('ChangeRequestDateFinder');
		chgAjax.addParam('sysparm_name', 'endDateFinder');
		chgAjax.addParam('sysparm_ticket', g_form.getDisplayBox('u_change_request').value);
		chgAjax.getXML(myCallBack);	
	}
}

function myCallBack(response)
{   //the argument 'response' is automatically provided when the callback funciton is called by the system.
	//Dig out the 'answer' attribute, which is what the function returns. 
	alert('entered callback');
	var endDate = response.responseXML.documentElement.getAttribute('answer');  
	alert('endDate value is: ' + endDate);
	//g_form.setValue('due_date', endDate);
}

SCRIPT INCLUDE:

var ChangeRequestDateFinder = Class.create();
ChangeRequestDateFinder.prototype = Object.extendsObject(AbstractAjaxProcessor, 
{
	endDateFinder: function() 
	{
		var ticketNum = this.getParameter('sysparm_ticket');
		var gr = new GlideRecord('change_request');
		gr.addQuery(number, 'CHG20002482');
		gr.query();
		var endDate = gr.getValue('end_date');		
		return endDate;
    }
});

 

1 ACCEPTED SOLUTION

Samiksha Gunjat
Kilo Guru

Hi @MBarrott,

 

Can you try the below scripts :

 

Client Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }

 

    if (g_form.getDisplayBox('u_change_request').value != '') {

        var chgAjax = new GlideAjax('ChangeRequestDateFinder');

        chgAjax.addParam('sysparm_name', 'endDateFinder');

        chgAjax.addParam('sysparm_ticket', g_form.getDisplayBox('u_change_request').value);

        chgAjax.getXML(myCallBack); 

    }

}

 

function myCallBack(response) {

    var endDate = response.responseXML.documentElement.getAttribute('answer');

    if (endDate) {

        g_form.setValue('due_date', endDate);

    } else {

        alert('End date not found.');

    }

}

 

 

Script Include:

 

var ChangeRequestDateFinder = Class.create();

ChangeRequestDateFinder.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    endDateFinder: function() {

        var ticketNum = this.getParameter('sysparm_ticket');

        var gr = new GlideRecord('change_request');

        gr.addQuery('number', ticketNum);

        gr.query();

        if (gr.next()) {

            var endDate = gr.getValue('end_date');

            return endDate;

        } else {

            return null; // or handle case when end date is not found

        }

    }

});

 

Please mark this response as correct or helpful if it assisted you with your question.

Regards,

Samiksha Gunjate

 

 

View solution in original post

5 REPLIES 5

James Chun
Kilo Patron

Hi @MBarrott,

 

Can you try updating the scripts as below ( FYI, I removed the logs/alerts for legibility)

 

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	var chgAjax = new GlideAjax('ChangeRequestDateFinder');
	chgAjax.addParam('sysparm_name', 'endDateFinder');
	chgAjax.addParam('sysparm_ticket', newValue);
	chgAjax.getXML(myCallBack);

}

function myCallBack(response) {   //the argument 'response' is automatically provided when the callback funciton is called by the system.
	var endDate = response.responseXML.documentElement.getAttribute('answer');
	g_form.setValue('due_date', endDate);
}

 

Script Include:

var ChangeRequestDateFinder = Class.create();
ChangeRequestDateFinder.prototype = Object.extendsObject(AbstractAjaxProcessor,
	{
		endDateFinder: function () {
			var ticketNum = this.getParameter('sysparm_ticket');
			var gr = new GlideRecord('change_request');
			gr.get(ticket);

			if (gr.isValidRecord()) {
				var endDate = gr.getValue('end_date');
				return endDate;
			}
		}
	});

You might have to do some data transformation for the endDate but let's see how this goes first.

 

Cheers

Hi @James Chun

 

I was actually able to resolve this myself in a similar way but rather than using a gr.isValidRecord conditional I opted for a while(gr.next()) check which ultimately allowed me to process the endDate value. I may actually opt to use your suggestion in the future though. 

Samiksha Gunjat
Kilo Guru

Hi @MBarrott,

 

Can you try updating the below scripts.

 

Client Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

if (g_form.getDisplayBox('u_change_request').value != '') {
var chgAjax = new GlideAjax('ChangeRequestDateFinder');
chgAjax.addParam('sysparm_name', 'endDateFinder');
chgAjax.addParam('sysparm_ticket', g_form.getDisplayBox('u_change_request').value);
chgAjax.getXML(myCallBack);
}
}

function myCallBack(response) {
var endDate = response.responseXML.documentElement.getAttribute('answer');
if (endDate) {
g_form.setValue('due_date', endDate);
} else {
alert('End date not found.');
}
}

 

Script Include:

var ChangeRequestDateFinder = Class.create();
ChangeRequestDateFinder.prototype = Object.extendsObject(AbstractAjaxProcessor, {
endDateFinder: function() {
var ticketNum = this.getParameter('sysparm_ticket');
var gr = new GlideRecord('change_request');
gr.addQuery('number', ticketNum);
gr.query();
if (gr.next()) {
var endDate = gr.getValue('end_date');
return endDate;
} else {
return null; // or handle case when end date is not found
}
}
});

Samiksha Gunjat
Kilo Guru

Hi @MBarrott,

 

Can you try the below scripts :

 

Client Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }

 

    if (g_form.getDisplayBox('u_change_request').value != '') {

        var chgAjax = new GlideAjax('ChangeRequestDateFinder');

        chgAjax.addParam('sysparm_name', 'endDateFinder');

        chgAjax.addParam('sysparm_ticket', g_form.getDisplayBox('u_change_request').value);

        chgAjax.getXML(myCallBack); 

    }

}

 

function myCallBack(response) {

    var endDate = response.responseXML.documentElement.getAttribute('answer');

    if (endDate) {

        g_form.setValue('due_date', endDate);

    } else {

        alert('End date not found.');

    }

}

 

 

Script Include:

 

var ChangeRequestDateFinder = Class.create();

ChangeRequestDateFinder.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    endDateFinder: function() {

        var ticketNum = this.getParameter('sysparm_ticket');

        var gr = new GlideRecord('change_request');

        gr.addQuery('number', ticketNum);

        gr.query();

        if (gr.next()) {

            var endDate = gr.getValue('end_date');

            return endDate;

        } else {

            return null; // or handle case when end date is not found

        }

    }

});

 

Please mark this response as correct or helpful if it assisted you with your question.

Regards,

Samiksha Gunjate