cancel
Showing results for 
Search instead for 
Did you mean: 

Calling functions

dranakan
Champ on-the-rise
Champ on-the-rise
Hello,

Can we call functions in a Activit workflow ? I would like to put the same code in one place… How can I do this ?

For example I'am sending a mail at each userTask. Could I put the code in a function and call it ?

<?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:activiti="http://activiti.org/bpmn"
   xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
   xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema"
   expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://activiti.org/bpmn20">
   <process id="Finance3a2ca1ca" name="Finance invoice (begin with 3 users)">
      <startEvent id="start" name="Start"
         activiti:formKey="sgfwf:submitStartFinance3a2ca1ca"></startEvent>
      <userTask id="modifyReview1" name="Validation 1"
         activiti:assignee="${sgfwf_assignee1.properties.userName}"
         activiti:formKey="sgfwf:modifyReviewTaskFinance3a2ca1ca">
         <extensionElements>
            <activiti:taskListener event="create"
               class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">
               <activiti:field name="script">
                  <activiti:string>
if (typeof bpm_workflowDueDate != 'undefined') task.dueDate = bpm_workflowDueDate;
if (typeof bpm_workflowPriority != 'undefined') task.priority = bpm_workflowPriority;

var sendMail = task.getVariable('sgfwf_sendMailForAssignee1');
if (sendMail == true) {
   var mail = actions.create("mail");
   mail.parameters.to = sgfwf_assignee1.properties.email;
   mail.parameters.subject = "Validation : " + bpm_workflowDescription;
   //mail.parameters.from = "admin@alfresco.com";
   //mail.parameters.text = "Mail for you :-)";
   mail.parameters.template = companyhome.childByNamePath("Data Dictionary/Email Templates/Workflow Notification/wf-email.html_custom.ftl");
   var templateArgs = new Array();
   templateArgs['workflowTitle'] = task.name;
   // Link to Modify the task (workflowPooled=true => view, false => edit)
   templateArgs['workflowPooled'] = false;
   templateArgs['workflowPriority'] = task.priority;
   templateArgs['workflowDueDate'] = task.dueDate;
   templateArgs['workflowMainDocument'] = sgfwf_mainDocument.nodeRef.toString();
   templateArgs['workflowDescription'] = bpm_workflowDescription;
   templateArgs['workflowId'] = "activiti$"+task.id;
   var templateModel = new Array();
   templateModel['args'] = templateArgs;
   mail.parameters.template_model = templateModel;
   mail.execute(bpm_package);
}
</activiti:string>
               </activiti:field>
            </activiti:taskListener>
            <activiti:taskListener event="complete"
               class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">
               <activiti:field name="script">
                  <activiti:string>execution.setVariable('sgfwf_approveRejectOutcome',task.getVariable('sgfwf_approveRejectOutcome'));
execution.setVariable('sgfwf_financialData',task.getVariable('sgfwf_financialData'));</activiti:string>
               </activiti:field>
            </activiti:taskListener>
         </extensionElements>
      </userTask>
      <userTask id="modifyReview2" name="Validation 2"
         activiti:assignee="${sgfwf_assignee2.properties.userName}"
         activiti:formKey="sgfwf:modifyReviewTaskFinance3a2ca1ca">
         <documentation>
            Verify the arbitrary task was completed.
         </documentation>
         <extensionElements>
            <activiti:taskListener event="create"
               class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">
               <activiti:field name="script">
                  <activiti:string>
if (typeof bpm_workflowDueDate != 'undefined') task.dueDate = bpm_workflowDueDate;
if (typeof bpm_workflowPriority != 'undefined') task.priority = bpm_workflowPriority;
var sendMail = task.getVariable('sgfwf_sendMailForAssignee2');
if (sendMail == true) {
   var mail = actions.create("mail");
   mail.parameters.to = sgfwf_assignee2.properties.email;
   mail.parameters.subject = "Validation : " + bpm_workflowDescription;
   //mail.parameters.from = "admin@alfresco.com";
   //mail.parameters.text = "Mail for you :-)";
   mail.parameters.template = companyhome.childByNamePath("Data Dictionary/Email Templates/Workflow Notification/wf-email.html_custom.ftl");
   var templateArgs = new Array();
   templateArgs['workflowTitle'] = task.name;
   // Link to Modify the task (workflowPooled=true => view, false => edit)
   templateArgs['workflowPooled'] = false;
   templateArgs['workflowPriority'] = task.priority;
   templateArgs['workflowDueDate'] = task.dueDate;
   templateArgs['workflowMainDocument'] = sgfwf_mainDocument.nodeRef.toString();
   templateArgs['workflowDescription'] = bpm_workflowDescription;
   templateArgs['workflowId'] = "activiti$"+task.id;
   var templateModel = new Array();
   templateModel['args'] = templateArgs;
   mail.parameters.template_model = templateModel;
   mail.execute(bpm_package);
}
</activiti:string>


Thank you.
2 REPLIES 2

freesoft
Champ in-the-making
Champ in-the-making
Hi dranakan,

Yes it is definitely possible, you can externalize you functions to a javascript file :

create file : my-js-functions-.js

function sendMail(params){
//mail action goes here;
}

After that you can call this function wherever you want:


<activiti:string>
&lt;import resource="classpath:alfresco/extension/workflow/my-js-functions-.js"&gt;
sendMail(someParams);   
</activiti:string>

Regards.

dranakan
Champ on-the-rise
Champ on-the-rise
Thank you freesoft