Parse email body text
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I have the following information in an email and i need to extract it to put in variable fields of a Catalog Request:
Name
Joe Customer
jcustomer@yahoo.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi,
This is a very common pattern in ServiceNow: you receive a semi-structured email and want to extract values (Name, Email) into Catalog Item variables via an Inbound Email Action + Record Producer.
Recommended
Email → Inbound Email Action → Record Producer → Catalog Variables
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks
Nayan Patel
IT ServiceNow Consult, ServiceNow ArchX
If my response has resolved your query, please mark it Helpful by giving it a thumbs up and Accept the Solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
You can parse the body using simple string operations in the inbound email action-
var body = email.body_text;
// Extract Name
var nameMatch = body.match(/Name\s*([\s\S]*?)\n\n/);
var name = nameMatch ? nameMatch[1].trim() : "";
// Extract Email
var emailMatch = body.match(/Email\s*([\s\S]*?)\n/);
var userEmail = emailMatch ? emailMatch[1].trim() : "";
// Set variables on the RITM
current.variables.u_name = name;
current.variables.u_email = userEmail;
Regards,
Nayan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
In your Inbound email action, you need to write a script to map email data to Catalog request.
sample script:
(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('<sys_id of your catalog item>');
cart.setVariable(item, 'requested_for', '<sys_id of requested for>');
cart.setVariable(item, 'request_short_description', email.subject.toString());
cart.setVariable(item, 'request_description', email.body_html);
cart.setVariable(item, 'request_type', 'others');
var rc = cart.placeOrder();
var ritmRec = new GlideRecord("sc_req_item");
ritmRec.addQuery("request", rc.sys_id);
ritmRec.query();
if (ritmRec.next()) {
ritmRec.short_description = email.subject.toString();
ritmRec.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
ritmRec.contact_type = "email";
ritmRec.update();
}
}

