Get user location and it region with script include call through the client script

Siva82
Tera Expert

Hi,

Get user location and it region with script include call through the client script,

I wrote the both script include and client script, but didn't get results, Its getting null alert.  Can u please check my code and attachement

script include:

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

getDetails: function() {

//Get the sys_ID for the call

//pass the input
var callerSysID = this.getParameter('sysparm_callerSysID');

//glide the user table for fetching the records
var gr = new GlideRecord('sys_user');
gr.get(callerSysID);


var mgr = gr.getValue('u_emploment_counrty.u_it_region');
var loc = gr.getValue('location');

var arr = []; // define the array
arr[0] = mgr; //set the manager in the array
arr[1] = loc; //set the location in the array

return JSON.stringify(arr);
},

type: 'reqtest'
});

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

 

client script:

var callerSysID = g_form.getValue('caller_id'); //push the sysID of the caller record

var ga = new GlideAjax('reqtest'); //call the script include
ga.addParam('sysparm_name','getDetails'); // call the function of include
ga.addParam('sysparm_callerSysID', callerSysID); //pass the fetched call sys_id in the include
ga.getXML(handleResponse);

function handleResponse(response){

var answer = response.responseXML.documentElement.getAttribute("answer"); //fetch the response from script include

var answers = answer.evalJSON(); //evaluate the response and decode it
alert(answer);
g_form.setValue('u_region',answers[0]); //set the short description
g_form.setValue('location',answers[1]); //set te description

}


}

 

find_real_file.png

Thank you

4 ACCEPTED SOLUTIONS

Hi,

you did not copy script include correctly

remove everything from script and add this again and save

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

	getDetails: function() {

		var callerSysID = this.getParameter('sysparm_callerSysID');
		var gr = new GlideRecord('sys_user');
		gr.get(callerSysID);

		var mgr = gr.u_emploment_counrty.u_it_region.toString();
		var loc = gr.getValue('location');

		var obj = {}; // define the array
		obj["mgr"] = mgr; //set the manager in the array
		obj["loc"] = loc; //set the location in the array

		return JSON.stringify(obj);
	},

	type: 'reqtest'
});

Regards
Ankur

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

View solution in original post

@Siva

Hope you are doing good.

Did my reply answer your question?

If so, please mark appropriate response as correct & helpful so that the question will appear as resolved for others who may have a similar question in the future.

If not, please let us know if you need some more assistance.

Thanks!
Ankur

 

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

View solution in original post

Hi I used this code for get location from requested by on change form and then set the location on work notes when requested by change . but return the sys_id . not the location. can you help me to this please

maryam7_0-1666363750932.png

maryam7_1-1666363796752.png

 

View solution in original post

14 REPLIES 14

script include

find_real_file.pngfind_real_file.png

Hi,

you did not copy script include correctly

remove everything from script and add this again and save

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

	getDetails: function() {

		var callerSysID = this.getParameter('sysparm_callerSysID');
		var gr = new GlideRecord('sys_user');
		gr.get(callerSysID);

		var mgr = gr.u_emploment_counrty.u_it_region.toString();
		var loc = gr.getValue('location');

		var obj = {}; // define the array
		obj["mgr"] = mgr; //set the manager in the array
		obj["loc"] = loc; //set the location in the array

		return JSON.stringify(obj);
	},

	type: 'reqtest'
});

Regards
Ankur

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

@Siva

Hope you are doing good.

Did my reply answer your question?

If so, please mark appropriate response as correct & helpful so that the question will appear as resolved for others who may have a similar question in the future.

If not, please let us know if you need some more assistance.

Thanks!
Ankur

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

@Siva

Hope you are doing good.

Did my reply answer your question?

If so, please mark appropriate response as correct & helpful so that the question will appear as resolved for others who may have a similar question in the future.

If not, please let us know if you need some more assistance.

Thanks!
Ankur

 

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

Hi I used this code for get location from requested by on change form and then set the location on work notes when requested by change . but return the sys_id . not the location. can you help me to this please

maryam7_0-1666363750932.png

maryam7_1-1666363796752.png