How do I replace g_form.getReference() to Glide Ajax client script

Bindhu1
Tera Contributor

Hi,

I need to replace g_form.getReference() by Glide Ajax for the below code , how do i do that.?

Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
//Label Value & Field Value
var requestedforLabel = $('label.sc_request.requested_for');
var requestedforField = $('sys_display.sc_request.requested_for');
if (!requestedforLabel || !requestedforField)
return;
if (!newValue) {
requestedforLabel.setStyle({backgroundImage: ""});
requestedforField.setStyle({color: ""});
return;
}
g_form.getReference('requested_for', vipRequestedforCallback); // need to replace this function
}
//callback function
function vipRequestedforCallback(Requestedfor) {
var requestedforLabel = $('label.sc_request.requested_for').down('label');
var requestedforField = g_form.getElement('sys_display.sc_request.requested_for');
if (!requestedforLabel || !requestedforField)
return;
//checking for VIP Status
if (Requestedfor.vip == 'true') {
var bgPosition = "95% 55%";
if (document.documentElement.getAttribute('data-doctype') == 'true')
bgPosition = "5% 45%";
//highlighting the field
g_form.showFieldMsg('requested_for',"VIP Special Handling",'info');
requestedforLabel.setStyle({backgroundImage: "url(images/icons/vip.gif)", backgroundRepeat: "no-repeat", backgroundPosition: bgPosition, paddingLeft: '30px' });
requestedforField.setStyle({color:"red"});
}
else {
//removing special handler
requestedforLabel.setStyle({backgroundImage: ""});
requestedforField.setStyle({color: ""});
}
}

 

 

Thanks ,

brunda

1 ACCEPTED SOLUTION

@brunda 

Create Script include which is client callable

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

	checkRecordPresent: function(){

		var id = this.getParameter('sysparm_userID');			
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', id);
		gr.query();
		if(gr.next()){
			return gr.vip.toString();
		}
	},

	type: 'checkRecords'
});

find_real_file.png

Client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	//Label Value & Field Value
	var requestedforLabel = $('label.sc_request.requested_for');
	var requestedforField = $('sys_display.sc_request.requested_for');
	if (!requestedforLabel || !requestedforField)
		return;
	if (!newValue) {
		requestedforLabel.setStyle({backgroundImage: ""});
		requestedforField.setStyle({color: ""});
		return;
	}
	// need to replace this function

	var ga = new GlideAjax('checkRecords');
	ga.addParam('sysparm_name', "checkRecordPresent");
	ga.addParam('sysparm_userID', g_form.getValue('requested_for'));
	ga.getXMLAnswer(function(answer){
		var requestedforLabel = $('label.sc_request.requested_for').down('label');
		var requestedforField = g_form.getElement('sys_display.sc_request.requested_for');
		if (!requestedforLabel || !requestedforField)
			return;
		//checking for VIP Status
		if (answer == 'true') {
			var bgPosition = "95% 55%";
			if (document.documentElement.getAttribute('data-doctype') == 'true')
				bgPosition = "5% 45%";
			//highlighting the field
			g_form.showFieldMsg('requested_for',"VIP Special Handling",'info');
			requestedforLabel.setStyle({backgroundImage: "url(images/icons/vip.gif)", backgroundRepeat: "no-repeat", backgroundPosition: bgPosition, paddingLeft: '30px' });
			requestedforField.setStyle({color:"red"});
		}
		else {
			//removing special handler
			requestedforLabel.setStyle({backgroundImage: ""});
			requestedforField.setStyle({color: ""});
		}
	});
}

Regards
Ankur

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@brunda 

Is that a OOB client script.

if yes then why to update that?

So did you start on the GlideAjax? where are you stuck?

Regards
Ankur

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

@brunda 

Create Script include which is client callable

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

	checkRecordPresent: function(){

		var id = this.getParameter('sysparm_userID');			
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', id);
		gr.query();
		if(gr.next()){
			return gr.vip.toString();
		}
	},

	type: 'checkRecords'
});

find_real_file.png

Client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	//Label Value & Field Value
	var requestedforLabel = $('label.sc_request.requested_for');
	var requestedforField = $('sys_display.sc_request.requested_for');
	if (!requestedforLabel || !requestedforField)
		return;
	if (!newValue) {
		requestedforLabel.setStyle({backgroundImage: ""});
		requestedforField.setStyle({color: ""});
		return;
	}
	// need to replace this function

	var ga = new GlideAjax('checkRecords');
	ga.addParam('sysparm_name', "checkRecordPresent");
	ga.addParam('sysparm_userID', g_form.getValue('requested_for'));
	ga.getXMLAnswer(function(answer){
		var requestedforLabel = $('label.sc_request.requested_for').down('label');
		var requestedforField = g_form.getElement('sys_display.sc_request.requested_for');
		if (!requestedforLabel || !requestedforField)
			return;
		//checking for VIP Status
		if (answer == 'true') {
			var bgPosition = "95% 55%";
			if (document.documentElement.getAttribute('data-doctype') == 'true')
				bgPosition = "5% 45%";
			//highlighting the field
			g_form.showFieldMsg('requested_for',"VIP Special Handling",'info');
			requestedforLabel.setStyle({backgroundImage: "url(images/icons/vip.gif)", backgroundRepeat: "no-repeat", backgroundPosition: bgPosition, paddingLeft: '30px' });
			requestedforField.setStyle({color:"red"});
		}
		else {
			//removing special handler
			requestedforLabel.setStyle({backgroundImage: ""});
			requestedforField.setStyle({color: ""});
		}
	});
}

Regards
Ankur

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