- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2024 11:36 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2024 11:48 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2024 11:48 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2024 11:48 PM
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 !!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 01:14 AM
Hi @Puneet4418 ,
You can refer to below article ,
Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Swathi Sarang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 01:39 AM
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
Regards,
Piyush Sain