- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2017 10:37 AM
Hi Guys, just a question
I am a bit stuck and need to finish this script:
What I want to do is to return a name and use that name to set another reference field with the same name
I just need help with the set of the reference field
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}
var user_ref = g_form.getReference('user', loadInfo);
function loadInfo(user_ref) {
var gp = new GlideRecord('cmn_location');
gp.addQuery('sys_id', user_ref.location);
gp.query(function(gp){
if(gp.next()) {
var locname = gp.name;
// This is working up until now
// NOW I NEED TO SET A VALUE ON A REFERENCE FIELD ON WHERE THE NAME IS THE SAME AS gp.name
}
});
}}
Once this is done, what would be the best way to do this for another 2 reference fields?
Regards
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2017 12:24 PM
Hi Rudi,
For this scenario you need to use glide ajax, as you need to do DB operation through client script. So Create a Catalog Client Script and pass the User sys ID to Script Include where we do DB operation and again the location Sys ID sends back to Client script where we set the location. Please use the below script:
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.clearValue('location');
var user = g_form.getReference('user').sys_id;
var ajax = new GlideAjax('SetLocationUtil');
ajax.addParam('sysparm_name', 'setLocation');
ajax.addParam('sysparm_user', user);
ajax.getXML(function () {
g_form.setValue('location', ajax.getAnswer());
});
}
Script Include:
Script include Name: SetLocationUtil
and don't forget to check the Client callable checkbox.
Here is the Script for Script include
var SetLocationUtil = Class.create();
SetLocationUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
setLocation: function(){
var user = this.getParameter('sysparm_user');
var gr = new GlideRecord("sys_user");
gr.addQuery('sys_id',user);
gr.query();
if (gr.next()) {
return gr.location.sys_id.toString();
}
else return '';
},
type: 'SetLocationUtil'
});
Thanks,
Arindam

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2017 10:41 AM
- function onChange(control, oldValue, newValue, isLoading) {
- if (newValue == '') {
- return;
- }
- var user_ref = g_form.getReference('user', loadInfo);
- function loadInfo(user_ref) {
- var gp = new GlideRecord('cmn_location');
- gp.addQuery('sys_id', user_ref.location);
- gp.query(function(gp){
- if(gp.next()) {
- var locname = gp.name;
- // This is working up until now
- // NOW I NEED TO SET A VALUE ON A REFERENCE FIELD ON WHERE THE NAME IS THE SAME AS gp.name
- g_form.setValue('your variable name',gp.sys_id); //Use sys_id, since you are setting a reference field to location
- }
- });
- }}
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2017 10:58 AM
Hi Sanjiv
The sys_id's are not the same, only the names. The user's location is different to the new location table's data, so I am setting the new reference if I can find the same value. We are getting the new locations from a different source.
Regards

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2017 11:06 AM
In that case, you will have to query the other data source as wel.
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2017 10:57 AM
Hi Rudi,
Writing glide record in client script is not a best practice. you can try with script include and call it in client script using glide ajax.
It will slow your form because you are travelling to server to get the record. each time when you will make the changes on form field it will enter to the server.
http://wiki.servicenow.com/index.php?title=GlideAjax