GlideRecord Query – Updating Specific Records Instead of All Records

beycos
Tera Contributor

Hello Community,

I am working with two tables: "Business" and "cmdb_ci_ip".

  1. The "Business" table has a reference field u_business, which references the "Configuration Item" table.
  2. The "cmdb_ci_ip" table has a string field u_display_name.

My goal is to update the u_maximum_acceptable_outage  field in the "cmdb_ci_ip" table, based on a match between u_business  (in the "Business" table) and  u_display_name.(in the "cmdb_ci_ip" table).

The process should work like this:

  • If the value in the u_business field in the "Business" table matches the u_display_name field in the "cmdb_ci_ip" table, I want to update the u_acceptable_outage field in the "Business" table with the value from u_maximum_acceptable_outage in the "cmdb_ci_ip" table.

However, my current script is updating all records in the "cmdb_ci_ip" table, not just the matching records. The script is not checking the values properly and instead, it's updating all the records.  

 

This is the script 

(function executeRule(current, previous /*null when async*/ ) {

var serviceRecord = new GlideRecord('cmdb_ci_ip');
serviceRecord.addQuery('u_business.sys_id', current.u_display_name);
serviceRecord.query();


while (serviceRecord.next()) {

serviceRecord.u_acceptable_outage= current.u_maximum_acceptable_outage ;


serviceRecord.update();

 

The script updates all records in the "cmdb_ci_ip" table instead of only the ones that match.

I want to update only those records in the cmdb_ci_ip table where u_business  matches u_display_name.

 

Can someone please explain why this is happening and how I can fix the issue so that only the matching records are updated?

Any help would be greatly appreciated!

 Best Regards, 

Beyza

1 ACCEPTED SOLUTION

Hi @beycos,

 

The issue occurs because the addQuery condition is incorrect. In your script, you are trying to match u_business.sys_id with u_display_name, but u_business is a reference field, and u_display_name is a string field. Therefore, the query is not properly filtering the records.

 

To fix this issue, you should ensure that you are comparing the correct field types. If u_business references the Configuration Item table, you need to compare the display value of u_business with u_display_name.

 

Here’s the corrected script:

 

Corrected Script

 

(function executeRule(current, previous /*null when async*/) {

    var serviceRecord = new GlideRecord('cmdb_ci_ip');

    serviceRecord.addQuery('u_display_name', current.u_business.getDisplayValue()); // Match display value

    serviceRecord.query();

 

    while (serviceRecord.next()) {

        // Update the field with the value from the Business table

        serviceRecord.u_maximum_acceptable_outage = current.u_acceptable_outage;

        serviceRecord.update(); // Update only matching records

    }

})(current, previous);

 

Explanation

 

1. Query Correction:

Use current.u_business.getDisplayValue() to get the display value of the u_business reference field.

Compare it with u_display_name, which is a string field.

 

 

2. Field Update:

The script updates the u_maximum_acceptable_outage field in the cmdb_ci_ip table only for records where the display value of u_business matches u_display_name.

 

3. Targeted Updates:

The addQuery ensures only matching records are fetched and updated, preventing all records from being modified.

 

Best regards,

Siddhesh Jadhav

 

If this solution helps resolve your query, kindly mark my answer as helpful and accepted. Let me know if you need further assistance!

View solution in original post

9 REPLIES 9

@beycos 

try this

(function executeRule(current, previous /*null when async*/) {
    // Query the cmdb_ci_ip table to find records where u_display_name matches u_business in the Business table
    var ipRecord = new GlideRecord('cmdb_ci_ip');
    ipRecord.addQuery('u_display_name', current.u_business);
    ipRecord.query();

    while (ipRecord.next()) {
        // Update the u_acceptable_outage field in the cmdb_ci_ip table
        ipRecord.u_acceptable_outage = current.u_maximum_acceptable_outage;
        ipRecord.update();
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@beycos 

try this

(function executeRule(current, previous /*null when async*/) {
    var businessRecord = new GlideRecord('business Table');
    businessRecord.addQuery('u_business', current.u_display_name);
    businessRecord.query();

    while (businessRecord.next()) {
        var ipRecord = new GlideRecord('cmdb_ci_ip');
        ipRecord.addQuery('u_display_name', businessRecord.u_business);
        ipRecord.query();

        while (ipRecord.next()) {
            ipRecord.u_acceptable_outage = businessRecord.u_maximum_acceptable_outage;
            ipRecord.update();
        }
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

beycos
Tera Contributor

Hello Ankur, Unfortunately, it didn't work. Actually,my script is running, but why  it be updating all records in this table? Shouldn't it only update the matching records.

Best Regards, 

beyza 

 

beycos
Tera Contributor

Hello again ,

How to determine the field that cause of following error message?

" Invalid query detected, please check logs for details [Unknown field null in table cmdb_ci_ip]"

Thank you.

Regards, 

Beyza

@beycos 

seems you are querying on some field which is not present on that table

it's not able to query and hence updating all the records

You already have a field on business app table which refers to cmdb_ci but we are querying cmdb_ci_ip table

that field should refer cmdb_ci_ip table and not cmdb_ci

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader