AnsweredAssumed Answered

Calling functions

Question asked by dranakan on Feb 28, 2012
Latest reply on Feb 11, 2013 by dranakan
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.

Outcomes