Inbound Action help: need to extract the next line after a specific word in email body

booher04
Tera Guru

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"

1 ACCEPTED SOLUTION

Nick Parsons
Mega Sage

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"

 

View solution in original post

3 REPLIES 3

Nick Parsons
Mega Sage

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"

 

Ankur Bawiskar
Tera Patron
Tera Patron

@booher04 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@booher04 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader