- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 05:37 PM
Hi All,
Will anyone help me come up with a client script that will copy the value of a subcategory to the Configuration Item field? I can't write javascript and am in a hurry
I believe it it should be "on change" type.
Incident table
psuedo script
If "subcategory" changes to "anything", set "Configuration Item" to same as "subcategory"
Will copying the display name into that reference field work if they are the same?
Currently, I have a reporting issue with the way I've enhanced the form and think this may be the quickest fix Until we figure out how to fix the data issue we've created
if I had this script I could remove several ui policies and actions which are becoming confusing and sort of have me cornered at the moment.
Please let let me know if you can help.
Thanks in advance for your time and assistance!
regards, Carl
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 10:55 PM
first create script include mark that check box client callable
var test = Class.create();
test.prototype = Object.extendsObject(AbstractAjaxProcessor, {
ajaxFunction_LocationAjax : function() {
var fo='';
var customerID = this.getParameter('sysparm_custid'); //paramter from the client side
var ag = new GlideRecord('cmdb_ci');
ag.addQuery('name', customerID);
ag.query();
while (ag.next()) {
fo= ag.sys_id;
gs.log('value is :'+fo);
}
return fo;
},
type: 'test'
});
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var customerNo = g_form.getValue('subcategory');
var gajax = new GlideAjax('test'); // call the Script Include class
// call the public method
gajax.addParam('sysparm_name', 'ajaxFunction_LocationAjax');
// pass the sysID to the method
gajax.addParam('sysparm_custid', customerNo);
//callback function is used to return the result, passed to the getXML function
gajax.getXML(callBackLocation);
function callBackLocation(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
alert(answer);
g_form.setValue('cmdb_ci', answer);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 10:21 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 10:25 PM
Please note getDisplayValue() will not work on client script, instead you can use the newValue in your glide query, this should help.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var ci = new GlideRecord('cmdb_ci');
ci.addQuery('name',newValue);
ci.query();
if(ci.next())
g_form.setValue('cmdb_ci',ci.sys_id);
}
You can use the GlideRecord on client script, However, this methods are no longer recommended due to their performance impact. This methods retrieve all fields in the requested GlideRecord when most cases only require one field.
Reference: http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices#Minimize_Server_Lookups
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 10:34 PM
that works!!!! thank you!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 10:40 PM
if you are doing glide record in your client script that will create some performance issue because client script always use to make some form validation or changes in UI or auto populating on form(if you need some value from other table and want that data to populate in the field, always prefer glide ajax).
best practice is always prefer client callable script include and use it in client script through glide ajax.
what is GlideAjax ?How to write Ajax function? | Ahmed Drar
Script include:
var LocationAjax = Class.create(); // create Class
LocationAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
ajaxFunction_LocationAjax : function() { // public method
var fo='';
var customerID = this.getParameter('sysparm_custid'); //paramter from the client side
var ag = new GlideRecord('cmdb_ci');
ag.addQuery('name', customerID);
ag.query();
while (ag.next()) {
var fo= ag.sys_id;
gs.log('value is :'+fo);
}
return fo;
},
type : "LocationAjax"
});
Client SCript:
var customerNo = g_form.getValue('subcategory');
var gajax = new GlideAjax("LocationAjax"); // call the Script Include class
// call the public method
gajax.addParam("sysparm_name", "ajaxFunction_LocationAjax");
// pass the sysID to the method
gajax.addParam("sysparm_custid", customerNo);
//callback function is used to return the result, passed to the getXML function
gajax.getXML(callBackLocation);
function callBackLocation(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
g_form.setValue('cmdb_ci', answer);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2017 10:47 PM
This is a bit over my head at the moment.
I think you suggest I create a script include and then "call it" in the client script. Is that correct?
I don't know which fields to put where to be able to try it out.