Hi guys,
Wondering whether it's possible to select attachments via scripting, based on the timestamp of the upload?
Say I have an item with 10 attachments. I'd like to print to a field, the name of the document that was uploaded first.
I believe the attachments array is unsorted.
I have tried creating an array of the timestamps (which works), although whenever I try and do anything with the data, I run into this error:
"Access to Java class "java.sql.Timestamp" is prohibited."
For info, here's some code I've played with using Test in the script editor.
var array = [];
var lastAttachment = item.attachments[item.attachments.length-1].timeStamp;
var firstAttachment= item.attachments[0].timeStamp;
println("first attachment is "+firstAttachment);
println("last attachment is "+lastAttachment);
for (var i = 0; i<item.attachments.length; i++){
var stampTime = item.attachments[i].timeStamp;
array.push(stampTime);
}
println("Array of dates is "+array);
oldestAttachment = new Date(Math.min.apply(null,array));
println("Oldest attachment is "+oldestAttachment);
println(typeof array);
println(typeof array[0]);
Anyone with more javascript experience able to help me?
Cheers,
Stuart
replace
item.attachments[i].timeStamp
with
item.attachments[i].timeStamp.getTime()
That should get you the actual numerical value of the timestamp (millisecond since 1970)
Fusion Lifecycle has a habit of blowing up if you try to print something that isn't intentionally made to print. You can actually probably compare the timeStamps, so long as you don't actaully print them.
Thanks for your reply.
I've read up on the javascript documentation since posting this, and it seems that Date class and Timestamp class, although related, are seperate entities.
Trying your method I get the same error, on the line with getTime() on it. "Access to Java class "java.sql.Timestamp" is prohibited."
What I tried instead, and I'm surprised it worked because it's so simple, is below:
var array = []; for (var i = 0; i<item.attachments.length; i++){ var stampTime = item.attachments[i].timeStamp; array.push(stampTime); } println(array); array.sort(); println(array);
println(array[0]);
Try this on any item with multiple attachments, and it should return the earliest timeStamp.
Cheers
Ugh, next issue:
println(array[0]);
item.QUOTE_RECEIVED = new Date(array[0]);
println(new Date(array[0]));
gives "Invalid Date"
Still running into the same error, "Access to Java class "java.sql.Timestamp" is prohibited."
I guess there's no way around this, I have tried manipulating the data to no effect.
Autodesk?
All I want to do is assign a date to the field "quote received", based on the earliest upload in the items attachments tab.
And nothing has changed. Access to Java class "java.sql.Timestamp" is prohibited. It it not possible to get the value converted to a string or anything else to convert the timestamp into a date.
I think the most you have to satisfy yourself with the toString on the attachment.timeStamp field.
The toString is in a string format such that doing a STRING comparison is enough to give you oldest.
Then chop up the string, create a proper Date object, to use to set the item's field.
This worked for me:
var oldestTs = null;
for (var i in item.attachments) {
var att = item.attachments[i];
var ts = ""+att.timeStamp; // 2022-03-07 03:16:47.348
if (oldestTs === null || ts < oldestTs) oldestTs = ts;
}
println("oldestTs="+oldestTs); // 2022-02-21 05:47:00.406
if (oldestTs !== null) {
var bit1 = oldestTs.split(' ')[0]; // 2022-02-21
var bit2 = oldestTs.split(' ')[1]; // 05:47:00.406
var yyyy = bit1.split('-')[0];
var MM = bit1.split('-')[1];
var dd = bit1.split('-')[2];
var hh = bit2.split(':')[0];
var mm = bit2.split(':')[1];
var ss = bit2.split(':')[2]; // 00.406
if (ss.indexOf('.')) ss = ss.split('.')[0]; // remove millisecs
var dt = new Date(yyyy, --MM, dd, hh, mm, ss); // remember months are zero based
println("dt="+dt);
item.QUOTE_DATE = dt;
}
Yes, I know I could avoid calling split() so much, but it is like this to be clearest.
Thanks to the help from Sven Dickmans I come to a similar solution:
var fileTimeStampStr = String(file.timeStamp); // "2019-05-28 07:10:27.639"
var timeDateArray = fileTimeStampStr.split(" "); // split [0] 2019-05-28 and [1] 07:10:27.639
var date = timeDateArray[0].split("-"); // split date in yyyy MM dd
var timeArray = timeDateArray[1].split("."); // split 10:24:48.32 [0] 07:10:27 and [1] 639
var time = timeArray[0].split(":"); // split time in HH mm ss
var fileDate = new Date(date[0], date[1]-1, date[2], time[0], time[1], time[2]);
// fileDate: 2019-05-28 07:10:27.639 Tue May 28 2019 07:10:27 GMT-0400 (EDT)
println('fileDate: ' + file.timeStamp, fileDate );