Say you wish to prompt the user for something simple (a case reference ID from an external system, for example), and based on what they enter, run code which queries an external system and populates various issue custom fields. This can be done with a a workflow function which you can set to be invoked immediately after the issue is created.
Attached is a sample workflow function, setting one field value to another, to get you going. It can be unpacked into the developer kit examples/ directory, and run 'maven jar' from there. The generated .jar should be dropped into JIRA's WEB-INF/lib. For reference, here is what the actual code looks like:
package com.atlassian.jira.copyfieldsfunction; import com.opensymphony.workflow.FunctionProvider; import com.opensymphony.workflow.WorkflowException; import com.opensymphony.module.propertyset.PropertySet; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.ModifiedValue; import com.atlassian.jira.issue.fields.CustomField; import com.atlassian.jira.ManagerFactory; import java.util.Map; /** * Sets one custom field to the value of another. */ public class CopyFieldsFunction implements FunctionProvider { public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException { Issue issue = (Issue) transientVars.get("issue"); String sourceFieldName = "foo"; String targetFieldName = "bar"; CustomField sourceCF = ManagerFactory.getCustomFieldManager().getCustomFieldObjectByName(sourceFieldName); if (sourceCF == null) throw new WorkflowException("No custom field '" + sourceFieldName + "' found."); CustomField targetCF = ManagerFactory.getCustomFieldManager().getCustomFieldObjectByName(targetFieldName); if (targetCF == null) throw new WorkflowException("No custom field '" + targetFieldName + "' found."); Object value = issue.getCustomFieldValue(sourceCF); System.out.println("Setting "+targetFieldName+" to " + value); System.out.println("oldvalue isssss "+issue.getCustomFieldValue(targetCF)); try{ FieldLayout fieldLayout = ComponentManager.getInstance().getFieldLayoutManager().getFieldLayout(issue.getGenericValue()); FieldLayoutItem fieldLayoutItem = fieldLayout.getFieldLayoutItem(targetCF.getId()); targetCF.updateValue( fieldLayoutItem, issue, new ModifiedValue(issue.getCustomFieldValue(targetCF), value), new DefaultIssueChangeHolder()); } catch (FieldLayoutStorageException e) { throw new WorkflowException(e); } } }
To set the workflow function to be run on issue creation, go to Admin -> Global Settings -> Workflows, copy the default 'jira' workflow, and click 'Steps' to edit it. The initial 'Create issue' transition is not visible here by default, so click the first 'Open' step, and then click the 'Create Issue' transition link. Click the 'Post Functions' tab here, and add the new function.
To hide the fields that will be automatically set on the 'create issue' page:
- Go to Admin -> Issue Fields -> Screens, and create a 'Create Issue Screen' by copying the default screen. Click to edit, and 'remove' the automatically populated field(s).
- Create a new Screen Scheme, click Configure, and set your 'Create Issue Screen' to show when creating issues.
- Create a new Issue Type Screen Scheme, and associate your new Screen Scheme with all issue types.
- Associate your new Issue Type Screen Scheme with your project(s).

