Convert Client script to Glide Ajax

Maxwell3
Kilo Guru

Hello all,

I am trying to convert this client script into a Glide Ajax call. This is located on this incident table and the incident form is taking a long time to load when updating the fields. I think a Glide Ajax call to a Script Include might help.

Here is the Client Script below.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading)
return;

//make work notes mandatory as appropriate
if(newValue == oldValue)
g_form.setMandatory('work_notes', false);
else
g_form.setMandatory('work_notes', true);

//clear dependent values
g_form.setValue('assigned_to', null);
g_form.setValue('u_field3', null);
g_form.setValue('u_field2', null);
g_form.setValue('u_field1', null);

//repopulate dependent values
var group = g_form.getReference('assignment_group', setQueue);
}

function setQueue(group) {
if (group)
{
g_form.setValue('u_field1', group.u_field1);
g_form.setValue('u_field2', group.u_field2);
g_form.setValue('u_field3', group.u_field3);
}
}

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Maxwell,

That's correct using GlideAjax is better than using getReference with callback in terms of performance

 

Script Include: it should be client callable

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	checkRecordPresent: function(){
                var obj = {};
		        var group = this.getParameter('sysparm_group');	
                var groupRec = new GlideRecord('sys_user_group');
                groupRec.addQuery('sys_id', group);
                groupRec.query();
                if(groupRec.next()){
                   obj['u_field1'] = groupRec.u_field1;
                   obj['u_field2'] = groupRec.u_field2;
                   obj['u_field3'] = groupRec.u_field3;
                }		
		return JSON.stringify(obj);
	},
	
    type: 'checkRecords'
});

Client Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading)
		return;

	//make work notes mandatory as appropriate
	if(newValue == oldValue)
		g_form.setMandatory('work_notes', false);
	else
		g_form.setMandatory('work_notes', true);

	//clear dependent values
	g_form.setValue('assigned_to', null);
	g_form.setValue('u_field3', null);
	g_form.setValue('u_field2', null);
	g_form.setValue('u_field1', null);

	var ga = new GlideAjax('checkRecords');
	ga.addParam('sysparm_name', "checkRecordPresent");
	ga.addParam('sysparm_group', g_form.getValue('assignment_group'));
	ga.getXMLAnswer(function(answer){
		if(answer != ''){
			var answerObj = JSON.parse(answer);
			g_form.setValue('u_field1', answerObj.u_field1);
			g_form.setValue('u_field2', answerObj.u_field2);
			g_form.setValue('u_field3', answerObj.u_field3);
		}
	});
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

17 REPLIES 17

sachin_namjoshi
Kilo Patron
Kilo Patron

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Maxwell,

That's correct using GlideAjax is better than using getReference with callback in terms of performance

 

Script Include: it should be client callable

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	checkRecordPresent: function(){
                var obj = {};
		        var group = this.getParameter('sysparm_group');	
                var groupRec = new GlideRecord('sys_user_group');
                groupRec.addQuery('sys_id', group);
                groupRec.query();
                if(groupRec.next()){
                   obj['u_field1'] = groupRec.u_field1;
                   obj['u_field2'] = groupRec.u_field2;
                   obj['u_field3'] = groupRec.u_field3;
                }		
		return JSON.stringify(obj);
	},
	
    type: 'checkRecords'
});

Client Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading)
		return;

	//make work notes mandatory as appropriate
	if(newValue == oldValue)
		g_form.setMandatory('work_notes', false);
	else
		g_form.setMandatory('work_notes', true);

	//clear dependent values
	g_form.setValue('assigned_to', null);
	g_form.setValue('u_field3', null);
	g_form.setValue('u_field2', null);
	g_form.setValue('u_field1', null);

	var ga = new GlideAjax('checkRecords');
	ga.addParam('sysparm_name', "checkRecordPresent");
	ga.addParam('sysparm_group', g_form.getValue('assignment_group'));
	ga.getXMLAnswer(function(answer){
		if(answer != ''){
			var answerObj = JSON.parse(answer);
			g_form.setValue('u_field1', answerObj.u_field1);
			g_form.setValue('u_field2', answerObj.u_field2);
			g_form.setValue('u_field3', answerObj.u_field3);
		}
	});
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello Ankur,

 

Thank you for your helpful reply.

I have verified that the ajax call is returning an answer, but the fields on the form are not getting populated.

In the Query I used ('sys_id', name) instead of    ('sys_id', group) as you have in your example. Should I change the query or do you think the issue is located somewhere else?

Hi Maxwell,

Query should be ('sys_id', group). As group variable is storing the Assignment group value being passed from client script. 

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad