update caller_id field through email body inbound action

Puneet4418
Tera Contributor

Hi,

 

I have a requirement to create an incident through inbound action and populate the incident fields according to the details provided in email body.

I am able to populate most of the fields but facing issue with caller_id (reference field).

 

The requirement is to fetch the user in caller_id field according to the Email Address mentioned in the email body.

below is the email format:

 

Email Address : xyz@test.com

department : test

location: test

1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@Puneet4418 

I think you'll need to fetch the user based on the email address provided in the email body and then set the caller_id field of the incident record with the fetched user's sys_id.

You can use below template

 

// Get the email body
var emailBody = email.body_text;

// Extract the email address from the email body
var emailRegex = /Email Address\s*:\s*([^\n]+)/i;
var emailMatch = emailBody.match(emailRegex);
var emailAddress = emailMatch ? emailMatch[1].trim() : '';

// Find the user based on the email address
var userGr = new GlideRecord('sys_user');
userGr.addQuery('email', emailAddress);
userGr.query();

if (userGr.next()) {
    // If user found, set caller_id field of the incident to the user's sys_id
    current.caller_id = userGr.sys_id;
} else {
    // If user not found, handle the case as per your requirement
    gs.log("User with email " + emailAddress + " not found.", "Inbound Email Action");
}

// Set other fields of the incident as per the email body
// current.short_description = "Incident created based on email";
// current.description = "Description provided in the email";

current.update();

 

  

Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

4 REPLIES 4

Maddysunil
Kilo Sage

@Puneet4418 

I think you'll need to fetch the user based on the email address provided in the email body and then set the caller_id field of the incident record with the fetched user's sys_id.

You can use below template

 

// Get the email body
var emailBody = email.body_text;

// Extract the email address from the email body
var emailRegex = /Email Address\s*:\s*([^\n]+)/i;
var emailMatch = emailBody.match(emailRegex);
var emailAddress = emailMatch ? emailMatch[1].trim() : '';

// Find the user based on the email address
var userGr = new GlideRecord('sys_user');
userGr.addQuery('email', emailAddress);
userGr.query();

if (userGr.next()) {
    // If user found, set caller_id field of the incident to the user's sys_id
    current.caller_id = userGr.sys_id;
} else {
    // If user not found, handle the case as per your requirement
    gs.log("User with email " + emailAddress + " not found.", "Inbound Email Action");
}

// Set other fields of the incident as per the email body
// current.short_description = "Incident created based on email";
// current.description = "Description provided in the email";

current.update();

 

  

Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Sohithanjan G
Kilo Sage

Hi @Puneet4418 , 

 

You can use the below script to populate the caller_id from email. If you are able to get the email address in a variable, you can use this script to populate caller_id.

var email_add = 'xyz@test.com';

var eUser = new GlideRecord('sys_user');
eUser.addQuery('email',email_add);
eUser.query();
if (eUser.next()){
var caller = eUser.sys_id;
}

//for updating the caller
current.caller_id=caller;
current.insert();

 

Please mark as Accepted as Solution & hit helpful !!! 

swathisarang98
Giga Sage

Hi @Puneet4418 ,

 

You can refer to below article ,

https://www.servicenow.com/community/developer-forum/get-email-address-from-body-of-inbound-email-an... 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

piyushsain
Tera Guru

Hi @Puneet4418 

You can get email id from the email body by using following regex code

var content = email.body_text;  

var emails = content.match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/ig);  

// Use the above varaiable to query user table and fetach the user and update in the required field