How to raise multiple change requests based on each window

GAyathri33
Tera Contributor

Hi Team,

 

Email Integration:  Inbound actions through create service request and change request

Script needed for below scenario:

1.  Once we will receive an email from "xyz" with .csv attachment of circuits' details, then it will create one service request and in the request's description, we can get the details of multiple start date and end date (Please see below)

 

Window 1:

Start: 22/08/2021 23:00

End: 22/08/2021 07:00

 

Window 2:

Start: 23/08/2021 23:00

End: 23/08/2021 07:00

 

Window 3:

Start: 24/08/2021 23:00

End: 24/08/2021 07:00

 

Based on the above window (these dates were mentioned in the email body)

 

those window's details are auto populated in the service request's description and it should read those window details and each window needs to create separate Change request.

 

Can you please help to get it successful - how to write code for it

 

Thanks in Advance.

 

3 REPLIES 3

Amit Pandey
Kilo Sage

Hi @GAyathri33 

 

Inbound action can help you in this requirement. You need to add the condition email.from == "xyz" and then try the following code-

 

 

(function processEmailAttachment(email, attachment) {
    // Check if the email is from "xyz" and has a .csv attachment
    if ( attachment.name.endsWith(".csv")) {
        var csvContent = new GlideSysAttachment().getContent(attachment.sys_id);
        var csvLines = csvContent.split('\n');

        // Iterate through each line in the CSV file
        for (var i = 0; i < csvLines.length; i++) {
            var csvLine = csvLines[i].split(',');

            // Extract circuit details from CSV line
            var circuitName = csvLine[0].trim();
            var startDate = csvLine[1].trim();
            var endDate = csvLine[2].trim();

            // Create service request
            var serviceRequest = new GlideRecord('sc_request');
            serviceRequest.initialize();
            serviceRequest.short_description = "Circuit Maintenance - " + circuitName;
            serviceRequest.description = "Maintenance window for circuit " + circuitName + ":\nStart: " + startDate + "\nEnd: " + endDate;
            var serviceRequestId = serviceRequest.insert();

            // Create change request for each window
            for (var j = 1; j <= 3; j++) {
                var changeRequest = new GlideRecord('change_request');
                changeRequest.initialize();
                changeRequest.short_description = "Circuit Maintenance - Window " + j + " - " + circuitName;
                changeRequest.description = "Maintenance window " + j + " for circuit " + circuitName + ":\nStart: " + startDate + "\nEnd: " + endDate;
                changeRequest.request = serviceRequestId;
                changeRequest.insert();
            }
        }
    }
})(email, attachment);

 

 

Thanks for the script, but Start date and end date are mentioned in the body text of the email, not in the .csv attachment

You can modify it-

 

(function processEmailBody(email) {
    
        // Extract email body text
        var emailBody = email.body_text;
        
        // Split email body text by line breaks
        var lines = emailBody.split('\n');
        
        // Regular expression to match date and time format (dd/mm/yyyy HH:MM)
        var dateTimeRegex = /\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}/g;
        
        // Array to store extracted start and end dates
        var dates = [];
        
        // Extract start and end dates from each line of the email body
        for (var i = 0; i < lines.length; i++) {
            var line = lines[i];
            var matches = line.match(dateTimeRegex);
            
            if (matches && matches.length >= 2) {
                var startDate = matches[0];
                var endDate = matches[1];
                dates.push({start: startDate, end: endDate});
            }
        }
        
        // Create service request and change requests for each date range
        for (var j = 0; j < dates.length; j++) {
            var startDate = dates[j].start;
            var endDate = dates[j].end;
            
            // Create service request
            var serviceRequest = new GlideRecord('sc_request');
            serviceRequest.initialize();
            serviceRequest.short_description = "Circuit Maintenance - Window " + (j + 1);
            serviceRequest.description = "Maintenance window:\nStart: " + startDate + "\nEnd: " + endDate;
            var serviceRequestId = serviceRequest.insert();
            
            // Create change request
            var changeRequest = new GlideRecord('change_request');
            changeRequest.initialize();
            changeRequest.short_description = "Circuit Maintenance - Window " + (j + 1);
            changeRequest.description = "Maintenance window:\nStart: " + startDate + "\nEnd: " + endDate;
            changeRequest.request = serviceRequestId;
            changeRequest.insert();
        }
    }
})(email);