cancel
Showing results for 
Search instead for 
Did you mean: 

Script within Workflow

americanolinhar
Champ on-the-rise
Champ on-the-rise
Good morning,

I would like to run a script within a custom workflow. This script must be run as soon as the User Approve the task.
The idea I had so far was to create a custom property (flag) to a document and update its value to true when the dcumento is approved. And then create a folder rule that calls the script when this property(flag) is updated.
But I prefer this script to be executed within the workflow directly.
Does anyone have any idea / suggestion to help me?

The script is attached

1 REPLY 1

darkredd
Star Contributor
Star Contributor
Hi Frederico,

A workflow user Task has 3 events that can be invoked to achieve certain things.

1. Assignment = this fires when the sequence flow preceding it "ends". This is the first event to happen and even if the task ultimately is not created this event will fire.
2. Create = Upon successful creation of the "UserTask" this event is triggered.
3. Complete = This triggers on a successful completion of a task, if there was an error the task will not complete and the event won't be triggered.

That being said here is a way you can achieve your goal on your "Approval" user task:

        <activiti:taskListener event="complete" class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">
          <activiti:field name="script">
            <activiti:string><![CDATA[<import resource="classpath:alfresco/extension/workflows/scripts/updateMetadata.js">]]></activiti:string>
          </activiti:field>
        </activiti:taskListener>

It is important to note that whatever code you run inside the user Task, the assigned user has right to perform such instructions, unless you explicitly mention that the code be executed as admin. In this case you will need to add the following code on the tasklistener event:

<activiti:field name="runAs">
   <activiti:string><![CDATA[admin]]></activiti:string>
</activiti:field>


Hope this answers your question.