cancel
Showing results for 
Search instead for 
Did you mean: 

Script triggered by rule can't iterate over folder

Romanesco
Champ in-the-making
Champ in-the-making

Hi,

I made a script in javascript saved in Alfresco Share in the repository.

The goal of this script is to iterate over the folder (linked to rule of triggering this script) and to log all the documents in this folder.

The code of this script:

function control_block(folder_to_control){
    if (folder_to_control && folder_to_control.isContainer) {
        var children = folder_to_control.children;
        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            if (child.isDocument) {
                logger.log(child.name);
            }
        }
    }
	else {
		logger.log("ERROR: folder to iterate don't exists");
	}
	return null;
}
function main(doc) { control_block(doc); } main(document);

Here are the rules of a folder in the repository:
- when an object enter the folder
- Execute the script

The problem is that in the logs it goes directly in the else block and says that the folder is empty:
2023-04-14 10:54:27,957 DEBUG [repo.jscript.ScriptLogger] [http-nio-8080-exec-1] ERROR: folder to iterate don't exists


I tried several other options like:

function control_block(folder_to_control){
	var folder = search.findNode(folder_to_control);
	...

But with the same results. I also tried to use directly the document variable like:

function control_block(){
    if (document && document.isContainer) {
        var children = document.children;
        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            if (child.isDocument) {
                logger.log(child.name);
            }
	...

What is my error in all this? It seems to me that the document variable refers to the folder that is linked to the rule right?

Config:
Alfresco 7.2

Best regards.

1 REPLY 1

angelborroy
Community Manager Community Manager
Community Manager

Just change your line

main(document);

to

main(document.parent);

The rule "document" is the one that was created in the folder.

Hyland Developer Evangelist
Getting started

Explore our Alfresco products with the links below. Use labels to filter content by product module.