Variable script to allow to pull data from body of email.

RobDoyle
Giga Guru

Hi,
I am trying to create a flow for handling alerts from our network monitoring platform.

We will receive 2 emails one when the device goes down and one when it comes back up.

Inside the body of the text is the device  (Screenshot attached) I want to extract so that I populate the CI field.

We have a similar email handler to get the variable for us just wondering how I would modify it to get the device

/*
** Access Flow/Action data using the fd_data object. Script must return a value.
*/

// Assuming body_text contains the whole text block provided
var bodyText = fd_data.trigger.body_text;

// Use a regular expression to match "Site Name & Reference" followed by any amount of whitespace and the colon
var regex = /Site Name & Reference\s*:\s*/;

// Split the text using the regular expression to account for any amount of whitespace
var splitByReference = bodyText.split(regex);

// Check if the split produced an array with at least two elements
if (splitByReference.length > 1) {
    // The number should be right after the "Site Name & Reference", on the same line
    // Split the second element of the array by newline to isolate the line
    var lines = splitByReference[1].split(/\r?\n/);

    // The line with the site name and number should be the first line after the split
    var siteAndNumber = lines[0];

    // Split the line at the comma to separate the site name from the number
    var parts = siteAndNumber.split(',');

    // Check if there are enough parts and if the second part is a number
    if (parts.length > 1 && !isNaN(parts[1].trim())) {
        // Return the trimmed number part
        return parts[1].trim();
    }
 
I assume I would replace the regex with device but just wondering how to write to be get me the device name and not the IP address after it.



1 ACCEPTED SOLUTION

Amit Verma
Kilo Patron
Kilo Patron

Hi @RobDoyle 

 

Can you try with the below regex:

/Device:(\s([A-Za-z]+\s)+)/

 

I tried this in the background script and it is giving me expected output :

AmitVerma_2-1722860104360.png

 

AmitVerma_1-1722860086287.png

 

Thanks and Regards

Amit Verma


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

View solution in original post

4 REPLIES 4

Mark Manders
Mega Patron

Try:

var regex = /Device:\s(.*?)\s\(/;

 Only thing is that you are checking between 'Device:' and '(', so I hope you always have the IP there. If not, you will need some logic to check on the IP being there or not and if so, remove it.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Amit Verma
Kilo Patron
Kilo Patron

Hi @RobDoyle 

 

Can you try with the below regex:

/Device:(\s([A-Za-z]+\s)+)/

 

I tried this in the background script and it is giving me expected output :

AmitVerma_2-1722860104360.png

 

AmitVerma_1-1722860086287.png

 

Thanks and Regards

Amit Verma


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

I know you used a variable to plug the data in but I am using the body text variable and its returning "switch not found"

Apologies for my lack of script knowledge but have i wrote it wrong 

RobDoyle
Giga Guru

This is how i ended running it to get it to work

/*
** Access Flow/Action data using the fd_data object. Script must return a value.
*/

// Assuming body_text contains the whole text block provided
var bodyText = fd_data.trigger.body_text;

// Use a regular expression to match "Device" followed by any amount of whitespace, a colon, the device name, and optionally an IP address in parentheses
var regex = /Device:\s*([A-Za-z0-9\s]+)(\s\([0-9.]+\))?/;
var match = bodyText.match(regex);

// Check if the match was successful before trying to access the result
if (match) {
var deviceName = match[1].trim(); // match[1] contains the device name, we use .trim() to remove any leading/trailing spaces
gs.info(deviceName); // Log the device name
return deviceName; // Return the device name
} else {
gs.info('No match found'); // Log the failure to match
return 'No match found'; // Return a default value
}