How to parse a line from email xml and assign to a field

George Taylor
Kilo Contributor

Hi team, 

I've created an Inbound action that creates a task whenever an email is received with certain words are in the body text and got it working. But unable to determine how to parse a particular line that can then be assigned to a variable

For example if I receive the email below and have a task created with this info

Hello, 

<IP_Address> 127.0.0.1 </IP_Address>

<Name> Joe Smith </Name>

<Title> Employee </Title>

 

Is there a way I can grab, say the IP Address as well as Title and assign them each to their own task variable, say u_ip_address and u_user_title? Trying to make it when a task is created I can have these fields auto-filled

Everything I've seen shows how to match but not anything past. 

Thanks in advance!

8 REPLIES 8

Ian Mildon
Tera Guru

You can use the "split" function such as what I'm using here to extract a value from the email body to add to the Short Description field and extracting the Cluster Name value to insert in the "ci" reference field.

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
	// Implement email action here
	current.caller_id = "b47e331c1b420d1068880d85624bcb9e"; //* user profile with email = Application, NetApp (NetApp) - ocumiq@unch.unc.edu
    current.u_best_contact_method = "email";
    current.u_best_contact_name = "DoNotReply";
    current.u_room_suite_information = 'n/a'; //* needed to add for P2 
    current.u_loc_conf = true; //* needed to add for P2
    current.business_service = "8068e708dbc5be00deda327e9d961920"; //* Epic@UNC
	current.contact_type = "Email";
    current.category = "Hardware";
    current.subcategory = "Server";
	current.impact = 2;
    current.urgency = 1;
	// current.cmdb_ci = ; //* get from email below
    current.assignment_group = "8634b5badb05b240ec5c3c00ad961918"; //* ISD Team Storage
    current.u_department = "fd6ccf3cdb05be00ec5c3c00ad96196f"; //* File Storage
    current.short_description = email.subject + " Volume (" +volumeInfo+ ")"; //* get from email and split value
	current.description = email.body_text;
	
    var getCluster = email.body_text;
    var clusterName = [];
    clusterName = getCluster.toString().split("Cluster Name  - "); //! there are two spaces between Name and -
    var spl = clusterName[1].split("\n"); //* split at carriage return
    current.cmdb_ci.setDisplayValue(spl[0].toLowerCase()); //* set the extracted name value

    var getVolume = email.body_text;
    var volumeName = [];
    volumeName = getVolume.toString().split("volumes/"); //* email body Source Details line
    var splVol = volumeName[1].split("\n"); //* split at carriage return
    var volumeInfo = splVol[0].toString(); //* extracted volume data (number string)

	current.insert();
})(current, event, email, logger, classifier);

Looking at you code I understand the top portion where it defines the fields for your task/incident. May need your assistance on the bottom two, specifically the one with clusterName. 

In my example would I write it similar to below:


var getCluster = email.body_text;
var clusterName = [];
clusterName = getCluster.toString().split("IP Address - "); 
var spl = clusterName[1].split("\n"); //* split at carriage return
current.cmdb_ci.setDisplayValue(spl[0].toLowerCase()); //* set the extracted name value

 

And if so would I then create a line blow that reads

current.u_ip_address = spl

So overall would it look like this?

var getCluster = email.body_text;
var clusterName = [];
clusterName = getCluster.toString().split("IP Address - "); 
var spl = clusterName[1].split("\n"); //* split at carriage return
current.cmdb_ci.setDisplayValue(spl[0].toLowerCase()); //* set the extracted name value
current.u_ip_address = spl

The main thing to remember with the split function is that first you want to define where you want to start from: in the "getVolume" section that would be after "volumes/" in the body text.

Then you want to get where you want to stop: in the same example, that would be the carriage return and this becomes the [1] as it's after the value you want.

The output in my example then becomes the [0] as I am wanting the value that is before the second split.

var getIPaddress = email.body_text;
var ipAddress = [];
ipAddress = getIPaddress.toString().split("IP Address - "); 
var spl = ipAddress[1].split("\n"); //* split at carriage return
current.u_ip_address = spl[0].toString();

 

I think I got it, would I just enter it twice? Once for each entry like such?

var getIPaddress = email.body_text;
var ipAddress = [];
ipAddress = getIPaddress.toString().split("IP Address - ");
var spl = ipAddress[1].split("\n"); //* split at carriage return
current.u_ip_address = spl[0].toString();

var getIPaddress = email.body_text;
var ipAddress = [];
ipAddress = getIPaddress.toString().split("Title - ");
var spl = ipAddress[1].split("\n"); //* split at carriage return
current.u_ip_address = spl[0].toString();