Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Add all attachments links including variable attachments from RITM Record to Email

GamNOW
Tera Contributor

Hello ServiceNow Experts,

 

I'm looking to see how I can fulfill this business requirement.  I am trying to achieve an email notice that will be sent to approvers that contains links to attachments from the sc_req_item RITM record.  While the script below works great, it does contain links that are attached on the RITM, but I would like to also include attachments from Type: Attachment variable.  Here is the email script I used for now.

 

 

attachLinks();

function attachLinks() {
    //Check for any attachments and add attachment links if they exist
    var gr = new GlideRecord('sys_attachment');
    gr.addQuery('table_sys_id', current.getValue('sysapproval'));
    gr.addQuery('table_name', 'sc_req_item');
    gr.query();
    if (gr.hasNext()) {
        template.print("Attachments: <br />");
        while (gr.next()) {
            var attachLink = '<a href="' + gs.generateURL(gr.getTableName(), gr.sys_id) + '">' + gr.file_name + '</a>';
            template.print(attachLink + "<br />");
        }
        template.print("<hr/>");
    }
}

 

 

Now, the Email Notification that gets sent is a blanket Approval Request, and includes the syntax as ${mail_script:attachmentLink}.  I want to be able to reference also the variables that are an attachment as a link to this email.  I hope I do not need to reference each variable's sys ID to retrieve this since the email notification applies to other catalog items, but any thoughts on how to achieve this? 

 

Thank you!

1 ACCEPTED SOLUTION

GopikaP
Mega Sage

Hi @GamNOW , Try this - 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here
    var gr = new GlideRecord('sys_attachment');
    gr.addQuery('table_sys_id', current.getValue('sysapproval'));
    gr.addEncodedQuery('table_nameLIKEsc_req_item');
    gr.query();
    if (gr.hasNext()) {
        template.print("Attachments: <br />");
        while (gr.next()) {
			template.print("Hi");
            var t = '<a href="' + gs.getProperty('glide.servlet.uri') + gs.generateURL(gr.getTableName(), gr.sys_id)+ '">' + gr.file_name + '</a>';
            template.print(t + "<br />");
        }
        template.print("<hr/>");
    }


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

View solution in original post

2 REPLIES 2

GopikaP
Mega Sage

Hi @GamNOW , Try this - 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here
    var gr = new GlideRecord('sys_attachment');
    gr.addQuery('table_sys_id', current.getValue('sysapproval'));
    gr.addEncodedQuery('table_nameLIKEsc_req_item');
    gr.query();
    if (gr.hasNext()) {
        template.print("Attachments: <br />");
        while (gr.next()) {
			template.print("Hi");
            var t = '<a href="' + gs.getProperty('glide.servlet.uri') + gs.generateURL(gr.getTableName(), gr.sys_id)+ '">' + gr.file_name + '</a>';
            template.print(t + "<br />");
        }
        template.print("<hr/>");
    }


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

GamNOW
Tera Contributor

Thank you GopikaP, this worked great!!