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.

Record producer script to assign an incident to one of two groups based on a multiple choice varable

Jason H1
Tera Contributor

Hi, 

I'm trying to have a record producer create an incident for one of two assignment groups based on the selection from a multiple-choice variable called phone_line_type. Here is the code:

 

(function () {
// Get the value of the 'phone_line_type' field
var phoneLineType = producer.phone_line_type;

// Define the sys_ids for the assignment groups
var assignmentGroup1 = 'sys_id_of_group_1';
var assignmentGroup2 = 'sys_id_of_group_2';

// Check if the assignment groups exist
var group1 = new GlideRecord('1f0ee795137867808b0d75c36144b018');
var group2 = new GlideRecord('2d993e78dbea99108d644641ba961926');
if (!group1.get(assignmentGroup1) || !group2.get(assignmentGroup2)) {
gs.addErrorMessage('One or both assignment groups do not exist. Please check the sys_ids.');
return;
}

// Create a new incident record
var incident = new GlideRecord('incident');
incident.initialize();

// Assign the incident to one of two groups based on the value of 'phone_line_type'
if (phoneLineType == 'analog_line') {
incident.assignment_group = assignmentGroup1;
} else if (phoneLineType == 'teams_call_line') {
incident.assignment_group = assignmentGroup2;
} else {
gs.addErrorMessage('Invalid phone line type. Please check the value of the phone_line_type field.');
return;
}

// Other incident fields can be set here, e.g.,
// incident.short_description = '...';
// incident.description = '...';

// Insert the new incident record
incident.insert();
})();
When I run this code, I see two errors: one is Record not found the other is Failed to create an incident with no Assignment Group 
 
Any idea where I need to improve here? I feel this should be easy to do.

 

 

1 ACCEPTED SOLUTION

Kishor O
Tera Sage

@Jason H1  Please try with the below code

var phoneLineType = producer.phone_line_type
if (phoneLineType == 'teams_call_line'); {
    current.assignment_group = '548cd3b48728a910c062fc48dabb35a1'; //sysid of assignment group
}
else if(phoneLineType == 'teams_call_line');{
 current.assignment_group = '788cd3b48728a910c062fc48dabb35a1'; //sysid of assignment group
}

View solution in original post

5 REPLIES 5

Maddysunil
Kilo Sage

@Jason H1 

    • Use the addQuery method to check if the assignment group records exist before attempting to get them. If the records don't exist, handle it accordingly.
    •  Make sure that the field names you are using for phone_line_type and assignment_group are correct
    • Please try the updated code and make sure to replace 'sys_id_of_group_1' and 'sys_id_of_group_2' with the actual sys_ids of your assignment groups

 

(function () {
    // Get the value of the 'phone_line_type' field
    var phoneLineType = producer.phone_line_type;

    // Define the sys_ids for the assignment groups
    var assignmentGroup1 = 'sys_id_of_group_1';
    var assignmentGroup2 = 'sys_id_of_group_2';

    // Check if the assignment groups exist
    var group1 = new GlideRecord('sys_user_group');
    group1.addQuery('sys_id', assignmentGroup1);
    group1.query();
    
    var group2 = new GlideRecord('sys_user_group');
    group2.addQuery('sys_id', assignmentGroup2);
    group2.query();

    if (!group1.next() || !group2.next()) {
        gs.addErrorMessage('One or both assignment groups do not exist. Please check the sys_ids.');
        return;
    }

    // Create a new incident record
    var incident = new GlideRecord('incident');
    incident.initialize();

    // Assign the incident to one of two groups based on the value of 'phone_line_type'
    if (phoneLineType == 'analog_line') {
        incident.setValue('assignment_group', assignmentGroup1);
    } else if (phoneLineType == 'teams_call_line') {
        incident.setValue('assignment_group', assignmentGroup2);
    } else {
        gs.addErrorMessage('Invalid phone line type. Please check the value of the phone_line_type field.');
        return;
    }

    incident.insert();
})();

 

Kindly mark helpful/accepted if it helps you.

Thanks