How to change the value of custom field in a task

It is quite simple to change the value of custom field while editing the task: all the custom fields are contained in HashMap inside SecuredTaskTriggerBean, with which the triggers for tasks are handled. They are stored in the form of pairs “Heading”, “Value”, whereas the value is assigned in the type string.
Thus, for changing the value of the field, the following function needs to be called in the trigger Task Edit/Before :

public SecuredTaskTriggerBean execute(SecuredTaskTriggerBean task) throws GranException {
...
task.setUdfValue("Field Heading", "Value");
...

The value, assigned in Before-trigger is saved automatically. It needs to be saved by calling the function task.update() in the triggers InsteadOf and After.
Note: here task is the parameter assigned to the trigger
Similarly, value of the custom field of the task can be changed in the After-trigger, by executing:

SecuredUDFAdapterManager am = AdapterManager.getInstance().getSecuredUDFAdapterManager();
am.setTaskUDFValueSimple(task.getSecure(), task.getId(), "date_udf", "11.03.11 17:55");

Note:

  • here task is the parameter assigned to the trigger
  • date_udf - heading of the field
  • assigned parameter - date and time. Their format depends upon the locale of user, running the script. Therefore, the most precise way to assign the date and time is:
            Calendar now = Calendar.getInstance();
            now.setTimeZone(TimeZone.getTimeZone(securedTaskBean.getSecure().getUser().getTimezone()));
            now.set(Calendar.HOUR_OF_DAY, 17);
            now.set(Calendar.MINUTE, 55);
            now.set(Calendar.SECOND, 0);
            now.set(Calendar.MILLISECOND, 0);
            now.set(Calendar.MONTH, Calendar.MARCH);
            now.set(Calendar.YEAR, 2011);
            now.set(Calendar.DAY_OF_MONTH, 11);
            String df = securedTaskBean.getSecure().getUser().getDateFormatter().parse(now);

In this case the value of field will correspond to March 11, 2011 17:55 as per the time zone of the user, running the script. If you need to set the time in some other format or in some other time zone, you need to use the respective Locale and TimeZone.
While changing the value of custom field by the method, mentioned above, this value doesn’t go to HashMap of trigger, though after After-trigger this data will not be used at all.

If the value of custom field needs to be changed not at the time of editing of task, but, let us say, in bulk-script, in that case we need to first create and then initialize SecuredTaskTriggerBean.

This is done very simply, if value is required to be assigned in already existing task and its id or number are known (their values can be obtained, for example, by selecting the tasks from the list).

...
/* Creating SecuredTaskBean through id*/
SecuredTaskBean theTask = new SecuredTaskBean(id, sc); // sc- SessionContext, it can be obtained from any secured-object, by 
calling getSecure()

/* Creating SecuredTaskTriggerBean */
SecuredTaskTriggerBean sttb = new SecuredTaskTriggerBean(theTask, new HashMap()); // HashMap – it is our list of custom fields, and it can now be passed as initialized. 
/* Setting the required value */
Sttb.setUdfValue(“Heading of field”, “Value”);
Sttb.update();
...