cancel
Showing results for 
Search instead for 
Did you mean: 

How can I extend a taskListener to move a document?

mafaldap
Star Contributor
Star Contributor

I need to move an approved document from one folder to the initiator home page folder. I think that I can do it from the workflow's task, adding script to this:

<userTask id="valutaCSS" name="Valuta Certificato di Servizio (semplice)" activiti:formKey="psw:activitiValutaCS">

      <extensionElements>

        <activiti:taskListener event="complete" class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">

          <activiti:field name="script">

            <activiti:string><![CDATA[if(task.getVariableLocal('psw_esitoValutazioneCS') == 'Approvato'){

    execution.setVariable('psw_prosegui', true);

    }else{

    execution.setVariable('psw_prosegui', false);

    }]]></activiti:string>

          </activiti:field>

        </activiti:taskListener>

      </extensionElements>

      <humanPerformer>

                <resourceAssignmentExpression>

                    <formalExpression>${bpm_assignee.properties.userName}</formalExpression>

                </resourceAssignmentExpression>

            </humanPerformer>

    </userTask>

I tried to change this code for a simple test:

var dest = companyhome.childByNamePath("Repository/Simulazioni");
for (var i = 0; i < bpm_package.children.length; i++)
{
  bpm_package.children[i].move(dest);
}

but it doesn't get the dest variable

1 ACCEPTED ANSWER

mafaldap
Star Contributor
Star Contributor

Ok, I solved this using your advices:

<extensionElements>

        <activiti:taskListener event="complete" class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">

          <activiti:field name="script">

            <activiti:string><![CDATA[if(task.getVariableLocal('psw_esitoValutazioneCS') == 'Approvato'){

    execution.setVariable('psw_prosegui', true);

            var nomeUtente = initiator.properties["cm:userName"]; //initiator.properties.userNamel'utente/"+nomeUtente); 

            var dest = companyhome.childByNamePath("/Home page del

  for (var i = 0; i< bpm_package.children.length; i++){ 

  bpm_package.children[i].move(dest); 

  }

    }else{

    execution.setVariable('psw_prosegui', false);

    }]]></activiti:string>

          </activiti:field>

          <activiti:field name="runAs">

          <activiti:string>admin</activiti:string>

          </activiti:field>

         

        </activiti:taskListener>

      </extensionElements>

Thanks everybody for the help!

View answer in original post

8 REPLIES 8

openpj
Elite Collaborator
Elite Collaborator

I think that the destination reference is wrong, try the following:

var dest = companyhome.childByNamePath("/Simulazioni"); 

Hope this helps Smiley Happy

mafaldap
Star Contributor
Star Contributor

Ok, thank you, now it works Smiley Happy

I'm trying to change the path so that every time it moves the file to the initiator user homepage. If I try:

var dest = companyhome.childByNamePath("/Home page dell'utente/MPapini");

It works, but when I tried

var percorso = "/Home page dell'utente/" + ${initiator.properties.userName};

var dest = companyhome.childByNamePath(percorso);

Alfresco returns me this:

org.activiti.engine.ActivitiException: Exception while invoking TaskListener: Exception while invoking TaskListener: 09220009 Failed to execute supplied script: missing ; before statement (AlfrescoJS#3)

mitpatoliya
Star Collaborator
Star Collaborator

You can move folder this way two points to consider here.

  • Make sure all user who will be working on that task have proper access on source and destination folder.
  • Make sure you are placing js in proper location at under particular event.

douglascrp
World-Class Innovator
World-Class Innovator

Mittal Patoliya​ For the first point, another option is to use the runas to set a user like system to perform the move.

By doing so, there will be no permission restriction.

So, if I want to move from a private folder to another private folder I have to use runas?

I'm reading some previous post about it but it's hard to figure out how to use runas here.

kaynezhang
World-Class Innovator
World-Class Innovator

Try to add following code to your  process definition

      <extensionElements> 

        <activiti:taskListener event="complete" class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener"> 

          <activiti:field name="script"> 

            <activiti:string><![CDATA[if(task.getVariableLocal('psw_esitoValutazioneCS') == 'Approvato'){

    execution.setVariable('psw_prosegui', true);

    }else{

    execution.setVariable('psw_prosegui', false);

    }]]>

  </activiti:string> 

          </activiti:field> 

   <activiti:field name="runAs">

               <activiti:string>

      admin

               </activiti:string>

          </activiti:field>

        </activiti:taskListener> 

      </extensionElements> 

mafaldap
Star Contributor
Star Contributor

Ok, I solved this using your advices:

<extensionElements>

        <activiti:taskListener event="complete" class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">

          <activiti:field name="script">

            <activiti:string><![CDATA[if(task.getVariableLocal('psw_esitoValutazioneCS') == 'Approvato'){

    execution.setVariable('psw_prosegui', true);

            var nomeUtente = initiator.properties["cm:userName"]; //initiator.properties.userNamel'utente/"+nomeUtente); 

            var dest = companyhome.childByNamePath("/Home page del

  for (var i = 0; i< bpm_package.children.length; i++){ 

  bpm_package.children[i].move(dest); 

  }

    }else{

    execution.setVariable('psw_prosegui', false);

    }]]></activiti:string>

          </activiti:field>

          <activiti:field name="runAs">

          <activiti:string>admin</activiti:string>

          </activiti:field>

         

        </activiti:taskListener>

      </extensionElements>

Thanks everybody for the help!