Date/Time Stamping a Scheduled Report's Attachment(s)

Jim Coyne
Kilo Patron

I've seen a couple requests to add a date/time stamp to the attachment of a Scheduled Report.   Capa JC shows us how to add this to the email's subject in this post (When configuring a Scheduled), but doing it on an attachment is still not possible.   Until now.

 

A nice little workaround with a Business Rule on the Email table is all you need.   When you configure your Scheduled Report, add "~!~rename_attachment~!~" as a prefix to the Subject to act as a trigger for the Business Rule (e.g. "~!~rename_attachment~!~Active Change Requests").

 

Next, add the following Business Rule:

 

Name: Custom - Rename Scheduled Report Attachments
Table: Email [sys_email]
When: before
Insert: checked
Condition: current.subject.indexOf("~!~rename_attachment~!~") > -1 && current.hasAttachments()
Script:


(function(){
         var splitter = "~x~y~z";
         current.subject = current.subject.replace("~!~rename_attachment~!~", "");
         var gr = new GlideRecord("sys_attachment");
         gr.addQuery("table_name", "sys_email");
         gr.addQuery("table_sys_id", current.getValue("sys_id"));
         gr.query();
         while (gr.next()) {
                   var fileName = gr.getValue("file_name");
                   var parts = fileName.split(".");
                   var dateCreated = splitter + gr.getValue("sys_created_on");
                   parts.splice(-1, 0, dateCreated);
                   fileName = parts.join(".").replace("." + splitter, "-");
                   gr.file_name = fileName;
                   gr.update();
         }
})();

 

The Business Rule queries the attachments, grabs each file name, splits it apart, inserts the created date, joins it back together and then finally updates the record with the new file name.

 

Simple

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

Just setting this answer as correct as the actual thread was not meant as a question and it cannot be changed because it came from the original Community site.

View solution in original post

26 REPLIES 26

Hi Nicholas



This works a treat, well almost.



I have two issues:


1. It adds the date to the front of the email subject as well


find_real_file.png


How do I get rid of that?


2. the date is reversed, how do I make it dd/mm/yyyy?



Thanks



Chris


Hi Chris,



The following line in the script is what puts the date into the subject; you can remove it if you don't want it:


current.subject = current.subject.replace("~!~file_date~!~", dt);


You can format the date by splitting it into an array and recombining it in the order you want, like this:


var dtArr = dt.split('-');


// now you will have an array where the first element is the year, the second element is the month, and the third element is the day (ISO format = yyyy-mm-dd)


var newDt = dtArr[2] + "/" + dtArr[1] + "/" + dtArr[0];



The use the newDt variable in place of dt for the rest of the script.



Hope that helps.



Thanks,


Nicholas


HI Chris,

How to give space after date?

 

 

Hi

 

Not sure if it is what you are after though if you add " " into the line it will add a space

Uncle Rob
Kilo Patron

Clapping.gif