Sync Assigned Users from list collector variable to Virtual PC request list collector field

ronro2
Tera Contributor

Hey guys! 

There are two cat items: 'Virtuell dator standard' and 'Virtuell dator avancerad'. Within these two there is a connected variabel set called 'vgr_virtual_pc_request' which contains a list collector variable 'vpcreq_assigned_to', based on sys_user table. This very list collector variable enables users to add/assign multiple users (user IDs) to a virtual computer (configuration item) (see illustration below):

ronro2_1-1741096919825.png

In the request (sc_req_item) there is a reference field called 'configuration_item' (which is a reference to table 'u_cmdb_ci_pc_hardware_virtual'. In table 'u_cmdb_ci_pc_hardware_virtual' there is a custom list collector field called 'u_vpc_assigned_to'. 

The business requirement: I want my custom field 'u_vpc_assigned_to' to be filled with all the chosen users (from the variable set variable 'vpcreq_assigned_to' automatically.  So I want custom 'u_vpc_assigned_to' field to be filled with the users chosen in the first illustration. Se illustration below: 

ronro2_2-1741097117032.png

 



So what is needed to achieve this? I tried with this Business Rule, which is not working: 

ronro2_3-1741097175923.png

 

 

 

(function executeRule(current, previous /*null when async*/) {
    // Get the values from the variable set
    var assignedTo = current.variables.vpcreq_assigned_to;

    // Check if the variable has values
    if (assignedTo) {
        // Convert the values to an array
        var assignedToArray = assignedTo.split(',');

        // Update the reference field with the values
        current.u_vpc_assigned_to = assignedToArray.join(',');
        current.update();
    }
})(current, previous);

 

 

What could be wrong? 
Thanks in advance!

1 REPLY 1

ronro2
Tera Contributor

It works now that I changed to After insert/update + after I adjusted the script to the following: 

(function executeRule(current, previous /*null when async*/) {
    // Ensure the variable has values
    if (!current.variables.vpcreq_assigned_to || current.variables.vpcreq_assigned_to.nil()) {
        gs.log("Business Rule: No users assigned in 'vpcreq_assigned_to'");
        return;
    }

    // Get the assigned users (sys_ids from list collector)
    var assignedTo = current.variables.vpcreq_assigned_to.toString();
    var assignedToArray = assignedTo.split(',');

    // Ensure there's a configuration item linked
    if (!current.configuration_item || current.configuration_item.nil()) {
        gs.log("Business Rule: No configuration item linked to the request");
        return;
    }

    // Retrieve the related virtual PC record
    var vpcRecord = new GlideRecord('u_cmdb_ci_pc_hardware_virtual');
    if (vpcRecord.get(current.configuration_item)) {
        // Set the custom field properly
        vpcRecord.setValue('u_vpc_assigned_to', assignedToArray); // Use setValue() for list fields
        if (vpcRecord.update()) {
            gs.log("Business Rule: Successfully updated u_vpc_assigned_to with: " + assignedToArray.join(','));
        } else {
            gs.log("Business Rule: Failed to update u_vpc_assigned_to.");
        }
    } else {
        gs.log("Business Rule: Could not find the corresponding virtual PC record.");
    }
})(current, previous);