AnsweredAssumed Answered

Access companyhome/userhome objects, onUpdateNode?

Question asked by henrik on May 27, 2010
Latest reply on Jun 20, 2010 by henrik

I am trying to configure an action where after some one adds an aspect to a specific node, one of the aspect properties is filled with a number.

I have a configured bean that looks like this, the script is executed on update and everything works fine there:

<?xml version='1.0' encoding='UTF-8'?>
   <bean id="onUpdateNode_SetNewNummer" class="org.alfresco.repo.policy.registration.ClassPolicyRegistration" parent="policyRegistration">
               <property name="policyName">
               <property name="className">
               <property name="behaviour">
                       <bean class="org.alfresco.repo.jscript.ScriptBehaviour" parent="scriptBehaviour">
                               <property name="location">
                                       <bean class="org.alfresco.repo.jscript.ClasspathScriptLocation">

Then, the JavaScript onUpdateNode_SetNewNumber.js contains this:

logger.log("LCM onUpdateNode_SetDocnumber has fired");

var scriptFailed = false;

// Have a look at the behaviour object that should have been passed
if (behaviour == null)
   logger.log("LCM The behaviour object has not been set.");
   scriptFailed = true;

// Check the name of the behaviour
if ( == null && != "onUpdateNode")
   logger.log("LCM The behaviour name has not been set correctly.");
   scriptFailed = true;
   logger.log("LCM Behaviour name: " +;

// Check the arguments
if (behaviour.args == null)
   logger.log("LCM The args have not been set.")
   scriptFailed = true;
   if (behaviour.args.length == 1)
       var nodeRef = behaviour.args[0];
       logger.log("LCM NodeRef: " +;
       if (nodeRef.hasAspect("{}documentNumber")) {

function setDocumentId(document) {
   var CounterValue = 1;
   var CurrentYear = new Date().getFullYear();
   // Get the Diarie Counter file
   var DiarieCounterFile = companyhome.childByNamePath(CurrentYear);
   // Check if the "YEAR" file exists allready
   if (DiarieCounterFile == null) {
      // If it does not exist, create it
      var fileNode = companyhome.createFile(CurrentYear);
      // Set the content to 1 (Default CounterValue value)
      fileNode.content = CounterValue;
   } else {
      // Add one to counter variable
      CounterValue= (parseInt(DiarieCounterFile.content)+1);
      // Set the content to the new counter value
      DiarieCounterFile.content = CounterValue;
   // Set up value
   var props = new Array(1);
   props["my:documentNumber"] = CurrentYear + '-' + utils.pad(CounterValue, 4);
   // Write the value to the document
   document.addAspect("my:documentNumber", props);

So, the problem I am having is that the object companyhome is not defined, neither is root nor userhome, as they are normally.

Why is that?
Is there a way around it?