- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2019 03:23 AM
Im trying to create an onLoad client script that will set the default value of a reference field on a CI based on its relationship to another CI.
The CI will only ever have 1 relationship so im trying to pass the sys_id in to lookup the child field on the cmdb_ci_rel table and then pull the name of the related parent in and insert it into the custom field i have created. This is the script im trying which is doing nothing currently:
function onLoad() {
if (g_form.u_related_mob == '') {
return;
}
var recName = g_form.getReference(sys_id);
var rec = new GlideRecord('cmdb_rel_ci');
rec.addQuery('child',recName);
if (rec.next())
g_form.setValue('u_related_mob', rec.parent.sys_id);
}
I dont know scripting at all and this is mostly a frankensteins monster of other scripts ive come across.
Thanks for any assistance.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2019 03:49 AM
Yes! Use GlideAjax! It's really useful to know how. Client side glide records are very inefficient and you can't dot-walk values so rec.parent.sys_id won't work. In this case the value of the parent field will be the sys_id anyway so rec.parent will work but it's much better to use an ajax call:
function onLoad() {
if (g_form.u_related_mob == '') {
return;
}
var ga = new GlideAjax('ScriptIncludeName');
ga.addParam('sysparm_name', 'functionName');
ga.addParam('sysparm_ci', g_form.getUniqueValue());
ga.getXMLAnswer(getAjaxData);
function getAjaxData(response){
var answer = JSON.parse(response);
g_form.setValue('u_related_mob', answer.sysId, answer.displayValue);
}
script include (make sure you tick client callable and name the script include the same as your glide ajax call):
var ScriptIncludeName = Class.create();
ScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
functionName: function(){
var obj = {};
var ci = this.getParameter('sysparm_ci');
var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('child', ci);
gr.query();
if(gr.next()){
obj = {
'sysId': gr.parent.getValue(),
'displayValue': gr.parent.getDisplayValue();
};
return JSON.stringify(obj);
},
type: 'ScriptIncludeName'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2019 03:49 AM
Yes! Use GlideAjax! It's really useful to know how. Client side glide records are very inefficient and you can't dot-walk values so rec.parent.sys_id won't work. In this case the value of the parent field will be the sys_id anyway so rec.parent will work but it's much better to use an ajax call:
function onLoad() {
if (g_form.u_related_mob == '') {
return;
}
var ga = new GlideAjax('ScriptIncludeName');
ga.addParam('sysparm_name', 'functionName');
ga.addParam('sysparm_ci', g_form.getUniqueValue());
ga.getXMLAnswer(getAjaxData);
function getAjaxData(response){
var answer = JSON.parse(response);
g_form.setValue('u_related_mob', answer.sysId, answer.displayValue);
}
script include (make sure you tick client callable and name the script include the same as your glide ajax call):
var ScriptIncludeName = Class.create();
ScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
functionName: function(){
var obj = {};
var ci = this.getParameter('sysparm_ci');
var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('child', ci);
gr.query();
if(gr.next()){
obj = {
'sysId': gr.parent.getValue(),
'displayValue': gr.parent.getDisplayValue();
};
return JSON.stringify(obj);
},
type: 'ScriptIncludeName'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2019 03:50 AM
Go through the guide below, it's a really good explanation of how glide ajax works, there's a technow episode on it as well which is handy:
https://community.servicenow.com/community?id=community_blog&sys_id=f8ccee25dbd0dbc01dcaf3231f961978
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2019 04:21 AM
That worked perfectly. Thank you very much for your help.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2019 03:33 AM
function onLoad() {
if (g_form.u_related_mob == '') {
return;
}
var recName = g_form.getReference('cmdb_rel_ci', setData);
}
function setData(recName) {
if (recName)
g_form.setValue('u_related_mob', recName.<field_name_of_table_cmdb_rel_ci>);
}
Regards,
Sanket