cancel
Showing results for 
Search instead for 
Did you mean: 

Simple JavaScript???

peterstb
Champ in-the-making
Champ in-the-making
Hi,

I'm struggling with something that feels like it should be as simple javascripting exercise, and am not able to derive any help from the JavaScript API reference or the JavaScript API Cookbook.  Although both are good documents, I seem to be struggling with context, specifically related to getting handles and executing workflow steps on documents that are not the documents that have initiated an action ("execute script").

I have draft, review, heartbeat, and live folders/spaces defined, and want to execute a script, when an approver approves a document from the review space.  There is a simple workflow rule defined to allow an approver to approve or reject an item in the revew folder.  Approval moves the item to the live space.  I have a rule defined on the live space to "execute a script". 

My desired functionality is that when a document lands in live, a heartbeat file (simple .txt file) is updated (in the heartbeat space), perhaps with something like a timestamp.  This file will be used by an external process to determine if the entire live space needs to be checked for new content, in order to support a simple caching strategy we have in mind.

This seems like it should be an easy task, but I'm really struggling!  Does anyone have any sample JavaScript they could provide to help me achieve this?  I'll also take alternative suggestions for accomplishing the same objective…

Thanks!
Todd
2 REPLIES 2

zaizi
Champ in-the-making
Champ in-the-making
var heartbeatfile = companyhome.childByNamePath("path to heart beat file");
var d = new Date();
// update the modified property
heartbeatfile.properties["cm:modified"] = d;
// update the file content
heartbeatfile.content = d.toString();
heartbeatfile.save();
Alternatively, you can just write a webscript (http://wiki.alfresco.com/wiki/Web_Scripts) that does a search through your live space and returns the date / time of the most recently updated document.

peterstb
Champ in-the-making
Champ in-the-making
Thanks so much, Zaizi, for the response!

I have inserted the following code into my .js file (residing in the Data Dictionary\Scripts directory:

var heartbeatfile = companyhome.childByNamePath("MySpace/Heartbeat/heartbeat.txt");
var d = new Date();

// update the modified property
heartbeatfile.properties["cm:modified"] = d;

// update the file content
heartbeatfile.content = d.toString();

heartbeatfile.save();

Where "MySpace" is a space defined under Company Home, and "Heartbeat" is a subspace under MySpace.  "heartbeat.txt" is the file I would like to update.

When I put that code in, I got the following error, at execution time:

Failed to approve the document due to system error: Failed to execute script 'workspace://SpacesStore/92a0850e-0476-468a-99f6-e028401de4eb': TypeError: Cannot read property "properties" from null (workspace://SpacesStore/92a0850e-0476-468a-99f6-e028401de4eb#5)

Any ideas what might be the cause?  I was seeing similar error in previous trial-and-error scripting.

BTW - I like your idea of the WebScript, responding with a datetime of the most recently updated content in the "Live" folder.  I may create an implementation of that, once I get this script working.

Thanks!
Todd