Workflow Activity Scripts¶
You can assign a workflow activity script to state transitions in workflows.
A workflow activity script is executed when the workflow transition to which the script is assigned is initiated. For example, you can automatically change the person responsible or create a subordinate item when the item status changes.
Examples¶
The following script appends a comment to the end of the description field when an item state has been changed.
import java.util.LinkedList
import java.util.List
import java.util.Map
import com.aurel.track.beans.TWorkItemBean
import com.aurel.track.errors.ErrorData
import com.aurel.track.util.event.IEventHandler
public class CommentByReopen implements IEventHandler {
private static String REOPEN_SEPARATOR =
"**************************REOPEN*******************************"
public Map<String, Object> handleEvent(Map<String, Object> inputBinding) {
TWorkItemBean workItemBean = (TWorkItemBean)inputBinding.get("issue")
String comment = workItemBean.getComment()
if (comment==null || comment.trim().length()==0) {
List<ErrorData> errorList = new LinkedList<ErrorData>()
errorList.add(new ErrorData("Comment is required by reopen"))
inputBinding.put("errorList", errorList)
} else {
String description = workItemBean.getDescription()
if (description==null) {
description = ""
}
description = description + REOPEN_SEPARATOR + comment
workItemBean.setDescription(description)
workItemBean.setComment(null)
}
return inputBinding
}
}
The next script changes the responsible to the “guest” user:
//
// This script changes the responsible to "guest" when a status change occurs
//
import com.aurel.track.beans.TWorkItemBean;
import com.aurel.track.util.event.IEventHandler;
import com.aurel.track.errors.ErrorData;
import com.aurel.track.dao.DAOFactory;
import com.aurel.track.dao.PersonDAO;
import com.aurel.track.beans.TPersonBean;
public class ChangeResponsible implements IEventHandler {
def public Map handleEvent(Map inputBinding) {
TWorkItemBean workItemBean = inputBinding.get("issue");
PersonDAO personDAO =
DAOFactory.getFactory().getPersonDAO();
TPersonBean personBean =
personDAO.loadByLoginName("guest");
workItemBean.setResponsibleID(personBean.getObjectID());
}
}
The next script changes the manager to the first person belonging to a group of persons in a specific role (“roleNew”):
//
// Change the manager to the first person that belongs to a group
// in role "roleNew" upon a status change
//
import com.aurel.track.beans.TWorkItemBean;
import com.aurel.track.util.event.IEventHandler;
import com.aurel.track.errors.ErrorData;
import com.aurel.track.dao.DAOFactory;
import com.aurel.track.dao.PersonDAO;
import com.aurel.track.beans.TPersonBean;
import com.aurel.track.dao.RoleDAO;
import com.aurel.track.beans.TRoleBean;
public class ChangeManager implements IEventHandler {
def public Map handleEvent(Map inputBinding) {
TWorkItemBean workItemBean = inputBinding.get("issue");
PersonDAO personDAO =
DAOFactory.getFactory().getPersonDAO();
RoleDAO roleDAO= DAOFactory.getFactory().getRoleDAO();
TRoleBean roleBean = roleDAO.loadByName("roleNew");
if (roleBean!=null) {
List<TPersonBean> personsWithExchangeRole =
personDAO.loadByProjectAndRoles(
workItemBean.getProjectID(),
roleBean.getObjectID());
if (personsWithExchangeRole!=null &&
personsWithExchangeRole.size() > 0) {
TPersonBean personBean =
(TPersonBean)personsWithExchangeRole.get(0);
workItemBean.setOwnerID(
personBean.getObjectID());
}
}
}
}
The next script copies an item and creates a link between the copy and the original:
import java.util.LinkedList
import java.util.List
import java.util.Locale
import java.util.Map
import com.aurel.track.beans.TLinkTypeBean
import com.aurel.track.beans.TPersonBean
import com.aurel.track.beans.TWorkItemBean
import com.aurel.track.errors.ErrorData
import com.aurel.track.fieldType.runtime.base.WorkItemContext
import com.aurel.track.item.ItemBL
import com.aurel.track.item.link.ItemLinkConfigBL
import com.aurel.track.linkType.LinkTypeBL
import com.aurel.track.util.event.IEventHandler
public class CopyItem implements IEventHandler {
/**
* Copies an item and creates a link between the original item and copied item
*/
public Map<String, Object> handleEvent(Map<String, Object> inputBinding) {
WorkItemContext workItemContext = (WorkItemContext)inputBinding.get(
"workItemContext")
TWorkItemBean workItemBean = workItemContext.getWorkItemBean()
Integer copiedFromItemID = workItemBean.getObjectID()
TPersonBean personBean = (TPersonBean)inputBinding.get("user")
Locale locale = (Locale)inputBinding.get("locale")
List<ErrorData> errorList = new LinkedList<ErrorData>()
//create a copy of an item
WorkItemContext copyContext = ItemBL.editCopyWorkItem(copiedFromItemID, personBean, locale)
TWorkItemBean workItemBeanCopy = copyContext.getWorkItemBean()
//change the copied item as needed
workItemBeanCopy.setSynopsis("(Copy) " + workItemBeanCopy.getSynopsis())
//save the copied item in db
Integer copiedItemID = ItemBL.copyWorkItem(copyContext,
errorList, false)
//look up the linktype ID in Link types config, ID column
Integer linkTypeID = 1001
// 1=Unidirectional outward and bidirectional links,
// 2=Unidirectional inward (MsProject)
Integer linkDirection = 1
List<ErrorData> linkErrors = ItemLinkConfigBL.saveItemLink(
copiedFromItemID, copiedItemID, linkTypeID, linkDirection,
null, null, personBean, locale, null, workItemContext)
if (linkErrors!=null) {
errorList.addAll(linkErrors)
}
inputBinding.put("errorList", errorList)
return inputBinding
}
}
The following script shows how to access custom fields:
import com.aurel.track.beans.TWorkItemBean;
import com.aurel.track.util.event.IEventHandler;
import com.aurel.track.errors.ErrorData;
import com.aurel.track.admin.customize.treeConfig.field.FieldDesignBL;
import com.aurel.track.beans.TFieldBean;
public class ConnectNavision {
private String CRLF=System.getProperty("line.separator");
public Map handleEvent (Map<String, Object> inputBinding) {
TWorkItemBean workItemBean = inputBinding.get("issue");
// PLZ is a custom field
List<TFieldBean> fieldBeans = FieldDesignBL.loadByName("ZIP Code");
TFieldBean fieldBean=fieldBeans.getFirst();
Integer fieldID = fieldBean.getObjectID();
String fname="NAV "+String.valueOf(workItemBean.getObjectID());
def file=new File("C:\temp\\"+fname+".txt");
if (file.exists()){
file.delete();
}
file << workItemBean.getObjectID()+CRLF;
file << workItemBean.getSynopsis()+CRLF;
file << workItemBean.getAttribute(fieldID)+CRLF;
return inputBinding;
}
}