- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2025 01:01 PM
I have an inbound action working well for new emails but the FWD is not working. I need a way to extract/parse the string in the line after a specific line: App Name.
Here is the code I have that is working for NEW inbound actions but not FWD:
createRequest();
function createRequest() {
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('acfb58a73bec6e100d0f8c9c24e45aa0');
var emailBody = email.body_text;
var lines = emailBody.split('\n');
var firstLine = lines[27];
var appBody = firstLine.replace("App Name", "");
cart.setVariable(item, 'email_subject', email.subject);
cart.setVariable(item, 'email_body',email.body_text );
cart.setVariable(item, 'app_name', appBody);
gs.log ('Test: ' + email.subject + ': ' + email.body_text);
gs.log('APPBODY: ' + appBody);
var rc = cart.placeOrder();
var grRITM = new GlideRecord('sc_req_item');
grRITM.addQuery('request',rc.getValue('sys_id'));
grRITM.query();
if(grRITM.next()) {
grRITM.setValue('sys_created_by', ' Inbound Email Action');
grRITM.setValue('contact_type', 'email');
//grRITM.setValue('short_description', '"Multifactor Authentication Compliance"' + current.variables.app_name);
grRITM.setValue('description', email.body_text);
grRITM.setValue('assignment_group','bddd7a5497c681909419b4e3f153af0b');
//Add Attachment(s)
GlideSysAttachment.copy('sys_email', sys_email.sys_id, 'sc_req_item', grRITM.sys_id);
grRITM.update();
var emailGR = new GlideRecord("sys_email");
emailGR.addQuery("sys_id", sys_email.sys_id);
emailGR.query();
if(emailGR.next()){
emailGR.instance = grRITM.sys_id;
emailGR.target_table = "sc_req_item";
emailGR.update();
}
}
}
I need to be able to find "App Name" on the email body and extract the very next line as the email format has it like:
App Name:
"xxxxxxx"
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2025 04:51 PM - edited 04-14-2025 04:51 PM
You can get the index of the "App Name:" text from your lines array, and then get the next line by adding one to get your appName ("xxxxxxx") in your example:
var lines = emailBody.split('\n');
var appNameLine = lines.indexOf("App Name:");
var appName = lines[appNameLine + 1]; // "xxxxxxx"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2025 04:51 PM - edited 04-14-2025 04:51 PM
You can get the index of the "App Name:" text from your lines array, and then get the next line by adding one to get your appName ("xxxxxxx") in your example:
var lines = emailBody.split('\n');
var appNameLine = lines.indexOf("App Name:");
var appName = lines[appNameLine + 1]; // "xxxxxxx"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2025 08:08 PM
try this
createRequest();
function createRequest() {
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('acfb58a73bec6e100d0f8c9c24e45aa0');
var emailBody = email.body_text;
var lines = emailBody.split('\n');
var appBody = "";
for (var i = 0; i < lines.length; i++) {
if (lines[i].includes("App Name")) {
appBody = lines[i + 1].trim(); // Extract the line after "App Name"
break;
}
}
cart.setVariable(item, 'email_subject', email.subject);
cart.setVariable(item, 'email_body', email.body_text);
cart.setVariable(item, 'app_name', appBody);
gs.log('Test: ' + email.subject + ': ' + email.body_text);
gs.log('APPBODY: ' + appBody);
var rc = cart.placeOrder();
var grRITM = new GlideRecord('sc_req_item');
grRITM.addQuery('request', rc.getValue('sys_id'));
grRITM.query();
if (grRITM.next()) {
grRITM.setValue('sys_created_by', 'Inbound Email Action');
grRITM.setValue('contact_type', 'email');
grRITM.setValue('description', email.body_text);
grRITM.setValue('assignment_group', 'bddd7a5497c681909419b4e3f153af0b');
// Add Attachment(s)
GlideSysAttachment.copy('sys_email', sys_email.sys_id, 'sc_req_item', grRITM.sys_id);
grRITM.update();
var emailGR = new GlideRecord("sys_email");
emailGR.addQuery("sys_id", sys_email.sys_id);
emailGR.query();
if (emailGR.next()) {
emailGR.instance = grRITM.sys_id;
emailGR.target_table = "sc_req_item";
emailGR.update();
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 06:45 AM
Thank you for marking my response as helpful.
As per new community feature you can mark multiple responses as correct.
If my response helped please mark it correct as well so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader