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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Add some debugging to your code. This will quickly identify up to where your code is working, variables hold values you would expect, etc.. With this, you will identify within seconds where to look at, instead of spending hours on this.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

Because you are trying to set reference fields with this client script, do also read and apply:
2020-10-19 setValue() reference fields/variables, use the value AND displayValue parameter

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you are forming array and then converting it to json string; don't do that

Also don't use evalJSON

please try this and share the update

Script Include:

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'
});

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 parsedData = JSON.parse(answer); //evaluate the response and decode it
	g_form.setValue('u_region',parsedData.mgr); //set the short description
	g_form.setValue('location',parsedData.loc); //set te description

}
}

Regards
Ankur

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

Hi ankur,

Thank you for your reply,

I am using your code, But i didn't get result, can u please help me on that.

Thank you.