Trying to Update IP Address Group Fields from Related CIs

ShaunB
Tera Contributor

I'm new at this and not sure if I'm in the right forum but here's the issue I'm having...

 

Currently, in the CI list of our IP address, Approval Group, Change Group, Managed By Group and Support is showing up as empty.

ShaunB_1-1718734518620.png

 

 

I have a script in my business rules, but it does not seem to work. Am I missing anything? Anyone familiar with hot to fix this?

 

Here's my script:

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

    // Function to update fields from related CI

    function updateFieldsFromRelatedCI(relatedCI) {

        var updateNeeded = false;

       

        // Update the Approval Group field if it has changed

        if (relatedCI.change_control.changes() || current.change_control != relatedCI.change_control) {

            current.change_control = relatedCI.change_control;

            updateNeeded = true;

        }

       

        // Update the Change Group field if it has changed

        if (relatedCI.assignment_group.changes() || current.assignment_group != relatedCI.assignment_group) {

            current.assignment_group = relatedCI.assignment_group;

            updateNeeded = true;

        }

       

        // Update the Managed By Group field if it has changed

        if (relatedCI.managed_by_group.changes() || current.managed_by_group != relatedCI.managed_by_group) {

            current.managed_by_group = relatedCI.managed_by_group;

            updateNeeded = true;

        }

       

        // Update the Support Group field if it has changed

        if (relatedCI.support_group.changes() || current.support_group != relatedCI.support_group) {

            current.support_group = relatedCI.support_group;

            updateNeeded = true;

        }

       

        // Update the IP Address record if any fields have changed

        if (updateNeeded) {

            current.update();

        }

    }

   

    // Query to find all related CIs

    var relatedCIs = new GlideRecord('cmdb_rel_ci');

    relatedCIs.addQuery('child', current.sys_id);

    relatedCIs.query();

   

    while (relatedCIs.next()) {

        var relatedCI = new GlideRecord(relatedCIs.parent.sys_class_name);

        if (relatedCI.get(relatedCIs.parent.sys_id)) {

            updateFieldsFromRelatedCI(relatedCI);

        }

    }

})(current, previous);

1 ACCEPTED SOLUTION

@ShaunB 
If you are writing before BR then you don't have to write current.update()

Also make sure to check the table name because you writing the on IP address table and the records which are updating should be present in the same table.

Also, keep some logs to debug the script.

 

Thanks,
Murthy

View solution in original post

7 REPLIES 7

Murthy Ch
Giga Sage

Hello @ShaunB 

Which BR is this?

Thanks,
Murthy

ShaunB
Tera Contributor

BR = Business Rules correct? Here's a screenshot.

ShaunB_0-1718753771718.png

 

ShaunB
Tera Contributor

Hi Murty! Here's a screenshot

ShaunB_0-1718756698816.png

 

ShaunB
Tera Contributor

@Murthy Ch Sorry for the spam! I thought my messages weren't appearing cause of an issue. Didn't know I had to wait for approval for every message I send...