Need to set the assignment group of incident based on the subject line

Mansi roy
Tera Contributor

Hello,

 

Could anyone please help me on this as i need to  set the assignment group based on the subject line of incident.

 

I have created one table and there i stored subject and assignment group.I have created the inbound action for 'Create Incident'.

 

Below is the code.

 

current.caller_id = gs.getUserID();
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
current.short_description = email.subject;

 

current.category = "Hardware";
current.incident_state = IncidentState.NEW;
current.notify = 2;
current.contact_type = "email";

 

if (email.body.assign != undefined)
   current.assigned_to = email.body.assign;

 

if (email.importance != undefined) {
   if (email.importance.toLowerCase() == "high") {
        current.impact = 1;
        current.urgency = 1;
   }
}

 

   var subject = email.subject.toLowerCase(); 
var mapping = new GlideRecord('u_subject_group_mapping'); 
 mapping.addQuery('u_keyword', 'LIKE', subject);
mapping.query(); 
var assignmentGroup = null;

 

if (mapping.next()) {

 

assignmentGroup = mapping.u_assignment_group; 

 

if (assignmentGroup) {

 

var incident = new GlideRecord('incident');

 

if (incident.get(email.reference)) {

 

incident.assignment_group = assignmentGroup;

 

incident.update();
}
 else {

 

gs.info ('No assignment group found for the subject: ' + subject);
}
}
current.insert();
 
 
The incident is getting created but the assignment group is not getting set.Can anyone please me on this why the assignment group is not getting set.
 
Regards,
Priti
1 ACCEPTED SOLUTION

Hi @Mansi roy 

Try bellow refined script once and test: 

// Set the caller and basic details from the email
current.caller_id = gs.getUserID();
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
current.short_description = email.subject;

// Set incident state, notify and contact type
current.incident_state = IncidentState.NEW;
current.notify = 2;
current.contact_type = "email";

// Assign incident if specified in the email body
if (email.body.assign != undefined) {
    current.assigned_to = email.body.assign;
}

// Set priority based on email importance
if (email.importance != undefined) {
    current.priority = 1;
}

// Normalize the subject for searching
var subject = email.subject.toLowerCase();

// Query the subject group mapping table
var mapping = new GlideRecord('u_subject_group_mapping');
mapping.addQuery('u_subject', subject);
mapping.query();

if (mapping.next()) {
    // If a mapping is found, set the assignment group
    var assignmentGroup = mapping.u_assignment_group;
    if (assignmentGroup) {
        current.assignment_group = assignmentGroup;
    } else {
        gs.info('No assignment group found for the subject: ' + subject);
    }
} else {
    gs.info('No mapping found for the subject: ' + subject);
}

// Insert the new incident record
current.insert();

Also keep consider the point mentioned by Ankur, here I am assuming 'assignment group' field is reference field in your custom table.

I hope my answer helps you to resolve your issue, if yes mark my answer correct & helpful.

thank you

rajesh. 

 

View solution in original post

22 REPLIES 22

Hello @Rajesh Chopade1.

 

I am not getitng the value.Could you please chekc once the script and let me know where i am doing wrong.

 

//  Note: current.opened_by is already set to the first UserID that matches the From: email address

current.caller_id = gs.getUserID();
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
current.short_description = email.subject;

//current.category = "Hardware";
current.incident_state = IncidentState.NEW;
current.notify = 2;
current.contact_type = "email";

if (email.body.assign != undefined)
   current.assigned_to = email.body.assign;

if (email.importance != undefined) {
    current.priority =1;
   //if (email.importance.toLowerCase() == "high") {
        //current.impact = 1;
        //current.urgency = 1;
        current.priority =1;
   //}
}

  // var subject = email.subject.toLowerCase();
var subject = email.subject();
var mapping = new GlideRecord('u_subject_group_mapping');

//var assignmentGroup = null; // Check if we have a matching record

//var assignmentGroup='';
var assignmentGroup = mapping.u_assignment_group;
if (mapping.next()) {
    gs.info(assignmentGroup);

assignmentGroup = mapping.u_assignment_group;

} // If a mapping was found, assign the incident to the group

if (assignmentGroup!='') {

// var incident = new GlideRecord('incident');

// if (incident.get(email.reference)) {

// incident.assignment_group = assignmentGroup;
current.assignment_group = assignmentGroup;
//current.update();

//incident.update();
//}
//  else {

// gs.info ('No assignment group found for the subject: ' + subject);
// }
}
current.insert();

@Mansi roy 

is that custom table field which holds group a reference or string type?

if it holds group name and is of type string then try this line

current.setDisplayValue('assignment_group',assignmentGroup);

if it's reference to group table this line should work fine.

current.assignment_group = assignmentGroup;

What debugging have you done so far? Is the GlideRecord working fine?

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

Hello @Ankur Bawiskar,@Rajesh Chopade1 

 

I tried the below script but it is not working.in my table both are string type field(assignment group and keyword).

 

current.caller_id = gs.getUserID();
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
current.short_description = email.subject;

current.category = "Hardware";
current.incident_state = IncidentState.NEW;
current.notify = 2;
current.contact_type = "email";

if (email.body.assign != undefined)
   current.assigned_to = email.body.assign;

if (email.importance != undefined) {
    current.priority =1;
   //if (email.importance.toLowerCase() == "high") {
        //current.impact = 1;
        //current.urgency = 1;
        current.priority =1;
   //}
}
var subject = email.subject.toLowerCase();
var mapping = new GlideRecord('u_subject_group_mapping');
mapping.addQuery('u_keyword', subject);
mapping.query();

if (mapping.next()) {
    // If a mapping is found, set the assignment group
    var assignmentGroup = mapping.u_assignment_group;
    if (assignmentGroup) {
        current.assignment_group = assignmentGroup;
        //current.setDisplayValue('assignment_group',assignmentGroup);
    } else {
        gs.info('No assignment group found for the subject: ' + subject);
    }
} else {
    gs.info('No mapping found for the subject: ' + subject);
}
 
Incident is getting created but assignment still not getting set.
 
Regards,
Mansi

@Mansi roy 

is it getting inside this IF?

if (mapping.next()) {

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello @Ankur Bawiskar,

 

It is not going inside if statement. What wrong i am doing could you please help me.