Business Rule to copy a list field to another list field

Staxed
Giga Guru

Looking for a little advice.  I have a business rule that is supposed to be updating a list field on one table to the list field on another table.  Both fields setup exactly the same, list types with a reference to the sys_user_group_type table in order to select multiple types.

 

The business rule looks like it's working according to the logs I have added, the log is showing multiple sysids after the business rule runs.  However, on the target table, after the business rule has run, the field only shows the first result in the list (it only ever has one entry in it, even though the log is showing all sysids are being passed).  I'm stuck trying to figure out what is going on at this point.

 

It is a before insert/update business rule set to run when the assignment group changes:

 

 

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

        var groupSysId = current.assignment_group;
        var groupRecord = new GlideRecord('sys_user_group');
        if (groupRecord.get(groupSysId)) {

            var groupTypes = groupRecord.type;
			gs.info("GroupType - Assignment Group Type: " + groupTypes);

            current.u_assignment_group_type = groupTypes;
			gs.info("GroupType - Current Custom Assignment Group Type: " + current.u_assignment_group_type);
        }
})(current, previous);

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Staxed 

if field u_assignment_group_type is list type then use this

(function executeRule(current, previous /*null when async*/ ) {
        // Check if assignment_group is set
        if (current.assignment_group) {
            // Get the sys_id of the assignment group
            var groupSysId = current.assignment_group;
            var arr = [];
            // Query the sys_user_group table to fetch the type field
            var groupRecord = new GlideRecord('sys_user_group');
            groupRecord.addQuery('sys_id', 'IN', groupSysIds);
            groupRecord.query();
            while (groupRecord.next()) {
                arr.push(groupRecord.type.toString());
            }
            // Set the u_assignment_group_type field with the same values
            current.u_assignment_group_type = arr.toString();
        }
})(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

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@Staxed 

try this

this will work if u_assignment_group_type field is string

 

(function executeRule(current, previous /*null when async*/ ) {
        // Check if assignment_group is set
        if (current.assignment_group) {
            // Get the sys_id of the assignment group
            var groupSysId = current.assignment_group;
            var arr = [];
            // Query the sys_user_group table to fetch the type field
            var groupRecord = new GlideRecord('sys_user_group');
            groupRecord.addQuery('sys_id', 'IN', groupSysIds);
            groupRecord.query();
            while (groupRecord.next()) {
                arr.push(groupRecord.type.getDisplayValue());
            }
            // Set the u_assignment_group_type field with the same values
            current.u_assignment_group_type = arr.toString();
        }
})(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

@Staxed 

if field u_assignment_group_type is list type then use this

(function executeRule(current, previous /*null when async*/ ) {
        // Check if assignment_group is set
        if (current.assignment_group) {
            // Get the sys_id of the assignment group
            var groupSysId = current.assignment_group;
            var arr = [];
            // Query the sys_user_group table to fetch the type field
            var groupRecord = new GlideRecord('sys_user_group');
            groupRecord.addQuery('sys_id', 'IN', groupSysIds);
            groupRecord.query();
            while (groupRecord.next()) {
                arr.push(groupRecord.type.toString());
            }
            // Set the u_assignment_group_type field with the same values
            current.u_assignment_group_type = arr.toString();
        }
})(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

This is doing the exact same thing as my original script, it returns a comma separated list of the correct sysids, but the field itself is only being updated with the first one in the list for some reason.

hmm, actually, I think both of our scripts work.  I just attempted to update the field manually with the business rule deactivated and it only saves one entry even though it's set as a list.  I might have to dig into another issue now.  Thanks.