Execute a script on cancel

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Member II

Execute a script on cancel

I have a workflow which makes some changes to permissions when it's started. It creates a new group adds the assignees to it and applies it to the attached document nodes. When the workflow finishes normally the end task runs a script that undoes all that. But if the workflow is canceled the cleanup script doesn't run.  How can I run that clean up when the workflow is manually cancelled? 

3 Replies
Highlighted
Established Member II

Re: Execute a script on cancel

Hi georgera,

Please check this, it might help:

Activiti User Guide: Event Types  

Regards.

Highlighted
Member II

Re: Execute a script on cancel

Hi. I can't seem figure out how to link that eventListener to the end task. I created an intermediateCatchEvent and a signal and pointed them all at each other but it's not triggering the endevent.

Here's my workflow definition:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlnsSmiley Surprisedmgdc="http://www.omg.org/spec/DD/20100524/DC" xmlnsSmiley Surprisedmgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
    <signal id="alertSignal" name="alert"></signal>
    <process id="GrantPermi" name="Grant Permissions" isExecutable="true">
        <extensionElements>
            <activiti:eventListener events="PROCESS_CANCELLED" signalName="alert" throwEvent="signal"></activiti:eventListener>
        </extensionElements>
        <startEvent id="alfrescoStartevent1" name="Alfresco start" activiti:formKey="wf:submitAdhocTask"></startEvent>
        <sequenceFlow id="flow1" sourceRef="alfrescoStartevent1" targetRef="alfrescoUsertask1"></sequenceFlow>
        <userTask id="alfrescoUsertask1" name="Alfresco User Task" activiti:assignee="${bpm_assignee.properties.userName}" activiti:formKey="DACGPWF:assigneeTask">
            <extensionElements>
                <activiti:taskListener event="create" class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">
                    <activiti:field name="script">
                        <activiti:string><![CDATA[
                                var groupName = "GrantPermi" + new Date().getTime();
                                var group = people.createGroup(groupName);
                                var user = people.getPerson(bpm_assignee.properties.userName);
                                people.addAuthority(group, user);
                                for (var i in bpm_package.children)
                                {                                    
                                    bpm_package.children[i].setPermission("Read", "GROUP_" + groupName);                                    
                                }
                                execution.setVariable('GroupName',groupName);]]></activiti:string>
                    </activiti:field>
                </activiti:taskListener>
            </extensionElements>
        </userTask>
        <sequenceFlow id="flow2" sourceRef="alfrescoUsertask1" targetRef="endevent1"></sequenceFlow>
        <endEvent id="endevent1" name="End">
            <extensionElements>
                <activiti:executionListener event="start" class="org.alfresco.repo.workflow.activiti.listener.ScriptExecutionListener">
                    <activiti:field name="runAs">
                        <activiti:string><![CDATA[admin]]></activiti:string>
                    </activiti:field>
                    <activiti:field name="script">
                        <activiti:string><![CDATA[
                                var groupName = execution.getVariable('GroupName');
                                for (var i in bpm_package.children)
                                {
                                    bpm_package.children[i].removePermission("Read", "GROUP_" + groupName );
                                }                            
                                groups.getGroup(groupName).deleteGroup();]]></activiti:string>
                    </activiti:field>
                </activiti:executionListener>
            </extensionElements>
        </endEvent>
        <boundaryEvent id="boundarytimer1" name="Timer" attachedToRef="alfrescoUsertask1" cancelActivity="true">
            <timerEventDefinition>
                <timeDuration>PT15M</timeDuration>
            </timerEventDefinition>
        </boundaryEvent>
        <sequenceFlow id="flow3" sourceRef="boundarytimer1" targetRef="endevent1"></sequenceFlow>
        <intermediateCatchEvent id="signalintermediatecatchevent1" name="SignalCatchEvent">
            <signalEventDefinition signalRef="alertSignal"></signalEventDefinition>
        </intermediateCatchEvent>
        <sequenceFlow id="flow4" sourceRef="signalintermediatecatchevent1" targetRef="endevent1"></sequenceFlow>
    </process>
    <bpmndi:BPMNDiagram id="BPMNDiagram_GrantPermi">
        <bpmndi:BPMNPlane bpmnElement="GrantPermi" id="BPMNPlane_GrantPermi">
            <bpmndi:BPMNShape bpmnElement="alfrescoStartevent1" id="BPMNShape_alfrescoStartevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="110.0" y="100.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="alfrescoUsertask1" id="BPMNShape_alfrescoUsertask1">
                <omgdc:Bounds height="55.0" width="105.0" x="270.0" y="90.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="boundarytimer1" id="BPMNShape_boundarytimer1">
                <omgdc:Bounds height="30.0" width="30.0" x="350.0" y="130.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="510.0" y="100.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="signalintermediatecatchevent1" id="BPMNShape_signalintermediatecatchevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="347.0" y="210.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
                <omgdi:waypoint x="145.0" y="117.0"></omgdi:waypoint>
                <omgdi:waypoint x="270.0" y="117.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
                <omgdi:waypoint x="375.0" y="117.0"></omgdi:waypoint>
                <omgdi:waypoint x="510.0" y="117.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
                <omgdi:waypoint x="380.0" y="145.0"></omgdi:waypoint>
                <omgdi:waypoint x="510.0" y="117.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
                <omgdi:waypoint x="364.0" y="210.0"></omgdi:waypoint>
                <omgdi:waypoint x="527.0" y="135.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
        </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
</definitions>

Highlighted
Intermediate

Re: Execute a script on cancel

You dont bind the event listener to the end task, you simply register it in the process definition.

This is described here: Activiti User Guide - Adding Listeners to Process Definitions 

Greg

bp3‌