How to email csv file from server side script result

Snehal13
Kilo Sage

I have server side script that runs and queries incident table and returns matching incidents. This output result from this script needs to emailed in a csv file, so how to reuse this code to email file

1 ACCEPTED SOLUTION

Shaqeel
Mega Sage

Hi @Snehal13 

 

Reuse your existing server-side script that queries the incident table. Then, format the output into CSV.

Server side:

function generateIncidentCSV() {
    var gr = new GlideRecord('incident');
    gr.addEncodedQuery('active=true');  // Your custom query here
    gr.query();

    var csvContent = 'Number,Short Description,State,Priority\n';  // CSV Header

    // Loop through each record and append to the CSV string
    while (gr.next()) {
        var number = gr.getValue('number');
        var shortDescription = gr.getValue('short_description');
        var state = gr.getDisplayValue('state');  // Use display value for readability
        var priority = gr.getDisplayValue('priority');
        
        csvContent += number + ',' + shortDescription + ',' + state + ',' + priority + '\n';
    }

    return csvContent;  // Return the CSV content as a string
}

 

Now that we have the CSV string, we can send it as an email attachment using GlideSysAttachment and GlideEmailOutbound.

Complete Script to Send CSV via Email:

function sendIncidentCSV() {
    // Step 1: Generate the CSV content
    var csvContent = generateIncidentCSV();

    // Step 2: Create an attachment from the CSV content
    var attachmentSysId = attachCSVtoRecord(csvContent, 'incident', 'CSV Report', 'incident_report.csv');
    
    // Step 3: Send the email with the CSV file attached
    sendEmailWithAttachment(attachmentSysId);
}

function generateIncidentCSV() {
    var gr = new GlideRecord('incident');
    gr.addEncodedQuery('active=true');  // Your custom query here
    gr.query();

    var csvContent = 'Number,Short Description,State,Priority\n';  // CSV Header

    while (gr.next()) {
        var number = gr.getValue('number');
        var shortDescription = gr.getValue('short_description');
        var state = gr.getDisplayValue('state');
        var priority = gr.getDisplayValue('priority');

        csvContent += number + ',' + shortDescription + ',' + state + ',' + priority + '\n';
    }

    return csvContent;
}

function attachCSVtoRecord(csvContent, tableName, description, fileName) {
    var gr = new GlideRecord(tableName);  // Create a new record in the table to attach the file
    gr.initialize();  // Initialize a new empty record
    gr.setValue('short_description', description);  // Set some description
    var recordSysId = gr.insert();  // Insert the record

    // Attach the CSV content as a file
    var attachment = new GlideSysAttachment();
    var csvStream = new GlideStringUtil().toStream(csvContent);
    var attachmentSysId = attachment.write(recordSysId, fileName, 'text/csv', csvStream);  // Create CSV attachment

    return attachmentSysId;
}

function sendEmailWithAttachment(attachmentSysId) {
    var mail = new GlideEmailOutbound();
    mail.setSubject('Incident Report - CSV Export');
    mail.setBody('Please find the attached incident report in CSV format.');
    mail.setFrom('your.email@company.com');  // Set your email address
    mail.setTo('recipient.email@company.com');  // Set the recipient's email address

    // Attach the CSV file
    mail.addAttachment(attachmentSysId);

    // Send the email
    mail.send();
}

// Call the function to send the email with the CSV attachment
sendIncidentCSV();


*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

*************************************************************************************************************

 

 

Regards

Shaqeel

 


***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

***********************************************************************************************************************





Regards

Shaqeel

View solution in original post

1 REPLY 1

Shaqeel
Mega Sage

Hi @Snehal13 

 

Reuse your existing server-side script that queries the incident table. Then, format the output into CSV.

Server side:

function generateIncidentCSV() {
    var gr = new GlideRecord('incident');
    gr.addEncodedQuery('active=true');  // Your custom query here
    gr.query();

    var csvContent = 'Number,Short Description,State,Priority\n';  // CSV Header

    // Loop through each record and append to the CSV string
    while (gr.next()) {
        var number = gr.getValue('number');
        var shortDescription = gr.getValue('short_description');
        var state = gr.getDisplayValue('state');  // Use display value for readability
        var priority = gr.getDisplayValue('priority');
        
        csvContent += number + ',' + shortDescription + ',' + state + ',' + priority + '\n';
    }

    return csvContent;  // Return the CSV content as a string
}

 

Now that we have the CSV string, we can send it as an email attachment using GlideSysAttachment and GlideEmailOutbound.

Complete Script to Send CSV via Email:

function sendIncidentCSV() {
    // Step 1: Generate the CSV content
    var csvContent = generateIncidentCSV();

    // Step 2: Create an attachment from the CSV content
    var attachmentSysId = attachCSVtoRecord(csvContent, 'incident', 'CSV Report', 'incident_report.csv');
    
    // Step 3: Send the email with the CSV file attached
    sendEmailWithAttachment(attachmentSysId);
}

function generateIncidentCSV() {
    var gr = new GlideRecord('incident');
    gr.addEncodedQuery('active=true');  // Your custom query here
    gr.query();

    var csvContent = 'Number,Short Description,State,Priority\n';  // CSV Header

    while (gr.next()) {
        var number = gr.getValue('number');
        var shortDescription = gr.getValue('short_description');
        var state = gr.getDisplayValue('state');
        var priority = gr.getDisplayValue('priority');

        csvContent += number + ',' + shortDescription + ',' + state + ',' + priority + '\n';
    }

    return csvContent;
}

function attachCSVtoRecord(csvContent, tableName, description, fileName) {
    var gr = new GlideRecord(tableName);  // Create a new record in the table to attach the file
    gr.initialize();  // Initialize a new empty record
    gr.setValue('short_description', description);  // Set some description
    var recordSysId = gr.insert();  // Insert the record

    // Attach the CSV content as a file
    var attachment = new GlideSysAttachment();
    var csvStream = new GlideStringUtil().toStream(csvContent);
    var attachmentSysId = attachment.write(recordSysId, fileName, 'text/csv', csvStream);  // Create CSV attachment

    return attachmentSysId;
}

function sendEmailWithAttachment(attachmentSysId) {
    var mail = new GlideEmailOutbound();
    mail.setSubject('Incident Report - CSV Export');
    mail.setBody('Please find the attached incident report in CSV format.');
    mail.setFrom('your.email@company.com');  // Set your email address
    mail.setTo('recipient.email@company.com');  // Set the recipient's email address

    // Attach the CSV file
    mail.addAttachment(attachmentSysId);

    // Send the email
    mail.send();
}

// Call the function to send the email with the CSV attachment
sendIncidentCSV();


*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

*************************************************************************************************************

 

 

Regards

Shaqeel

 


***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

***********************************************************************************************************************





Regards

Shaqeel