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

Ankur Bawiskar
Tera Patron
Tera Patron

@beycos 

your BR is on which table?

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

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!

Hello Siddhesh , 

it worked now . Many Thanks, 

Regards,

Beyza

beycos
Tera Contributor

Hello Ankur, 

My BR is on Business table and runs after "insert" and "update".

Regards,

Beyza