How to fetch a substring from a string from an email body

Sandeep109
Mega Contributor

Hi All,

i actually need to retrieve a substring from a string and that too from a email body, and all this is related to inbound email actions where the email is triggered from a tool to our instance and from there i need to retrieve

 

For example: In the email body we have the below data for Configuration and Description and we need to fetch only 'ZSNH36TJ50-000YT-100YDT' from configuration

Configuration : ZSNH36TJ50-000YT-100YDT in New Jersey,  USA

Description:Power is down

 

The script i used 

if (email.body.configuration != undefined)
var res = email.body.configuration;

var sec=res.substring(0,24);

 

But neither the string in configuration is getting fetched nor substring is fetched

Could you please help me with the code

 

Thanks and Regards,

Sandeep

 

 

 

 

 

7 REPLIES 7

Hi,

I don't know what you have in your script at this point, but what I've included above would work as a 1 line solution to your problem.

So this...

var res = email.body.configuration;

var sec=res.substring(0,24);

can simply be changed to:

var res = email.body_text.split("Configuration : ")[1].split(" ")[0];

or try:

var res = email.body_text;
var sec = res.split("Configuration : ")[1].split(" ")[0];

Now "res" (in the first example) has the outcome that you want and can be used elsewhere in your script or "sec" (in the second example).

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

_ChrisHelming
Tera Guru

If the space is there and you're unable to use email.body.configuration you could use a regular expression on the entire email body looking for the match.

email.body.match(/Configuration : ([^\s]+)/);

vkachineni
Kilo Sage
Kilo Sage
var config = "Configuration : ZSNH36TJ50-000YT-100YDT in New Jersey,  USA";
var id = config.split(":")[1].trim() || ''; //If for some reason it could not split, default to ''
if(id != ''){
    id = id.substring(0,23);
}
gs.print(id);
ZSNH36TJ50-000YT-100YDT
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022