Script to Generate Serial Number for Child Folders in Alfresco

Recently I have come across one of the requirement where customer have particular name pattern for all spaces (folders). So, irrespective of whatever name given by creator alfresco should generate one number based on pattern and should apply that name to folder.

One example of such pattern is like “abc-xyz-comp-<serialno>”

Where serialno à refers to Serial number generated. We need to maintain one counter where each of these folders needs to give incremental number like “abc-xyz-comp-1”, “abc-xyz-comp-2”….. And so on.

Now question is how to achieve this?

For this first requirement we need to maintain one counter which keep track of last serial number.

Here we are creating one counter aspect which keeps track of this counter.

<aspect name=" test:counter ">

         <title>Counter</title>

         <properties>

           <property name="test:countervalue">

               <title>Counter Value</title>

               <type>d:int</type>

           </property>

         </properties>

</aspect>

Now apply this aspect on parent folder under which you want to apply that pattern.

Next step would be create script which will read this counter value, create new name of new folder, rename new folder according to pattern name, Increment counter value.

RenameFolderScript.js

 var NewDocID=1;
var prefixstring="abc-xyz-comp-";
    if(!space.hasAspect("trans:counter"))
    {
        var props1 = new Array(1);
        props1["trans:countervalue"] =1;
        space.addAspect("trans:counter",props1);
        space.save();
    }else
    {
        var LastDocID = space.properties["test:countervalue"];
        NewDocID = LastDocID + 1;
        space.properties["test:counter"] = NewDocID;
        space.save();
    }
var props = new Array(2);
props["test:counter"] =NewDocID;
props["test:transmittalno"] =prefixstring+NewDocID;
document.addAspect("test:counter",props);
document.name=document.name+NewDocID;
document.save();

Now upload this script under Company Home>Data Dictionary>Scripts from alfresco user interface.

Create a Rule on Parent space with following set of parameters. This rule will be executed whenever new space is being created inside it. This rule will invoke RenameFolder.js script and rename folder according to our require pattern.

Rule to rename Folder with pattern

Rule to rename Folder with pattern

Summary: This is one more great use of alfresco javascript which we had discussed earlier in this post. In this post I have used Space and Folder interchangeably both of them are same in context to alfresco.

Looking for quality Alfresco Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , , , , , , , | Posted under Alfresco | RSS 2.0

Author Spotlight

mitpatoliya

mitpatoliya

I love opensource technologies working with those technologies from the time I have stepped in to the Software Industry. Alfresco CMS is my area of expertise. I have worked on various complex implementations which involved integration of Alfresco with other technologies, extensively worked with JBPM workflows and Webscripts.

4 Comments on Script to Generate Serial Number for Child Folders in Alfresco

  1. Better yet, you could use one of the OOTB aspects.

    The cm:countable aspect is defined like this:

    Countable

    d:int

    d:int

  2. Trying again to fix the code:

    <aspect name="cm:countable"> &lt;title&gt;Countable&lt;/title&gt; <properties> <property name="cm:hits"> <type>d:int</type> </property> <property name="cm:counter"> <type>d:int</type> </property> </properties> </aspect>

Leave a Reply

Your email address will not be published. Required fields are marked *


9 + 5 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>