Setting field values from the email body ( Inbound Action)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2024 09:06 AM
Hello All,
I have an requirement where user will put User name (email Id or name) in the body of the email ( for eg ; User name xyz@gmail.com ). We have to set the user name as the caller of the Incident.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2024 09:21 AM
Hi @C_S3,
To set the user name as the caller of an incident in ServiceNow based on the email body, follow these steps:
1. Create an Inbound Email Action:
- Go to `System Policy > Email > Inbound Actions`.
- Click `New` to create a new Inbound Action.
2. Configure the Inbound Email Action:
- Name: Set it to "Set Caller from Email Body".
- Target table: Set to `Incident`
- Type: Choose `Create a Record` (or `Update a Record`).
3. Set the Condition (optional):
- Define any specific conditions for triggering this action.
4. Add Action Script:
- Use the following script to extract the user name from the email body and set it as the caller:
(function runAction(email, action, incident) {
var emailBody = email.body_text;
var emailRegex = /User name\s+(\S+)/i;
var match = emailBody.match(emailRegex);
if (match) {
var userName = match[1];
var user = new GlideRecord('sys_user');
if (user.get('email', userName) || user.get('user_name', userName)) {
incident.caller_id = user.sys_id;
} else {
gs.log('User not found: ' + userName);
}
} else {
gs.log('User name not found in email body');
}
incident.short_description = email.subject;
incident.description = emailBody;
})(email, action, incident);
5. Save and Test:
- Save the Inbound Email Action.
- Send a test email with the format "User name xyz@gmail.com" and verify the incident creation.
This script processes incoming emails, extracts the user name or email, and sets it as the caller of the incident.
Thank you, please make helpful if you accept the solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2024 09:21 AM
Hi @C_S3 ,
To achieve this requirement in ServiceNow, you will need to create a mail script to process incoming emails, extract the user's email address from the email body, and set the extracted user as the caller of the incident. I've provided the steps below follow them, and see if it helps:
Create an Inbound Email Action:
- Navigate to System Policy > Email > Inbound Actions.
- Click New to create a new inbound email action.
- Configure the inbound email action:
- Name: "Set Caller from Email Body".
- Target Table: "Incident".
- When to Run: Define your conditions (e.g., when the subject contains "New Incident").
- Action Type: "Record Action".
2. Script to Extract Email and Set Caller:
- Add the following script in the Script field:
- Feel free to customize the script according to you.
(function process(inboundEmail, emailAction, event) {
// Get the email body text
var emailBody = inboundEmail.body_text;
// Regular expression to extract email address
var emailRegex = /User name\s+([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/i;
var emailMatch = emailBody.match(emailRegex);
if (emailMatch && emailMatch[1]) {
var userEmail = emailMatch[1].trim();
// Query sys_user table to find the user by email
var userGR = new GlideRecord('sys_user');
userGR.addQuery('email', userEmail);
userGR.query();
if (userGR.next()) {
// Set the caller_id to the found user
current.caller_id = userGR.sys_id;
// Log a message for debugging
gs.log("Caller set to user: " + userGR.name + " (" + userEmail + ")", 'Inbound Email Action');
} else {
gs.log("No user found with email: " + userEmail, 'Inbound Email Action');
}
} else {
gs.log("No valid email address found in email body.", 'Inbound Email Action');
}
// Insert or update the incident record
current.insert();
// This is completly optional, Sending back email response.
var response = new GlideEmailOutbound();
response.setSubject("Incident Created");
response.setBody("An incident has been created and the caller has been set based on the email provided.");
response.setTo(inboundEmail.origemail);
response.send();
})(inboundEmail, emailAction, event);
Thanks,
Hope this helps.
If my response proves helpful please mark it helpful and accept it as solution to close this thread.