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

Hi @Samiksha Gunjat 

 

Thank you for this. I had actually fixed the script myself eventually but your solution was near identical to what I ended up doing. Accepted as solution.