How to fetch a substring from a string from an email body
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 08:25 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 09:24 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 08:50 AM
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]+)/);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 09:04 AM
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
Vinod Kumar Kachineni
Community Rising Star 2022