Returning a string, object or an array from script include gives value undefined

moni170
Tera Contributor

I created an onchange Client Script on Configuration item field of incident table. Used GlideAjax and Script-Include.

The script include returns the company, manufacturer and the asset_tag value of the configuration item being selected.

But somehow it's returning undefined.

I tried using array, object and string as well to return the value from the script include but still the same result.

I even tried to match the sys_id instead of the name field.

I'm attatching the screenshots of Client script, script include and the output here.

Looking forward for the solution.

Thankyou.

2 ACCEPTED SOLUTIONS

Sandeep Rajput
Tera Patron
Tera Patron

@moni170 Here is the source of your issue.

 

someCode.jpg

Please try updating your code as follows and see if it works for you.

 

 

cmdb: function() {
	var obj = ‘’;
	var ab = this.getParameter(‘sysparm_ci’);
	var gr = new GlideRecord(‘cmdb_ci’);
	gr.addQuery(‘sys_id’, ab);
	gr.query();
	if (gr.next()) {
		obj = obj + gr.manufacturer.toString() + ',' + gr.company.toString() + ',' + gr.asset_tag.toString();
	}

	return obj;
}

 

Hope this helps.

View solution in original post

Sohithanjan G
Kilo Sage
Kilo Sage

Hey @moni170 , 

you are missing .next() in script include. Please correct it

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

View solution in original post

5 REPLIES 5

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi,
You should change the script include function "cmdb" like  gr.addQuery('sys_id',ab)


Thanks and Regards,

Saurabh Gupta

Vishal Birajdar
Giga Sage

Hi @moni170 

 

In script include, write below code like below

 

cmdb : function (){
/* 1. Declare array*/
var result =[];

/*2. Glide record on your table */
var gr = new GlideRecord('table_name');
gr.addQuery('field_name','your_query');
gr.query();

while (gr.next()){
var obj ={};
obj.company = gr.getValue('company');
obj.asset = gr.getValue('asset_tag');
//You can add more as per your need

/*3. Push object in result array*/
result.push(JSON.stringify(obj));

}

/*Return the result*/
return result.toString();

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Sandeep Rajput
Tera Patron
Tera Patron

@moni170 Here is the source of your issue.

 

someCode.jpg

Please try updating your code as follows and see if it works for you.

 

 

cmdb: function() {
	var obj = ‘’;
	var ab = this.getParameter(‘sysparm_ci’);
	var gr = new GlideRecord(‘cmdb_ci’);
	gr.addQuery(‘sys_id’, ab);
	gr.query();
	if (gr.next()) {
		obj = obj + gr.manufacturer.toString() + ',' + gr.company.toString() + ',' + gr.asset_tag.toString();
	}

	return obj;
}

 

Hope this helps.

Vasu ch
Kilo Sage

Hi @moni170 ,

Try with the below code

Script Include:

Vasuch_0-1699121393924.png

Client Script:

Vasuch_1-1699121439606.png

 

Output:

Vasuch_2-1699121478443.png