Comments (13)
Jan 12, 2006
Robert Castaneda says:
...trying to get this to compile, seems as though targetCF.updateValue now wants......trying to get this to compile, seems as though targetCF.updateValue now wants a "FieldLayoutItem" as a parameter?
Jan 12, 2006
Robert Castaneda says:
..it also wants an IssueChangeHolder! seems like a bit of a major API change hap.....it also wants an IssueChangeHolder! seems like a bit of a major API change happening...
With no fear, I changed the last line to:
targetCF.updateValue( (FieldLayoutItem)transientVars.get("fieldLayoutItem"), issue, new ModifiedValue(issue.getCustomFieldValue(targetCF), value), new DefaultIssueChangeHolder());
The output of my tomcat window says:
"Setting bar to blah"
although bar doesn't seem to get set... any pointers?
Jan 12, 2006
Robert Castaneda says:
doesnt work in 3.4.2 or 3.4.3doesnt work in 3.4.2 or 3.4.3
Jan 13, 2006
Keith says:
Hi Robert, I think it will be necessary to retrieve the field layout item objec...Hi Robert,
I think it will be necessary to retrieve the field layout item object as follows:
FieldLayout fieldLayout = ComponentManager.getInstance().getFieldLayoutManager().getFieldLayout(issue.getGenericValue());
FieldLayoutItem fieldLayoutItem = fieldLayout.getFieldLayoutItem(targetCF.getId());
Let us know how you go with this.
Jan 13, 2006
Robert Castaneda says:
modified working code is: public void execute(Map transientVars, Map args, ...modified working code is:
public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException { Issue issue = (Issue) transientVars.get("issue"); String sourceFieldName = "foo"; String targetFieldName = "bar"; CustomField sourceCF = ManagerFactory.getCustomFieldManager().getCustomFieldObjectByName(sourceFieldName); if (sourceCF == null) throw new WorkflowException("No custom field '" + sourceFieldName + "' found."); CustomField targetCF = ManagerFactory.getCustomFieldManager().getCustomFieldObjectByName(targetFieldName); if (targetCF == null) throw new WorkflowException("No custom field '" + targetFieldName + "' found."); Object value = issue.getCustomFieldValue(sourceCF); System.out.println("Setting "+targetFieldName+" to " + value); System.out.println("oldvalue isssss "+issue.getCustomFieldValue(targetCF)); try{ FieldLayout fieldLayout = ComponentManager.getInstance().getFieldLayoutManager().getFieldLayout(issue.getGenericValue()); FieldLayoutItem fieldLayoutItem = fieldLayout.getFieldLayoutItem(targetCF.getId()); targetCF.updateValue( fieldLayoutItem, issue, new ModifiedValue(issue.getCustomFieldValue(targetCF), value), new DefaultIssueChangeHolder()); } catch (FieldLayoutStorageException e) { throw new WorkflowException(e); } }Jan 13, 2006
Jonathan Nolen says:
Thanks for the fix, Rob. I updated the example.Thanks for the fix, Rob. I updated the example.
Mar 07, 2006
Bettina Zucker says:
Hello Keith, I managed to compile the function after adding following imports: ...Hello Keith,
I managed to compile the function after adding following imports:
and substituting the last line of the original code with your code with some changes:
I still only get a tomcat server message about setting the filed to some (strange) value, but no real field change in Jira. In fact the message was:
Setting MyVersion to [com.atlassian.jira.project.version.VersionImpl@0]
(what I was trying was changing a custom field 'MyVersion' to the value of another custom field 'Integrated Version').
Did anybody manage to get this function working?
Cheers
Bettina Zucker
Mar 08, 2006
Bettina Zucker says:
Sorry, I did not see at first that the thread was longer than 3 comments. Must r...Sorry,
I did not see at first that the thread was longer than 3 comments. Must replace my glasses.
Now I'm using the official corrected function by Robert, but still I see only the output in the tomcat window, but not the field change in Jira. I'v been using different types of custom fields, like single version or text field, but no chance. I even let the function output the custom field id to be sure, and it was correct.
A modified version of the function where I put a non-custom value into the target custom field worked though.
This I really cannot understand, since the function seems to work correctly up to the tomcat window output and the rest of the function works in my modified version where the target is set to the issues assignee. It's crazy.
On which fields types did you test this function?
Cheers
Bettina
Mar 12, 2006
Dylan Etkin says:
Hi Bettina, You must keep in mind that there may be some permission checks arou...Hi Bettina,
You must keep in mind that there may be some permission checks around the update code, so you need to make sure that the issue is being transitioned by someone who can modify the fields values. Could this be causing your issue?
Thanks,
Dylan
May 08, 2006
Bettina Zucker says:
Hello, I finally understood what is missing in this plugin. After updating the c...Hello,
I finally understood what is missing in this plugin.
After updating the custom field you need to update also the issue!
And you need a MutableIssue, not just an Issue.
See http://jira.atlassian.com/browse/JRA-10079, there is some code attached.
Cheers
Bettina
May 18, 2006
Bettina Zucker says:
Hello, this plugin definitely does not work properly from jira version 3.5...Hello,
this plugin definitely does not work properly from jira version 3.5, but there is a correct implementation of this function in the Jira Suite Utilities by Gustavo Martin (a Codegeist contribution). See:
http://confluence.atlassian.com/display/JIRAEXT/JIRA+Suite+Utilities
In the source, look for the file:
CopyValueFromOtherFieldPostFunction.java
The important news is that you have to call the function:
instead of
so you won't need any layout managers and other complicate issues.
Just set the custom field and be happy!
Cheers
Bettina
Nov 07, 2007
Abdelkader Boumediene says:
Hi all, Is there a version for Jira 3.11 Current version is for 3.5 3.9 and does...Hi all,
Is there a version for Jira 3.11
Current version is for 3.5 - 3.9 and does not work with Jira 3.11: fields is not updated with anoter field contents.
Thanks
Feb 13
Erin says:
Echoing the request for an updated version\! I'm using 3.12.1 Enterprise. \\ jav...Echoing the request for an updated version! I'm using 3.12.1 Enterprise.
java:compile:
[echo] Compiling to /home/erin/jira-development-kit-3.12/examples/copyfields-jira-plugin/target/classes
[javac] Compiling 1 source file to /home/erin/jira-development-kit-3.12/examples/copyfields-jira-plugin/target/classes
/home/erin/jira-development-kit-3.12/examples/copyfields-jira-plugin/src/java/com/atlassian/jira/copyfieldsfunction/CopyFieldsFunction.java:31: cannot find symbol
symbol : method updateValue(com.atlassian.jira.issue.Issue,com.atlassian.jira.issue.ModifiedValue)
location: interface com.atlassian.jira.issue.fields.CustomField
targetCF.updateValue(issue, new ModifiedValue(issue.getCustomFieldValue(targetCF), value));
^
1 error
BUILD FAILED
File...... /home/erin/.maven/cache/maven-java-plugin-1.5/plugin.jelly
Element... ant:javac
Line...... 63
Column.... 48
Compile failed; see the compiler error output for details.
Total time: 2 seconds
Finished at: Wed Feb 13 12:07:33 EST 2008
Bettina's proposed fix produced the same error.