How to query the cmdb_ci table

geek1
Kilo Contributor

Hi everyone,

I'm having problems sending a query to the CMDI_CI table and was hoping someone could tell me what I'm doing wrong. The below script is pretty simple. It's an onchange when the user changes a reference field.

I have a reference field where the user selects an asset number from the cmdb_ci table. Once they select it, I have about 5 fields I want to autopopulate with more information about the asset they chose (install status, serial number, name, etc).

I can't get my script below to populate anything at all. It's not even catching in newData.next() as proven by my g_form.setValue('name', 'no match'); which always come back.

Can you query the CMDB_CI with name? Or does it have to be something in particular? I WANT to search it by asset_tag which is what they are selecting but even that isn't pulling back information.

Thank you!

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading)
{
return;
}

if (newValue == '')
{
return;
}

var newData = new GlideRecord('cmdb_ci');
newData.addQuery('name', newValue);
newData.query();

if (newData.next())
{
g_form.setValue('test', newUser.install_status.getDisplayValue());
g_form.setValue('name', 'we matched something');

}
else
{
g_form.setValue('name', 'no match');

}
}

1 REPLY 1

Michael Kaufman
Giga Guru

I think you need to change this line:



newData.addQuery('name', newValue);


to



newData.addQuery('sys_id', newValue);


Reference fields store sys_id of the record.

Mike