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

Kshitij Tripath
Tera Contributor

Hi Kathryn,



Regarding your first point : To have the attachments in the approval mail, you need to have the attachment available on the approval record. You can do this using the Service-now attachment api :



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



GlideSysAttachment is a Java class. Just replace the paramaters in above method for your requirement. For your case, target table will be Approval table.



Regarding your second point : the table against which email notification is written should have the attachment available for the record and the column "Include attachments" should be checked.



Please let me know if this helps you.


Hi,



Sorry, but could clarify where to create the service now api??



Many thanks


Kathryn


Another way, instead of creating a copy of the first attachment to go with the Approval, amend your script so that it looks up the   record that the approval is for (sysapproval) and attaches the documents associated with that instead


your current script is looking to attach any attachments that are associated with the current. sysapproval value and not current.sys_id


manikorada
ServiceNow Employee
ServiceNow Employee

Kathryn,



For you #2, have the script something like:


<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',current.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>