Modify custom field depending on transition properties

JIRA Workflow post function : Modify custom field depending on transition properties

Name Modify custom field depending on transition properties
Version 0.1.0
Product Versions JIRA 3.7+
Author(s) Riccardo Govoni
Price free!
Download JAR cse-jiraext-workflows-0.1.jar
Download Source cse-jira-wflow-extensions-0.1.0.zip

Modify custom field from transition properties Workflow Plugin

This post function lets you modify a custom field in two ways :

  • clearing the values of a custom field, as described in Clear Custom Field value.
  • changing the value of a custom field with the value defined in a transition property.

This plugin is useful when you have a custom field whose value is in someway related with the current state of its associated issue. With the plugin you can automatically update the custom field value.
Example: suppose you have a custom field which defines the server where the issue/feature has been fixed/deployed ( test, staging, production ). You may use this plugin to automatically set this field to 'production' when the issue reaches the 'closed' status on the workflow.

Warning

The value-changing function actually works only with multiselect fields. Feel free to extend the plugin if you need to modify other types of custom fields. Otherwise, leave a comment on this page with the requested field type and, depending on my spare time, I may extend the plugin for you.

Installation and usage

  1. Download the zipped file. Unzip and place the jar file in the target directory in your atlassian JIRA webapp's lib folder
  2. Restart JIRA
  3. Add the post function to your workflow step(s)
  4. When adding a value-changing post function, edit the transition properties to define the values to be used

The value-changing post function uses the following algorithm to associate the custom field to be modified with the values to be used :

  • convert the custom field name to lowercase and replace all the white spaces with underscores ( _ )
  • append the suffix .newvalues
  • use the result to lookup the property and use the associated value to modify the custom field. For multiselect custom fields, you can use a comma separated list to specify multiple values .

For example , a custom field name My Custom Field will use the transition property my_custom_field.newvalues to obtain the values to be used.

How it works

The following piece of code defines the class associated with the value-changing post function ( the core of the plugin ) :

public class ModifyCustomFieldFunction implements FunctionProvider {
	
	public static final String VALUES_SUFFIX = ".newvalues";
	
	public void execute(Map transientVars, Map args, PropertySet ps)
			throws WorkflowException {

		Set<String> newValues = new HashSet<String>();
		
		// Lookup of the custom field to be modified
		String fieldId = (String) args.get("field.id");	
		CustomFieldManager cfm = ComponentManager.getInstance().getCustomFieldManager();
		CustomField field = cfm.getCustomFieldObject(fieldId);
		
		// the name which will be used for the lookup of the new values
		String fieldName = field.getName().toLowerCase().replace(' ', '_'); 
		
		// lookup of the current workflow
		MutableIssue mIssue = (MutableIssue) transientVars.get("issue");			
		JiraWorkflow wflow = null; 
		try {
			wflow = ManagerFactory.getWorkflowManager().getWorkflow(mIssue);
		}
		catch(com.atlassian.jira.workflow.WorkflowException we) {
			throw new WorkflowException("ModifyCustomFieldFunction: Unable to lookup the workflow associated with this issue",we); 
		}
		
		if (wflow == null) {
			throw new WorkflowException("ModifyCustomFieldFunction: Unable to lookup the workflow associated with this issue (null workflow)");
		}
		else {
			
			// Once we have the workflow, we retrieve the action descriptor associated
			// with the current transition			
			int curTransitionId = (Integer) transientVars.get("actionId");
			
			Collection actions = wflow.getAllActions();			
			for (Object o : actions) {
				ActionDescriptor ad = (ActionDescriptor) o;											
				if (curTransitionId == ad.getId()) {
							
					// transition properties lookup to identify the newvalues to be 
					// assigned to the customfield					
					boolean found = false;
					for (Object attr : ad.getMetaAttributes().entrySet()) {
						Map.Entry m = (Map.Entry) attr;
						if (m.getKey().toString().equals(fieldName + VALUES_SUFFIX)) {
							for (String s : m.getValue().toString().split(",")) {
								newValues.add(s.trim());
							}											
							found = true;
						}
						
					}
					
					if (!found) {
						StringBuilder sb = new StringBuilder();
						sb.append("Unable to find new values to be added to custom field " + field.getName() + " (id=" + fieldId + ") ");
						sb.append("using property " + fieldName + VALUES_SUFFIX + " for lookup." );
						sb.append("Plese contact your administrators");
						throw new WorkflowException(sb.toString());
					}
					
				}

			}
		} 
		
		
		// Integrate the custom field values
		if (newValues.size() > 0) {
			if (mIssue.getCustomFieldValue(field) != null) {
				
				// WARNING: this operation will fail if the custom field does not
				// use a collection of String to store its values !! 
				// This has been tested only for multiselect fields.
				
				newValues.addAll((Collection) mIssue.getCustomFieldValue(field));
			}
		}
		
		// Update the custom field
		mIssue.setCustomFieldValue(field, newValues);
			
	}

}

Screenshots

Labels

workflow workflow Delete
post post Delete
function function Delete
plugin plugin Delete
custom custom Delete
field field Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Oct 11, 2007

    Jira Always says:

    I could not see any of my custom fields listed in the drop-down list after insta...

    I could not see any of my custom fields listed in the drop-down list after installed this plugin. Any idea?

    I am using 3.9.2 enterprise edition. 

    Thanks,

    1. Oct 12, 2007

      Riccardo Govoni says:

      I'm sorry, but I haven't the possibility to verify your issue, since I have stop...

      I'm sorry, but I haven't the possibility to verify your issue, since I have stopped using JIRA some months ago. The last time I tested the plugin was with version 3.7 , don't know if the issue is related to version 3.9 ... you have to figure it out on your own :-/