The CreatorCon Call for Content is officially open! Get started here.

Adding attachments to a workflow notifcation

khumphreys
Tera Contributor

Hi,

I have a process where the ess user creates a new service catalog item and attachs a document. (the attachment button is a variable that looks up a macro).

The workflow then sends a sc task to a new set of users and another attachment is added.

1. My first issue is that i need to attach the documents to the Approval notifcations (i have ticked the 'include attachment' in the notification and nothing happens?)

2. I also need the attachments to appear in a final workflow email notifcation. I have added in the following mail script to my workflow notification which works fine for the SC Attachment, but this doesn't attach my other document which was uploaded in the Catalog Task:

Any help will be greatly appreciated.

<mail_script>

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.sys_id);

      gr.query();

      if(gr.hasNext()){

            template.print('Attachments: \n');

                  while (gr.next()) {

                        var attachLink = '<a href="' + gs.getProperty("glide.servlet.uri") + gs.generateURL(gr.getTableName(),gr.sys_id) +   '">' + gr.file_name + '</a>';

                        template.print(attachLink +   '\n');

                  }

            template.print('<hr/>');

      }

}

</mail_script>

1 ACCEPTED SOLUTION

Kathryn,



Change the Script to :


<mail_script>


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.sys_id);


      gr.query();


      if(gr.hasNext()){


            template.print('Attachments: \n');


                  while (gr.next()) {


                        var attachLink = '<a href="' + gs.getProperty("glide.servlet.uri") + gs.generateURL(gr.getTableName(),gr.sys_id) +   '">' + gr.file_name + '</a>';


                        template.print(attachLink +   '\n');


                  }


            template.print('<hr/>');


      }


       


      var task = new GlideRecord('sc_task');


      task.addQuery('request_item',current.sys_id);


      task.query();


      while(task.next())


      {


          var gr1 = new GlideRecord('sys_attachment');


      gr1.addQuery('table_sys_id',task.sys_id);


      gr1.query();


      if(gr1.hasNext()){


            template.print('Attachments: \n');


                  while (gr1.next()) {


                        var attachLink = '<a href="' + gs.getProperty("glide.servlet.uri") + gs.generateURL(gr1.getTableName(),gr1.sys_id) +   '">' + gr1.file_name + '</a>';


                        template.print(attachLink +   '\n');


                  }


            template.print('<hr/>');


      }



        }


}


</mail_script>


View solution in original post

14 REPLIES 14

Hi Mani,



thank you for your script, i have used the code above and its copied across two links in the notification to the attachment that was attached on the service Cat request and not the task attachment?



Any ideas?


thanks


Kathryn


Kathryn,



Change the Script to :


<mail_script>


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.sys_id);


      gr.query();


      if(gr.hasNext()){


            template.print('Attachments: \n');


                  while (gr.next()) {


                        var attachLink = '<a href="' + gs.getProperty("glide.servlet.uri") + gs.generateURL(gr.getTableName(),gr.sys_id) +   '">' + gr.file_name + '</a>';


                        template.print(attachLink +   '\n');


                  }


            template.print('<hr/>');


      }


       


      var task = new GlideRecord('sc_task');


      task.addQuery('request_item',current.sys_id);


      task.query();


      while(task.next())


      {


          var gr1 = new GlideRecord('sys_attachment');


      gr1.addQuery('table_sys_id',task.sys_id);


      gr1.query();


      if(gr1.hasNext()){


            template.print('Attachments: \n');


                  while (gr1.next()) {


                        var attachLink = '<a href="' + gs.getProperty("glide.servlet.uri") + gs.generateURL(gr1.getTableName(),gr1.sys_id) +   '">' + gr1.file_name + '</a>';


                        template.print(attachLink +   '\n');


                  }


            template.print('<hr/>');


      }



        }


}


</mail_script>


Hi Team,



I have tried this script.



Attachment is showing as an hyperlink format. I am not able to open it. Can anyone suggest me Please.



find_real_file.png



Regards,


Saridha.L


postwick
Giga Expert

All that script does is generate HTML links in the email.   If the user clicks the link, the browser will download a file IF they are logged into ServiceNow.



The reason the attachments aren't being added to the email even though you have "include attachments" in the notification is because the notification is generated from the Approvals table, but the files are attached to a Request.   In order to make this work, you have to copy the attachments from the parent Request when the Approval record is created.   As Kshitij pointed out, you do this in your script with the following line of code:




GlideSysAttachment.copy("source_table", "source_record_sys_id", "target_table", "target_Record_sys_id");


Hi Paul,



Thank you for your email, can you confirm where this line of code goes? my script above is in the final workflow notification email in the workflow, so i'm guessing it doesn't go there?



My Approval emails are notifications sent from the email notifcation templates?


thanks


kathryn