Business Rule not working as expected

Gal Katz
Kilo Sage

I have a business rule on the 'alm_asset' table that after an update of a record, updates all other records that have the same Serial Number.
After updating the icon that indicates a change on a field appears but the value is not updated:

Screenshot 2023-10-02 135715.png

 The business rule script is:

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

    ////////////////////// Query the 'alm_asset' table for records with the same serial number///////////////
    var serialNumber = current.serial_number;
    var assetGr = new GlideRecord('alm_asset');
    assetGr.addQuery('serial_number', serialNumber);
    assetGr.query();
    ///////////////////////Get all fields///////////////////////////////////////////////////////////////////
    var arr_fields = [];
    var fields = new GlideRecord('sys_dictionary');
    fields.addQuery('name', 'alm_asset');
    fields.query();

    while (fields.next()) {
        arr_fields.push(fields.element.toString());
    }

    ///////////////////// Loop through matching records and update them/////////////////////////////////////////
    while (assetGr.next()) {
        if (assetGr.sys_id != current.sys_id) {
            // Update the fields on related records
            for (i = 0; i < arr_fields.length; i++) {
                assetGr.setValue(arr_fields[i], current.getValue(arr_fields[i]));
            }
			assetGr.update();
        }
	}

})(current, previous);
1 ACCEPTED SOLUTION

Gal Katz
Kilo Sage

The problem was that my script was trying to change the sys_id.
The working script is:

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

    ////////////////////// Query the 'alm_asset' table for records with the same serial number///////////////
    var serialNumber = current.serial_number;
    var assetGr = new GlideRecord('alm_asset');
    assetGr.addQuery('serial_number', serialNumber);
	assetGr.addQuery('sys_id', '!=', current.sys_id);
    assetGr.query();
    ///////////////////////Get all fields///////////////////////////////////////////////////////////////////
    var arr_fields = [];
    var fields = new GlideRecord('sys_dictionary');
    fields.addQuery('name', 'alm_asset');
    fields.query();

    while (fields.next()) {
        arr_fields.push(fields.element.toString());
    }
    ///////////////////// Loop through matching records and update them///////////////////////
    while (assetGr.next()) {
            for (i = 0; i < arr_fields.length; i++) {
                if(arr_fields[i] != 'sys_id'){
                    assetGr.setValue(arr_fields[i], current[arr_fields[i]]);             
                }
          
            }
			assetGr.update();
        }
})(current, previous);

View solution in original post

8 REPLIES 8

Hi @Community Alums. 
There are 2 of them with Operation of report_view

Community Alums
Not applicable

okay, please check the ACL rules, if they have proper permissions or else you can't view those fields.

Regards

Suman P.

Looked on it. Unfortunately the issue does not lie there. 

Gal Katz
Kilo Sage

The problem was that my script was trying to change the sys_id.
The working script is:

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

    ////////////////////// Query the 'alm_asset' table for records with the same serial number///////////////
    var serialNumber = current.serial_number;
    var assetGr = new GlideRecord('alm_asset');
    assetGr.addQuery('serial_number', serialNumber);
	assetGr.addQuery('sys_id', '!=', current.sys_id);
    assetGr.query();
    ///////////////////////Get all fields///////////////////////////////////////////////////////////////////
    var arr_fields = [];
    var fields = new GlideRecord('sys_dictionary');
    fields.addQuery('name', 'alm_asset');
    fields.query();

    while (fields.next()) {
        arr_fields.push(fields.element.toString());
    }
    ///////////////////// Loop through matching records and update them///////////////////////
    while (assetGr.next()) {
            for (i = 0; i < arr_fields.length; i++) {
                if(arr_fields[i] != 'sys_id'){
                    assetGr.setValue(arr_fields[i], current[arr_fields[i]]);             
                }
          
            }
			assetGr.update();
        }
})(current, previous);