cancel
Showing results for 
Search instead for 
Did you mean: 

Rule (and its script) not working as expected

srowsell
Champ in-the-making
Champ in-the-making
I have created a folder and a rule for this folder which will, upon file creation:
- change the node to a new content type (which is a child of cm:content); and
- execute a little script which is meant to populate two of its properties:

var results=search.luceneSearch("+PATH:\"/app:company_home/st:sites/cm:contract-management/cm:documentLibrary/cm:ActiveContracts//*\"+TYPE:\"{duca.contracts.model}contract\"");
var maxContractNumber=0;
for (var i=0;i<results.length;i++)
{
   if (results.properties["contract:contractID"]>maxContractNumber)
   {
      maxContractNumber=results.properties["contract:contractID"];
   }
}
document.properties["contract:contractID"]=maxContractNumber+1;
document.properties["contract:isActive"]=true;


(Basically I'm just incrementing the value of contractID so the user doesn't have to.  It's not a terribly meaningful value to a human.)

The node is changed to the content type as expected.  The query works and returns what I expect it to return.  The last two lines, however, appear to do nothing.  They definitely refer to properties of this model.

Can anyone think of a reason why this isn't working?  Does Alfresco just need more time between setting the content type and accessing its properties?
3 REPLIES 3

mitpatoliya
Star Collaborator
Star Collaborator
Add this line in the end

document.save();

srowsell
Champ in-the-making
Champ in-the-making
Yeah, that was it, thanks.

Another minor problem:  when I update that contractID property, I'm giving it an integer.  I even did this:

document.properties["contract:contractID"]=Math.round(maxContractNumber+1);

to make sure I was passing in an integer.  But the value I see in Alfresco is "3.0" or whatever.  Is there any way to make sure Alfresco takes an integer value, rather than a float/double/whatever?

zladuric
Champ on-the-rise
Champ on-the-rise
How did you define this property in your data model? That is probably your issue.
Or try passing the parseInt(maxContractNumber + 1) to the property.

Also, as a suggestion, you can make a property, or even a simple file or something on the data dictionary. Keep the max contract Id there. That way your script will run fast even when there are thousands of contracts already (as you're now looping them all).

So basically you have a file and it's property (could be anything, even in a page title.). Then in this script just go, fetch that number, increment it by 1, and assign it to the next contract. As an added value, you can even manually change the max contract number from there, or 'rewind' the counter if needed.