Auto Assigning incident Based on Email body Keywords to Assigned to

manumanoj
Tera Contributor

Hi All

 

I am trying to Assign the incident to Assigned to 

let say in email body @XYZ  look in to maintenance which creates an incident Assigned to xyz

in my condition Inbound actions 

manumanoj_0-1716450028052.png

here in the incident configured that based on caller location is auto populated and based on location Assignment group would be auto-populated my requirement that in the body updates like Hi @xyz desk light not working please look in to it So that Assigned to auto update to xyz



8 REPLIES 8

Mahathi
Mega Sage
Mega Sage

Hi @manumanoj ,

You can get the email body, use the below script : 

var email_to_str = email.from.toString();  
email_to_str = email_to_str.toLowerCase();
 
Get the answer and query the user table to get the user details and put it to assigned_to field.

Please refer to the article for a detailed step by step guidance : 

https://www.servicenow.com/community/developer-forum/how-to-trim-data-from-received-email-s-body/m-p...

Please accept solution & 👍Helpful if it helped answer your query.

Thanks,
Mahathi


 

Hi @Mahathi 

Can you please more specific on the this 
for example email body : Hi @xyz issue with Adobe software Please look in to it 
it has to assigned to xyz 

Hi @manumanoj ,

You can try something like below: 


var emailBody = email.body_text;

// Find the word "hi" and get the substring after it
var hi = emailBody.toLowerCase().indexOf("hi");
if (hi!== -1) {
var afterHi = emailBody.substring(hiIndex + 2).trim();
// Extract the username or email from the remaining string
// Assuming the format is "hi username" or "hi user@example.com"
var usernameOrEmail = afterHi.split(" ")[0];

// Query the sys_user table for the user
var userGR = new GlideRecord('sys_user');
userGR.addQuery('user_name', usernameOrEmail).addOrCondition('email', usernameOrEmail);
userGR.query();

// Check if the user exists
if (userGR.next()) {
var userSysId = userGR.sys_id;
// Update the "Assigned to" field in the incident
current.assigned_to = userSysId;
current.update();
}
}

Check what will be displayed, the username or email and modify the code accordingly.

Please mark helpful and accept as solution if this helped answer your query.

Thanks,
Mahathi

Mahathi
Mega Sage
Mega Sage

 @manumanoj : Any luck?