The CreatorCon Call for Content is officially open! Get started here.

How to call new GlideRecord with a variable as parameter

felipe_barbosa
ServiceNow Employee
ServiceNow Employee

Hi everyone,

Today I lost a lot of time trying to make a client script to work as I want, and after some time failing I decided to come for help.

All I need is to make a GlideRecord run when the table name comes from a variable. The code I am working with is much more complex than the example I will post below, but all I need to know can be shown in a few lines. Basically I need to call new GlideRecord passing a variable as parameter:

var table = 'cmdb_ci';
var ci = new GlideRecord(table);
ci.query();

This definetly doesnt work as I need it to work... can anyone help me on this?

Thanks and regards,


Felipe

2 REPLIES 2

SakshiArora
Giga Contributor

Hi,

We can use variable to run Glide Record by passing the table name as variable.You can see the below code as an example:

function onLoad() {
var count = 0;
var table = 'incident';
var gr = new GlideRecord(table);
gr.addQuery('priority',1);
gr.query();
while(gr.next())
{
count = count + 1;
}
alert(count);
}

I have tried this and it is working in onLoad Client Script.. I hope you are looking for the same.


Hi Sakshi,

Yes basically this is what I am doing, I didn't forgot about having a loop to read throught the results, and still, the call to GlideRecord didn't worked when I passed a variable as the name of the table. I even tried like table.toString() to force a string output.

Anyway, I kind of accomplished what I needed by calling a script include from my client script, and in the script include I used the GlideRecordUtil which has a nice feature that gets all the cmdb data, from cmdb_ci and from the child table, like, cmdb_ci_service. It has another nice features that worth taking a look:

GlideRecordUtil on ServiceNow Wiki

Cheers!