- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2024 01:41 PM - edited 02-26-2024 01:45 PM
Hi All,
I have an ask to create a RITM whenever an email is received in ServiceNow and the data needs to be parsed, and pushed to the variables in the catalog that creates the RITM.
Below is how the mail body is received in SNOW:
Dear Service Desk,
Our HR system has indicated that Simon Beggs is due to leave the company on 16-02-2024.
AD Login - op-simonb1
Email Address - Simon.Beggs@norbrook.co.uk
Location - Norbrook Laboratories Ltd
Area - Armagh Road
Thanks,
IT Services
Till now, I have used regex to parse the data into the fields on RITM. The inbound script looks as below:
(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
// Implement email action here
var text = email.body_text;
gs.log('text is: ' + text);
// Define regular expressions to match the patterns
var adLoginPattern = /AD Login - (.+)/;
var emailPattern = /Email Address - (.+)/;
var locationPattern = /Location - (.+)/;
var areaPattern = /Area - (.+)/;
var namePattern = /indicated that (.+) is due/;
var DepartDatePattern = /([A-Za-z0-9]+(-[A-Za-z0-9]+)+)/i;
// Function to extract values using regular expressions
function extractValue(text, pattern) {
var match = text.match(pattern);
gs.log('match is: ' + match);
if (match) {
// Remove asterisks from the captured group (value) if found
var value = match[1].replace(/^\*|\*$/g, ''); // This regex removes asterisks from start and end of the string
return value.trim(); // Trim any extra whitespace
} else {
return null; // Return null if not found
}
}
// Extract values using regular expressions
var adLogin = extractValue(text, adLoginPattern);
var email = extractValue(text, emailPattern);
var location = extractValue(text, locationPattern);
var area = extractValue(text, areaPattern);
var name = extractValue(text, namePattern);
var depdate = extractValue(text, DepartDatePattern);
gs.log('ad login :' + adLogin);
gs.log('email is: ' + email);
gs.log('location is: ' + location);
gs.log('area is :' + area);
gs.log('name is :' + name);
gs.log('depart date: ' + depdate);
current.u_adlogin = adLogin;
current.u_mailid = email;
current.u_leaver_location = location;
current.u_area = area;
current.u_ad_login = name;
current.u_departure_date = depdate;
// current.u_departure_date.setDisplayValue(depdate);
current.insert();
// Now set catalog variables with parsed values
var ritmVariables = current.variables;
if (ritmVariables) {
ritmVariables.staff_member_name = name;
ritmVariables.date_of_departure = depdate;
ritmVariables.email_address = email;
ritmVariables.ad_login = adLogin;
ritmVariables.location = location;
ritmVariables.area = area;
ritmVariables.update();
}
})(current, event, email, logger, classifier);
- I am able to parse the data from mail body and push it in the fields as below in the newly created RITM.
Ask is - instead of the above fields (ad login, location, mail, area, departure date), the parsed data from mail needs to be pushed into the variables of a catalog item and then create an RITM from that catalog automatically. Or create an RITM and push the data into the variable, either way.
Below is the catalog item with variables:
So basically - I need an automated flow where, whenever an email is received in SNOW, the mail body needs to be parsed and pushed to the catalog item as above and create an RITM automatically.
I got the parse data in the fields but not sure how to push that data into the variables on RITM. The catalog has a cart section in the RITM where the data needs to be visible, something like below:
Or any other path to complete the ask other than one I mentioned above is appreciated! I came across few threads on forum as this one.
But it didn't helped. The RITM was not created when I use the script from the mentioned thread.
I am also not aware of CART API and CARTJS on how to use it.
Can anyone help on how to achieve this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2024 04:37 AM
@swapnil15 : This can be achieved by using flow designer.
1. Set trigger as inbound email and set condition as required.
2. There is ServiceNow flow action provided by ServiceNow on share "Parse Email body text" Attached word document for steps to download it for reference.
3. In flow , there is provision to create flow variables -click on three dots at right upper corner- flow variables option- Using this option you can create variables to store values from email body text after parsing. (though script or dot walking should be available). For every value create flow variable which will store respective variable required on this request.
4. then use "Submit catalog item request" step- pass your catalog item and in variables pass the above each flow variables by dot walking. This will create request automatically.
Give a try. Hope it helps. Kindly mark helpful/accepted if it helps.
Regards,
Priyanka Salunke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 03:20 AM
You need to make custom action for it. Click on New -> Action. Inside the action, make use of Script Step to run your script. Pass the output of this action as an input to Submit Catalog Item Request action.
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 04:37 AM
Hi Swapnil
see below what I'm using in an inbound action
//Leaver Report
if (email.subject.toString() == 'Leaver Report') {
if (getSize(sys_email.sys_id) > 243) {
createRequest();
}
}
function createRequest() {
var cart = new sn_sc.CartJS();
var item = {
'sysparm_id': '691753ed1b617f00067cca217e4bcb8a',
'sysparm_quantity': '1',
'variables': {
'recovery_result': 'No equipment to recover',
'requested_for': 'c5b8f53d1b7be810df9e2fc5604bcb82',
'email_subject': email.subject.toString(),
}
};
var cartDetails = cart.addToCart(item);
var requestDetails = cart.submitOrder(cartDetails);
var grRITM = new GlideRecord('sc_req_item');
var RITMsysid = '';
grRITM.addQuery('request', requestDetails.request_id.toString());
grRITM.query();
if (grRITM.next()) {
sys_email.target_table = "sc_req_item";
RITMsysid = grRITM.getValue('sys_id');
grRITM.short_description = email.subject.toString();
grRITM.description = email.body_text;
grRITM.update();
sys_email.instance = RITMsysid;
sys_email.update();
gs.log('SB - ritm' + RITMsysid);
}
GlideSysAttachment.copy('sys_email', sys_email.sys_id, 'sc_req_item', RITMsysid);
}
function getSize(emailID) {
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", emailID);
gr.query();
if (gr.next()) {
return gr.size_bytes;
}
}
event.state = 'stop_processing';
})(current, event, email, logger, classifier);
note the section that contains the values being pushed into the variables that are on that specific item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 05:38 AM - edited 02-27-2024 05:43 AM
@scott barnard1 , I have used the script provided by you. Changed the sys id of catalog and changed the variables value as below:
Triggered a mail to servicenow but the inbound action didn't create a RITM.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 05:49 AM
if (email.subject.toString() == 'Leaver Report') {
if (getSize(sys_email.sys_id) > 243) {
createRequest();
}
}
Hi Swapnil
if (email.subject.toString() == 'Leaver Report') {
if (getSize(sys_email.sys_id) > 243) {
createRequest();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 05:50 AM
No idea why that duplicated