Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Hi @Sheldon  Swift 

Thanks a million for your instant guidance.

I just replaced this line below:

assignmentGroupFieldName = 'support_group';

 

with

current.assignment_group = current.cmdb_ci.support_group;
 
Now it is working however not sure why above line specially did not. But it is working now as expected. Thanks Again