- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 08:57 AM
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;
}
});
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 11:10 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 12:22 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 01:36 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 09:39 PM
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
}
}
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 11:10 PM
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