cancel
Showing results for 
Search instead for 
Did you mean: 

Starting a script

irene08
Champ in-the-making
Champ in-the-making
Good Day!

I really need your help in this script. I know the flow but I'm not sure how to start. I will execute a script. This is the content of my codes/javascript. To be short, I need the category to auto change from category(Reviewed) to category(Approval) if requiredapprovepercent is equal to actualpercent. This is inside the workflow process.


<activiti:taskListener event="complete" class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">
    <activiti:field name="script">
       <activiti:string>
               
        var Approval = file.properties["cm:category"];
               
        if (workflowname = activitiParallelReview || workflowname = activitiParallelGroupReview)
        {
          if (wf:requiredApprovePercent = 100 && wf:actualPercent = 100)
             {
              Alfresco.logger.debug(“Irene…Reading the if statement”);
              getCategory("Approval");
              Alfresco.logger.debug(“enabling the approval”);
             }
           if (wf_requiredApprovePercent == wf_actualPercent)
             {
              Alfresco.logger.debug(“Irene…Reading the 2nd if statement”);
              getCategory("Approval");
              Alfresco.logger.debug(“enabling the approval.. 2nd if statement”);
             }
      }
       </activiti:string>
    </activiti:field>
</activiti:taskListener>


Thank you. Please help.

Irene Smiley Happy
10 REPLIES 10

afaust
Legendary Innovator
Legendary Innovator
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.

Regards
Axel

irene08
Champ in-the-making
Champ in-the-making
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.

Regards
Axel

Hi!

Good Day!

Somehow I understand your example but its hard for me to translate it in my case. I don't know if I should declare a class or if it is a standalone script. And how to call a category.

Thank you,

Irene Smiley Happy

afaust
Legendary Innovator
Legendary Innovator
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.

Regards
Axel

irene08
Champ in-the-making
Champ in-the-making
Hi!

I really appreciate your help and reply.

I will do your sample script. And I'll let you know what will happen. But wait, Is the first line of your script is the declaration of variable? No need to declare a class? I will run the script in javascript console for me to know the errors.

Thank you,
Irene Smiley Happy

irene08
Champ in-the-making
Champ in-the-making
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.

Regards
Axel
_______________________________________________________________________________________________________________________
Hi Axel! Good Day!

I already combine/merge your script into my script. I have an error. Do you know what is the meaning of this?

500 Internal Error An error inside the HTTP server which prevented it from fulfilling the request. 09080022 Wrapped Exception (with status template): 09080178 Failed to execute script 'Javascript Console Script': 09080177 TypeError: Cannot read property "0.0" from null (5a377633a778ec638ecd525d46f2ac0a.js#15)

Here is the script.

//Tag category to Approval

   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 (workflowname == activitiParallelReview || workflowname == activitiParallelGroupReview)
         {
           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();
      }
   }      

Thank you,
Irene

irene08
Champ in-the-making
Champ in-the-making
Hi Axel!

TypeError: Cannot find function getSubCategories.

Did you encounter this error?

Thank you,
Irene

afaust
Legendary Innovator
Legendary Innovator
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.

Regards
Axel

irene08
Champ in-the-making
Champ in-the-making
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.

Regards
Axel
_________________________________________________________________________________________________________________________
Hi Axel!

I see. I thought you test your script. I really appreciate your reply. This is my latest script.
________________________________________________________________________________________________________________________
//Tag category to Approval

    var categoryToAdd = 0;
    var categories = classification.getRootCategories("cm:generalclassifiable");
    var remainingNames = ["Document Review Status", "Approval"];
    var wf_actualPercent = 100;
   
   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 = 0;
      
      for(;idx < max; idx++)
      {
      var category = categories[idx];
                     if (wf_actualPercent >= 100)
                        {
                          if(remainingNames.length == 1)
                            {
                             categoryToAdd = category;
                            }
                        else
                           {
                             category.subCategories();
                             remainingNames.shift();
                                  logger.log(category.subCategories);
                                  logger.log("test");
                                  break;
                            }
                         }
                 }
    }

I executed this script and unfortunately no returning values.

Thank you,
Irene

afaust
Legendary Innovator
Legendary Innovator
Hello Irene,

without modifying a workflow, creating category and rules, it is a bit difficult (and otherwise too time-consuming) to test a script that I only intended to be used as a hint.

Your last script is shorter than the previous one - it lacks the part that actually changes the node. Is that by purpose? The script is not intended to return a value or do you mean missing output in your log?
Please remove the superflous call "category.subCategories()" in the first line of your last else block.

I'd advise you use the JavaScript debugger to further investigate. See http://techblog.zabuchy.net/2012/debugging-javascript-in-alfresco/ or http://docs.alfresco.com/3.4/index.jsp?topic=%2Fcom.alfresco.Enterprise_3_4_0.doc%2Ftasks%2Fws-contr... on how to activate it, and https://developer.mozilla.org/en-US/docs/Rhino/Debugger for general information on the debugger. Using this, you can inspect the variables at each step of your script without having to insert a lot of log statements.

Regards
Axel