As we transition to the new Hyland Community, some post links may be temporarily disrupted as we finalize redirects. We apologize for the inconvenience and are working to resolve this as quickly as possible. Details.
Hello Irene,this appears to be the same question as in https://forums.alfresco.com/en/viewtopic.php?f=5&t=46501. I provided an example there that - although not directly applicable - can be easily adapted to your use case. Your code does not seem to have evolved much from the first post. Did you have trouble with the example? Your last response in that thread did not indicate any problems.RegardsAxel
var categoryToAdd = null; var categories = classification.getRootCategories("cm:generalclassifiable"); var remainingNames = ["Document Review Status", "Approval"]; while(categoryToAdd == null && categories && remainingNames.length > 0){ var idx = 0, max = categories.length, innerCategories = categories; // reset categories to prevent further iterations if we don't find a match categories = null; for(;idx < max; idx++){ var category = categories[idx]; if(category.name.equals(remainingNames[0])){ if(remainingNames.length == 1){ categoryToAdd = category; }else{ categories = category.getSubCategories(); remainingNames.shift(); break; } } } }
if(categoryToAdd != null){ var oldCategories = document.properties["cm:categories"]; var newCategories = oldCategories != null ? oldCategories : []; newCategories.push(categoryToAdd); document.properties["cm:categories"] = newCategories; document.save(); }
Hello Irene,my example is a JavaScript. You can just incorporate it into your existing script in the appropriate places.The following fragment from my example already provides the code necessary to get a specific category from a specific branch in the category tree (the remainingNames variable can be thought of as a path to your category): var categoryToAdd = null; var categories = classification.getRootCategories("cm:generalclassifiable"); var remainingNames = ["Document Review Status", "Approval"]; while(categoryToAdd == null && categories && remainingNames.length > 0){ var idx = 0, max = categories.length, innerCategories = categories; // reset categories to prevent further iterations if we don't find a match categories = null; for(;idx < max; idx++){ var category = categories[idx]; if(category.name.equals(remainingNames[0])){ if(remainingNames.length == 1){ categoryToAdd = category; }else{ categories = category.getSubCategories(); remainingNames.shift(); break; } } } } Then you only need to use the category reference (a ScriptNode, since the category is a full content object) to modify the cm:categories property (the collection of applied categories) of any document you wish to update. This is what this fragment is for: if(categoryToAdd != null){ var oldCategories = document.properties["cm:categories"]; var newCategories = oldCategories != null ? oldCategories : []; newCategories.push(categoryToAdd); document.properties["cm:categories"] = newCategories; document.save(); } It takes the category that we want to add and inserts it into the collection of applied categories. In your case, you may need to remove the previous state category, i.e. the category you want to replace due to the approval state change.RegardsAxel
Hello Irene,as I wrote, I did not run / test the script, simply wrote it based on the API and previous, similar scripts.For "getSubCategories()" you may try the alternative "category.subCategories" method of access.Do you have a line number for the "0.0 from null" error? There is only one access in the code where "0" is used as an index and I don't see that as a possible candidate.RegardsAxel