Inbound Email A ction to Custom variable

Tyler Johnson
Tera Expert

Hello Guys, I was attempting to pull a specfic line after the last : that starts with 

"UniqueFailureID="  from the email body text. I tried the below code but i couldnt get it to populate after the incoming email. any suggestions to my code? 
 
var emailBody = email.body.text;
var regex = /UniqueFailureID=.*:([a-f0-9\-]+)$/m;
var match = emailBody.match(regex);
 
if (match && match[1]) {
    var uniqueFailureID = match[1];
    current.u_uniquefailureid = uniqueFailureID;
    current.update();
} else {
    gs.log("UniqueFailureID not found in the email body for incident: " + current.number, "Inbound Email Action AAC_Automation");
}
 
1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@Tyler Johnson 

Can you log the email body to inspect the content you getting in that, Also Depending on the format of your email body, you might need to adjust the regular expression. For instance, if there could be whitespace characters around the = sign or the : sign, you might want to make the expression more flexible.

Please try the updated code below:

 

var emailBody = email.body_text;
gs.info("Email Body: " + emailBody); // Log the email body to inspect its content

// Adjusted regular expression to be more flexible
var regex = /UniqueFailureID\s*=\s*.*:([a-f0-9\-]+)$/im;
var match = emailBody.match(regex);

if (match && match[1]) {
    var uniqueFailureID = match[1];
    current.u_uniquefailureid = uniqueFailureID;
    current.update();
} else {
    gs.log("UniqueFailureID not found in the email body for incident: " + current.number, "Inbound Email Action AAC_Automation");
}

 

 

lease Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

6 REPLIES 6

SanjivMeher
Kilo Patron
Kilo Patron

Can you try something like this?

 

 

var emailBody = email.body.text;
var match = emailBody.split(':');
 
if (emailBody.indexOf('uniqueFailureID')>-1) {
    for (var i=0;i<match.length;i++)
    {
            var uniqueFailureID = match[i].split('=');
            current.u_uniquefailureid = uniqueFailureID[1];
            current.update();
    }
} else {
    gs.info("UniqueFailureID not found in the email body for incident: " + current.number, "Inbound Email Action AAC_Automation");
}
 

 


Please mark this response as correct or helpful if it assisted you with your question.

I am still unable to transition any data from incoming emails with your proposed solution. It just reverts to the error message in the else. The incoming emails has multiple items besides the specific value I'm attempting to bring in. the full value of the line that is in the text  is "UniqueFailureID=PROD:SystemLegacy:AdviceWorks:ea17cf9a-4773-4400-9fa9-216f74ce3bcf"

 

This value comes at the very end of the email body. I would like to fill in the line after the last : but it seems any variations i attempt fail. 

kabi
Tera Contributor

First you can check if the 'UniqueFailureID exists in the text using

var exist=string.indexOf('UniqueFailureID');

if it exists it gives the index of that value, if doesnot exist it gives -1

If the index for example is 2 then you can use

string.substring(2) it will extract all the values of string from that value till the end of the string.

Maddysunil
Kilo Sage

@Tyler Johnson 

Can you log the email body to inspect the content you getting in that, Also Depending on the format of your email body, you might need to adjust the regular expression. For instance, if there could be whitespace characters around the = sign or the : sign, you might want to make the expression more flexible.

Please try the updated code below:

 

var emailBody = email.body_text;
gs.info("Email Body: " + emailBody); // Log the email body to inspect its content

// Adjusted regular expression to be more flexible
var regex = /UniqueFailureID\s*=\s*.*:([a-f0-9\-]+)$/im;
var match = emailBody.match(regex);

if (match && match[1]) {
    var uniqueFailureID = match[1];
    current.u_uniquefailureid = uniqueFailureID;
    current.update();
} else {
    gs.log("UniqueFailureID not found in the email body for incident: " + current.number, "Inbound Email Action AAC_Automation");
}

 

 

lease Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks