GlideRecord not return data from table

stevethomas
Giga Contributor

I am trying to pull data from the demand and risk tables.  I have a GlideRecord that pulls data from the demand table and this is working.  I take the demand ticket number and assign to a variable (tasknumber).  I use that variable in another GlideRecord to pull any matching record from the risk table.  Here is where my script is failing.  Seem no data is returned. (I have triple check the table and there is a match in the risk table field risk ticket to a demand ticket number.  I have set up logging and the GRrisk.risk and GRrisk.short_description is "undefined".  I can use some help!

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var demandname = g_form.getValue('name');

var GRdemand = new GlideRecord('dmn_demand');
GRdemand.addQuery('short_description', demandname);
GRdemand.query();

while(GRdemand.next())
{
g_form.setValue('short_description', GRdemand.short_description);
g_form.setValue('number', GRdemand.number);
g_form.setValue('business_case', GRdemand.business_case);
}

var tasknumber = g_form.getValue('number');
jslog('task number@@@@@@@@@@@@@ ' + tasknumber);

var GRrisk = new GlideRecord('risk');
GRrisk.addQuery('task', tasknumber);
GRrisk.query();

jslog('task number ***** ' + GRrisk.task);
jslog('short des ***** ' + GRrisk.short_description);


while(GRrisk.next())
{
g_form.setValue('tasknumber', GRrisk.task);
g_form.setValue('taskshortdescription', GRrisk.short_description);
}

}

4 REPLIES 4

simonbergstedt
Tera Guru

Hmm, from the looks of it you are tasknumber is the number of the task (ie CHG123456) while task in your GRrisk.addquery wants the sys_id of the task. Could you try to use this instead of that addquery?

GRrisk.addEncodedQuery("task.number=" + tasknumber);

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Steve, would mind explaining your use case?  Using GlideRecord in a client script is not good practice and where possible move this processing to server side.  Can this data be filled in after submit or update instead?

We want the business to be able to view all demands without having to purchase more licenses for ServiceNow.  I am creating a Record Producer in the Service Catalog that the business can go in and from lookup select box select a demand.  I want to have the data from the following table to viewed in the record producer.  Demand, Stakeholders, Requirements, Risks, Decisions, Resource Plans, and Assessment Results.  Used an OnChange so the data will be display when a Demand is selected.  If there is a better way please let me know.  Under stand GlideRecords are not best practice, but this is my the first time using GlideRecords and want to get a handle on GlideRecords before using the GlideAjax.

Mike Patel
Tera Sage

As Michael mentioned doing gliderecord query is not good practice but if you had to do it for any reason than change below;

var tasknumber = g_form.getUniqueValue();
or

var tasknumber = g_form.getValue('sys_id');