I want to Download a Attachment from a lInk which is send to my Outlook mail box from servicenow

vaishnav
Tera Contributor

Hi Community,

 

I am trying to solve an issue where I have creating an CSV attachment file of a record and then sending a link of that file to my outlook mailbox. But I am facing an error while downloading the attachment through that link from outlook...

 

Whenever I download it redirects to the my instance login page which I dont want, It should just download directly the csv file...

 

Can anyone help me through this?

1 REPLY 1

vaishnav
Tera Contributor

This is the script I am using:

 

(function runMailScript(current, template, email, email_action, event) {

    // Verify data and debug script execution

    gs.info("Script execution started");

    // Initialize CSV data and filename

    var filename = "AgendaItem.csv";

    var csvData = '';

    // Fetch CAB meeting details

    var cabMeetingId = current.cab_meeting.toString(); // Assuming cab_meeting is a reference field

    var cabMeetingGR = new GlideRecord("cab_meeting");

    if (cabMeetingGR.get(cabMeetingId)) {

        // Check if attachment already exists for this CAB meeting

        var existingAttachments = new GlideRecord("sys_attachment");

        existingAttachments.addQuery("table_name", "cab_meeting");

        existingAttachments.addQuery("table_sys_id", cabMeetingId);

        existingAttachments.addQuery("file_name", filename);

        existingAttachments.query();

        if (existingAttachments.next()) {

            // Delete existing attachment

            existingAttachments.deleteRecord();

        }

        // Fetch CAB agenda items for the selected meeting

        var agendaItemsGR = new GlideRecord("cab_agenda_item");

        agendaItemsGR.addQuery("cab_meeting", cabMeetingId);

        agendaItemsGR.query();

        // Add headers to CSV data

        csvData += "RFC Number,State,Risk,Service CI,Change Description,Planned Start Date/Time,Planned End Date/Time,Outage Start Date/Time,Outage End Date/Time,Change coordinator\n";

        // Loop through agenda items and add them to CSV data

        while (agendaItemsGR.next()) {

            var taskGR = new GlideRecord("change_request");

            if (taskGR.get(agendaItemsGR.task)) {

                // Retrieve priority from the associated task record

                //var priority = taskGR.priority.getDisplayValue();

                var risk = taskGR.risk.getDisplayValue() || "";

                // Fetch the display value of the state field using a GlideRecord query

                var stateDisplayValue = '';

                var stateGR = new GlideRecord('sys_choice');

                stateGR.addQuery('name', 'change_request');

                stateGR.addQuery('element', 'state');

                stateGR.addQuery('value', taskGR.state);

                stateGR.query();

                if (stateGR.next()) {

                    stateDisplayValue = stateGR.label;

                }

                // Add agenda item details to CSV data

                csvData += '"' + agendaItemsGR.task.number + '","' + stateDisplayValue + '","' + risk + '","' + taskGR.category.getDisplayValue() + '","' + agendaItemsGR.short_description + '","' + taskGR.start_date.getDisplayValue() + '","' + taskGR.end_date.getDisplayValue() + '","' + taskGR.u_planned_outage_start_date.getDisplayValue() + '","' + taskGR.u_planned_outage_end_date.getDisplayValue() + '","' + taskGR.assigned_to.getDisplayValue() + '"\n';

            } else {

                gs.error("Task record not found for agenda item: " + agendaItemsGR.task);

            }

        }

        // Write CSV data to attachment

        var attachmentSysId = new GlideSysAttachment().write(cabMeetingGR, filename, 'application/csv', csvData);

        // Print download link for the CSV attachment

        if (attachmentSysId) {

            gs.log('Attachment Exist', 'vaishnav');

            template.print("Please find the agenda item details for CAB meeting '" + cabMeetingGR.name + "':<br>");

            // Generate a unique token

            var token = gs.generateGUID();

            // Append the token to the download link

            var attachmentLink = cabMeetingGR.name + " - <a href='" + gs.getProperty('glide.servlet.uri') + "sys_attachment.do?sys_id=" + attachmentSysId + "'>Download AgendaItem.csv</a>";

            template.print(attachmentLink);

        }

    } else {

        gs.error("CAB Meeting with ID '" + cabMeetingId + "' not found.");

        template.print("CAB Meeting not found.");

    }

})(current, template, email, email_action, event);