This topic describes how to use scripts to calculate a custom field value.
To calculate a custom field value:
To return the day of the week on which an issue was created use the following task processing script:
gran.tools.Logger log = new gran.tools.Logger("script"); log.debug("start script"); Calendar ca = Calendar.getInstance(); ca.setTime(new Date(task.getSubmitdate().getTime())); 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 processing 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 budget for all not-closed tasks, use the following task processing script:
public double getAbudget(Object t) { double d = 0; if(t.getActualBudget() != null && t.getClosedate() == null) d = t.getActualBudget().doubleValue(); for(Iterator it = t.getChildren().iterator(); it.hasNext();) d += getAbudget(it.next()); 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 processing script:
String s = ""; Map udfs = task.getUDFValues(); for(Iterator it = udfs.keySet().iterator(); it.hasNext();) { Object udf = udfs.get(it.next()); s += udf.getCaption() + ":"; s += udf.getValue(task) + "|"; } 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 processing script:
((new Date()).getTime() - task.getUpdatedate().getTime())/DAYS
Associated custom field properties:
Property |
Value |
Type |
Integer |
To show the link to Task->Uploads for tasks with custom fields use the following task processing script:
if (task.hasAttachments()) return gran.app.Config.getInstance().getSiteURL() +"/jsp/task/viewtask/uploads/Uploads.jsp?TSSESSION="+task.getSecure().getId() +"&ID="+task.getId() +"\nDownload"; else return "";
Associated custom field properties:
Property |
Value |
Type |
URL |
To get a logged user login use the following task processing script:
ArrayList arr = new ArrayList(); arr.add(task.getSecure().getUser().getLogin()); return arr;
or the following user processing script:
ArrayList arr = new ArrayList(); arr.add(user.getSecure().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 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 |