How a statement NOT equals an encoded query

Kenneth3
Tera Guru

Hi,

I have a script that checks if a record matches specific criteria. I have put those criteria in an encoded query. When the record match the criteria I update a field, like this:

var activateWorkstation = new GlideRecord("cmdb_ci_hardware");
activateWorkstation.addEncodedQuery("sys_class_name=cmdb_ci_computer^ref_cmdb_ci_computer.os_domainINexample.com,example2.com^hardware_statusINinstalled,in_stock^hardware_substatusINin_use,reserved);
activateWorkstation.query();

while (activateWorkstation.next())
{
  activateWorkstation.u_exclude_for_reconcile_sam = "false";
  activateWorkstation.update();
}


But when an record does not match those specific criteria (for example something changes in the record), I want to set this field to "true". This has to be done, when the record NOT equals the encoded query. Something like this:

var ignoreWorkstation = new GlideRecord("cmdb_ci_hardware");
ignoreWorkstation.addEncodedQuery("sys_class_name=cmdb_ci_computer^ref_cmdb_ci_computer.os_domainINexample.com,example2.com^hardware_statusINinstalled,in_stock^hardware_substatusINin_use,reserved);
ignoreWorkstation.query();

while (!ignoreWorkstation.next())
{
  ignoreWorkstation.u_exclude_for_reconcile_sam = "true";
  ignoreWorkstation.update();
}


I know the second blok is not correct, but I want to describe the issue I am facing in the second code block. Does anyone have some suggestions how to accomplish this?

 

Thanks,
Kenneth

 

2 REPLIES 2

Anil Lande
Kilo Patron

Hi,

Your 2nd while loop will not work because there is no record available to update when you apply that query.

You have used while(!ignoreWorkstation.next()), in this case it will enter inside a loop when ignoreWorkstation holds no record. So there is nothing to update.

 

If you want to update records which are not matching the query you have written then you need to apply exact opposite query to get records which does not match condition(your current condition) and update those records. 

Your script would be same as 1st loop the only thing you need to change is Query. And set value to false for another set of records.

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

BALAJI40
Mega Sage

Hi,

You can select your query with not operations,

sys_class_name=cmdb_ci_computer(choose is not operator in the filter)

ref_cmdb_ci_computer.os_domainINexample.com,example2.com^ choose is not with AND operation each,

 ref_cmdb_ci_computer.os_domain!=example.com^ref_cmdb_ci_computer.os_domain!=example2.com

hardware_statusINinstalled,in_stock (choose is not one of operator in the filter)

^hardware_substatusINin_use,reserved -- (choose is not one of operator in the filter)