Links
TrackStudio Enterprise 3.5
Calculating a Custom Field Value

The following topic describes how to use scripts to calculate a custom field value.

To calculate a custom field value:
  1. Use the Current User -> Scripts... menu item to create Task / Custom Field Value or User / Custom Field Value script.
  2. Add a custom field and fill in the Script property for it.

To return the day of the week on which an issue was created use the following Task / Custom Field Value script:

Calendar ca = task.getSubmitdate();
ca.setTimeZone(TimeZone.getTimeZone(sc.getTimezone()));
int day = ca.get(Calendar.DAY_OF_WEEK);
switch (day){
     case Calendar.SUNDAY: return "Sunday";
     case Calendar.MONDAY: return "Monday";
     case Calendar.TUESDAY: return "Tuesday";
     case Calendar.WEDNESDAY: return "Wednesday";
     case Calendar.THURSDAY: return "Thursday";
     case Calendar.FRIDAY: return "Friday";
     case Calendar.SATURDAY: return "Saturday";
}
return null;

 

Associated custom field properties:

Property 
Value 
Type 
List 
List of Values 
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday 

To collect text from all messages for the current task use the following Task / Custom Field Value script:

String s = "";
for(Iterator it = task.getMessages().iterator(); it.hasNext();) {
    String desc = it.next().getDescription();
    if (desc != null) {
      s += desc + " | ";
    }
}
return s;

Associated custom field properties:

Property 
Value 
Type 
String 

To calculate a summary of the actual time for all not-closed tasks, use the following Task / Custom Field Value script:

public double getAbudget(Object t) {
  double d = 0;
  if(t.getActualBudget() != null && t.getClosedate() == null && t.getChildrenCount() == 0)
    d = t.getActualBudget().doubleValue();
  for(Iterator it = t.getChildren().iterator(); it.hasNext();)
    d += getAbudget(it.next());
  d += t.getActualBudget().doubleValue()-d;
  return d;
}
return getAbudget(task);

Associated custom field properties:

Property 
Value 
Type 
Float 

To list all custom fields with values for the current task, use the following Task / Custom Field Value script:

String s = "";
ArrayList udfs = task.getUDFValuesList();
for(Iterator it = udfs.iterator(); it.hasNext();) {
    SecuredUDFValueBean udf = (SecuredUDFValueBean)it.next();
    s += udf.getCaption() + ":";
    Object value = udf.getValue(task);
    s +=  ((value!=null) ? value : "null") + "|";
}
return s;

Associated custom field properties:

Property 
Value 
Type 
String 

To return the number of days since the last update (this field can be used to find out what tasks have been neglected for a long time), use the following Task / Custom Field Value script:

return ((new java.util.Date()).getTime() - task.getUpdatedate().getTime().getTime())/DAYS;

Associated custom field properties:

Property 
Value 
Type 
Integer 

To show favorite links use the following Task / Custom Field Value script:

return "<table>"+
"<tr><th>Site</th><th>URL</th></tr>"+
"<tr><td>Google</td><td><a href=\"http://www.google.com\">Click here</a></td></tr>"+
"<tr><td>MSN</td><td><a href=\"http://www.msn.com\">Click here</a></td></tr>"+
"</table>";

Associated custom field properties:

Property 
Value 
Type 
Memo 
HTML View 
checked 

To get the logged-in user's login id, use the following Task / Custom Field Value script:

ArrayList arr = new ArrayList();
arr.add(sc.getUser().getLogin());
return arr;

Associated custom field properties:

Property 
Value 
Type 
User 

To get a task list, use the following script:

ArrayList arr = new ArrayList();
arr.add("1");
arr.add("5");
arr.add("10");
return arr;

Associated custom field properties:

Property 
Value 
Type 
Task 

To get multiple String values, use the following script:

ArrayList arr = new ArrayList();
arr.add("Sunday");
arr.add("Monday");
return arr;

Associated custom field properties:

Property 
Value 
Type 
Multiple List 
List of Values 
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday 

To calculate the number of months between the task submit date and the current date, use the following Task / Custom Field Value script. It can be used to find tasks submitted this month, last month, etc.

import java.util.Date;
Calendar ca = Calendar.getInstance();
ca.setTime(new Date(task.getSubmitdate().getTime().getTime()));
int submitMonth = ca.get(Calendar.MONTH);
int submitYear = ca.get(Calendar.YEAR);
ca.setTime(new Date());
int currentMonth = ca.get(Calendar.MONTH);
int currentYear = ca.get(Calendar.YEAR);
return (currentYear - submitYear)*12 + currentMonth - submitMonth;

Associated custom field properties:

Property 
Value 
Type 
Integer 

To get the value of the task- or workflow-based custom field Order Availability:

String availability = AdapterManager.getInstance().getSecuredUDFAdapterManager()
     .getTaskUDFValue(sc,task.getId(),"Order Availability");
if (availability == null)
   return "Unknown";
else
   return availability;

To get the value of the user-based custom field Address:

String address = AdapterManager.getInstance().getSecuredUDFAdapterManager()
     .getUserUDFValue(sc,task.getSubmitter().getId(),"Address");
if (address == null)
   return "Unknown address";
else
   return address;

Associated custom field properties:

Property 
Value 
Type 
String 

To show the link to attachments for tasks, use the following Task / Custom Field Value script:

if (task.hasAttachments()) {
   StringBuffer rez = new StringBuffer();
   for (Iterator it = task.getAttachments().iterator(); it.hasNext();) {
      SecuredAttachmentBean att = (SecuredAttachmentBean) it.next();
      rez.append("<a target=\"blank\" href=\"");
      rez.append(com.trackstudio.startup.Config.getInstance().getSiteURL());
      rez.append("/DownloadAction.do?method=download&session=");
      rez.append(task.getSecure().getId());
      rez.append("&id=");
      rez.append(att.getId());
      rez.append("\">");
      rez.append(att.getName());
      rez.append("</a>");
      rez.append("<br />");
   }
   return rez.toString();
} else
   return "";

Associated custom field properties:

Property 
Value 
Type 
String 
HTML View 
checked 
Cache Values 
unchecked