Using client script to lookup a table and insert result

oliver_shields
Mega Contributor

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.

1 ACCEPTED SOLUTION

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

View solution in original post

8 REPLIES 8

AbhishekGardade
Giga Sage

Hello Oliver

function onLoad() {
if (g_form.u_related_mob == '') {
return;
}
var recName = g_form.getUniquevalue();
var rec = new GlideRecord('cmdb_rel_ci');
rec.addQuery('child',recName);
if (rec.next())
g_form.setValue('u_related_mob', rec.parent.sys_id);
}

 

GlideRecord is not recommended in client script

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade

I assume i should be using GlideAjax in a CS? Unfortunately i have even less knowledge of this than GlideRecord. Is it easy enough to convert it from one to the other?

If you are returning single value then you can use of getreference in client script instead of calling script include.
Can you please share a screenshot for depending upon which field you want to value ?

you can refer:

https://www.servicenowguru.com/scripting/client-scripts-scripting/gform-getreference-callback/

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade

why you are making us of client script? Make us of display business rule or use advanced qualifier

Code for business rule:

var recName = current.sys_id;

var rec = new GlideRecord('cmdb_rel_ci');

rec.addQuery('child',recName);

if (rec.next()){

rec.setValue('u_related_mob', rec.parent.sys_id);

rec.update();
}

For reference qualifier:

Advanced Reference Qualifier Using a Script Include

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade