Autopopulate Assignment group not populating by SYS ID

Nisha30
Kilo Sage

Hi ,

The OOB Business rule  Populate Assignment Group based CI/SO  is working fine the only condition not working is the ELSE it is executing but the assignemnt group is not populating. we tried with SYS ID and Name as well.

 

Nisha30_0-1731517824451.png

 

 

(function executeRule(current, previous /*null when async*/ ) {
    /*
    This business rule will get executed for incident/problem/change on insert and update, if Assignment Group is empty and CI/ServiceOffering changing from empty value.
    */
    gs.log("<<<POP BR");
    //!(current.sys_class_name + '' === 'change_request' && current.unauthorized == true)
    //!(current.sys_class_name + '' === 'change_request' && current.unauthorized == true)

    var assignmentGroupFieldName = '';
    if (current.sys_class_name + '' == 'change_request') {
        assignmentGroupFieldName = 'assignment_group';
    } else if (current.sys_class_name + '' == 'incident' || current.sys_class_name + '' == 'problem') {
        if(current.x_moogs_incident_m_moogsoft_incident_id.toLowerCase() != 'automation')
        {
        assignmentGroupFieldName = 'support_group';
        }
        else
        {
               assignmentGroupFieldName = "3f02ab9687359e10ab9c2fce8bbb355a";  
        }
    }

    if (!gs.nil(current.cmdb_ci) && current.cmdb_ci.changesFrom('')) {
        assignmentGroupFieldName = populateAssignmentGroupFieldName(current.sys_class_name + '', 'ci') || assignmentGroupFieldName;
        current.assignment_group = current.cmdb_ci[assignmentGroupFieldName];
    }

    if (gs.nil(current.assignment_group) && !gs.nil(current.service_offering) && current.service_offering.changesFrom('')) {
        assignmentGroupFieldName = populateAssignmentGroupFieldName(current.sys_class_name + '', 'service_offering') || assignmentGroupFieldName;
        current.assignment_group = current.service_offering[assignmentGroupFieldName];
    }
})(current, previous);

function populateAssignmentGroupFieldName(className, fieldName) {
    // Populate CI and Service Offering field names from properties that will be used to populate current task assignment group
    return GlideApplicationProperty.getValue('com.snc.' + className + '.' + fieldName + '_assignment_group.field_name');
}
 
Thanks
1 ACCEPTED SOLUTION

What is the overall requirement?

 

It looks like you want OOB behavior if x_moogs_incident_m_moogsoft_incident_id is not 'automation' - otherwise you want to set the assignment group to '3f02ab9687359e10ab9c2fce8bbb355a'?

 

 

    var assignmentGroupFieldName = '';
    if (current.sys_class_name + '' == 'change_request') {
        assignmentGroupFieldName = 'assignment_group';
    } else if (current.sys_class_name + '' == 'incident' || current.sys_class_name + '' == 'problem') {
        if(current.x_moogs_incident_m_moogsoft_incident_id.toLowerCase() != 'automation')
        {
        assignmentGroupFieldName = 'support_group';
        }
        else
        {
               assignmentGroupFieldName = "3f02ab9687359e10ab9c2fce8bbb355a";  
        }
    }

 

 

 

I am not familiar with the Moogsoft integration, but I would confirm that current.x_moogs_incident_m_moogsoft_incident_id is a valid field before attempting to process its value. You might also add some logging for while you're testing.

 

Something like this:

 

var assignmentGroupFieldName = '';
if (current.sys_class_name + '' == 'change_request') {
    assignmentGroupFieldName = 'assignment_group';
} else if (current.sys_class_name + '' == 'incident' || current.sys_class_name + '' == 'problem') {
    if (current.isValidField('x_moogs_incident_m_moogsoft_incident_id')) {
        // Log the value if the field is valid
        gs.info('x_moogs_incident_m_moogsoft_incident_id is valid. Value: ' + current.x_moogs_incident_m_moogsoft_incident_id);

        if (current.x_moogs_incident_m_moogsoft_incident_id.toLowerCase() != 'automation') {
            assignmentGroupFieldName = 'support_group';
        } else {
            // Do something
        }
    } else {
        // Log that the field is not valid
        gs.info('x_moogs_incident_m_moogsoft_incident_id is not a valid field on the current record.');
        // Do something
    }
}

 

 

After debugging, you can leverage short-circuit evaluation for cleaner code, if you like:

 

    var assignmentGroupFieldName = '';
    if (current.sys_class_name + '' == 'change_request') {
        assignmentGroupFieldName = 'assignment_group';
    } else if (current.sys_class_name + '' == 'incident' || current.sys_class_name + '' == 'problem') {
    if (current.isValidField('x_moogs_incident_m_moogsoft_incident_id') &&
        current.x_moogs_incident_m_moogsoft_incident_id.toLowerCase() != 'automation') {
        assignmentGroupFieldName = 'support_group';
    } else {
        // Do something
    }
}

 

 

Next, you should avoid using hard-coded sys_id. It is good practice to create a system property and store the sys_id there:

 

var assignmentGroupFieldName = '';
if (current.sys_class_name + '' == 'change_request') {
    assignmentGroupFieldName = 'assignment_group';
} else if (current.sys_class_name + '' == 'incident' || current.sys_class_name + '' == 'problem') {
    if (current.isValidField('x_moogs_incident_m_moogsoft_incident_id') &&
        current.x_moogs_incident_m_moogsoft_incident_id.toLowerCase() != 'automation') {
        assignmentGroupFieldName = 'support_group';
    } else {
        // Check the system property for the default assignment group sys_id
        var defaultAssignmentGroupSysId = gs.getProperty('x_moogs_incident_m.default_assignment_group');

        // Validate that the sys_id corresponds to an existing sys_user_group record
        if (defaultAssignmentGroupSysId) {
            var groupGR = new GlideRecord('sys_user_group');
            if (groupGR.get(defaultAssignmentGroupSysId)) {
                current.assignment_group = defaultAssignmentGroupSysId;
            } else {
                gs.info('Default assignment group sys_id from property x_moogs_incident_m.default_assignment_group is not valid.');
            }
        } else {
            gs.info('System property x_moogs_incident_m.default_assignment_group is not set.');
        }
    }
}

 

 

Finally, you should be be aware that the first if statement below could set assignment group, so you should consider if you need to add gs.nil(current.assignment_group) && before !gs.nil(current.cmdb_ci) there.

 

    if (!gs.nil(current.cmdb_ci) && current.cmdb_ci.changesFrom('')) {
        assignmentGroupFieldName = populateAssignmentGroupFieldName(current.sys_class_name + '', 'ci') || assignmentGroupFieldName;
        current.assignment_group = current.cmdb_ci[assignmentGroupFieldName];
    }

    if (gs.nil(current.assignment_group) && !gs.nil(current.service_offering) && current.service_offering.changesFrom('')) {
        assignmentGroupFieldName = populateAssignmentGroupFieldName(current.sys_class_name + '', 'service_offering') || assignmentGroupFieldName;
        current.assignment_group = current.service_offering[assignmentGroupFieldName];
    }

 

View solution in original post

10 REPLIES 10

Try the code samples from my previous reply. Start by replacing the relevant code section with the code I provided after “Something like this:” and then share the logs here.

Hi @Sheldon  Swift thanks a lot for pointing out 

 

in this code now the SYS ID via system property is absolutely working 

however now the IF conditions where support group to be assigned is not working 😞

 

Thanks

Can you share your updated code, as well as any logs?

Hi @Sheldon  Swift 

here is the updated code:  Its Before -Insert/Update Business Rule . Thanks

 

(function executeRule(current, previous /*null when async*/ )
{
   
var assignmentGroupFieldName = '';
if (current.sys_class_name + '' == 'change_request')
 {
    assignmentGroupFieldName = 'assignment_group';
 } else if (current.sys_class_name + '' == 'incident' || current.sys_class_name + '' == 'problem')
  {
    if (current.x_moogs_incident_m_moogsoft_incident_id.toLowerCase() != 'automation')
         {
        assignmentGroupFieldName = 'support_group';
         }
    else
     {
        // Check the system property for the default assignment group sys_id
           var defaultAssignmentGroupSysId = gs.getProperty('x_moogs_incident_m.default_assignment_group');

        // Validate that the sys_id corresponds to an existing sys_user_group record
        if (defaultAssignmentGroupSysId) {
            var groupGR = new GlideRecord('sys_user_group');
            if (groupGR.get(defaultAssignmentGroupSysId)) {
                current.assignment_group = defaultAssignmentGroupSysId;
            } else {
                gs.info('Default assignment group sys_id from property x_moogs_incident_m.default_assignment_group is not valid.');
            }
        } else {
            gs.info('System property x_moogs_incident_m.default_assignment_group is not set.');
        }
      }
    }

})(current, previous);

Try this:

(function executeRule(current, previous /*null when async*/ ) {
    /*
    This business rule will get executed for incident/problem/change on insert and update, if Assignment Group is empty and CI/ServiceOffering changing from empty value.
    */

    var assignmentGroupFieldName = '';
    if (current.sys_class_name + '' == 'change_request') {
        assignmentGroupFieldName = 'assignment_group';
    } else if (current.sys_class_name + '' == 'incident' || current.sys_class_name + '' == 'problem') {
        if (current.isValidField('x_moogs_incident_m_moogsoft_incident_id')) {
            // Log the value if the field is valid
            gs.info('x_moogs_incident_m_moogsoft_incident_id is valid. Value: ' + current.x_moogs_incident_m_moogsoft_incident_id);
            if (current.x_moogs_incident_m_moogsoft_incident_id.toLowerCase() != 'automation') {
                assignmentGroupFieldName = 'support_group';
            } else {
                // Check the system property for the default assignment group sys_id
                var defaultAssignmentGroupSysId = gs.getProperty('x_moogs_incident_m.default_assignment_group');

                // Validate that the sys_id corresponds to an existing sys_user_group record
                if (defaultAssignmentGroupSysId) {
                    var groupGR = new GlideRecord('sys_user_group');
                    if (groupGR.get(defaultAssignmentGroupSysId)) {
                        current.assignment_group = defaultAssignmentGroupSysId;
                    } else {
                        gs.info('Default assignment group sys_id from property x_moogs_incident_m.default_assignment_group is not valid.');
                    }
                } else {
                    gs.info('System property x_moogs_incident_m.default_assignment_group is not set.');
                }
            }
        } else {
            // Log that the field is not valid
            gs.info('x_moogs_incident_m_moogsoft_incident_id is not a valid field on the current record.');
            // Do nothing
        }
    }

    if (!gs.nil(current.cmdb_ci) && current.cmdb_ci.changesFrom('')) {
        assignmentGroupFieldName = populateAssignmentGroupFieldName(current.sys_class_name + '', 'ci') || assignmentGroupFieldName;
        gs.info('Populate Assignment Group based on CI/SO - Setting assignment group to current.cmdb_ci[' + assignmentGroupFieldName + ']');
        current.assignment_group = current.cmdb_ci[assignmentGroupFieldName];
    }

    if (gs.nil(current.assignment_group) && !gs.nil(current.service_offering) && current.service_offering.changesFrom('')) {
        assignmentGroupFieldName = populateAssignmentGroupFieldName(current.sys_class_name + '', 'service_offering') || assignmentGroupFieldName;
        gs.info('Populate Assignment Group based on CI/SO - Setting assignment group to current.service_offering[' + assignmentGroupFieldName + ']');
        current.assignment_group = current.service_offering[assignmentGroupFieldName];
    }

})(current, previous);

 

If it's still not working as expected, share the logs here. I won't be able to help further without the logs.