We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Parse email body text

Karina Baublits
Tera Contributor

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

 

Email

jcustomer@yahoo.com

3 REPLIES 3

Nayan ArchX
Tera Guru

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

 

Nayan Mahato
Tera Guru

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

Tanushree Maiti
Giga Sage

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();
}

}

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: