cancel
Showing results for 
Search instead for 
Did you mean: 

JavaScript fails to add category

nicolasraoul
Star Contributor
Star Contributor
Hello all,

For test purposes, I want to add a random category to all content added to Alfresco.
So I created a rule that calls a JavaScript. But I am stuck at a basic step, even before implementing randomization.

The following JavaScript tries to add an existing category ("Slovakia") to the document, but it does not work:

document.addAspect("cm:generalclassifiable");
var nodeRef = search.luceneSearch("Slovakia")[0];
var categories = new Array(10);
categories.push(nodeRef);
document.properties["cm:categories"] = categories;

It runs without errors, but when I look at the created document afterwards, it has no categories. The details view shows: "This document does not yet have any categories applied".
I am sure the rule is executed, because when I add a typo an error appears.
Using Alfresco's JavaScript debugger, I checked that nodeRef is indeed a category node.

Same result if I load the category's node reference directly rather than using Lucene:
var nodeRef = search.findNode("workspace://SpacesStore/35fbe20e-3c33-4cfb-9ce5-97ad433e5d4b");

Something else is strange, could be the same problem. If I append this:
document.properties["cm:description"] = "Hello";
Then the description is not affected.

A recent thread talked about a problem when adding categories containing a space character to an existing document that already has categories. My problem is different because there is no space character, and the document does not have any category beforehand.
I am using Alfresco 3.2 Community 3.2r2

Thanks a lot!
Nicolas Raoul
2 REPLIES 2

mryoshio
Champ in-the-making
Champ in-the-making
Hi Nicolas,

I experienced same issue in the past years and solved it by adding one line at the end of javascript code.

document.save();

Please try this.

Regards,
YOSHIOKA Ashitaba@aegif corporation

nicolasraoul
Star Contributor
Star Contributor
That did the trick!
Thanks a lot 🙂
Nicolas