This page last changed on Mar 01, 2010 by rosie@atlassian.com.
This documentation applies to JIRA version 4.0 and older. If you are using JIRA 4.1, please see Performing Issue Operations.

Retrieving an issue

You can retrieve the issue generic value using the following code:

// By Id
GenericValue issueGV1 = issueManager.getIssue(new Long(10000));
// By Issue Key
GenericValue issueGV2 = issueManager.getIssue("TST-1");

Creating a new Issue

In order to create an issue, one requires a MutableIssue object.  This can be obtained via the IssueFactory class.
MutableIssue issueObject = issueFactory.getIssue();

As mentioned below you can get a handle on an IssueFactory either by Constructor Injection or explicitly via a call like:

ComponentManager.getInstance().getIssueFactory()

Here is sniplet of code that creates a new issue:

MutableIssue issueObject = issueFactory.getIssue();

// Regular Fields
issueObject.setProject(projectManager.getProject(new Long(10000)));
issueObject.setIssueType(constantsManager.getIssueType("1"));
issueObject.setSummary("Test Issue");
issueObject.setReporter(UserUtils.getUser("admin"));
issueObject.setAssignee(UserUtils.getUser("admin"));
issueObject.setPriority(constantsManager.getPriority("1"));
issueObject.setDescription("Test description");
issueObject.setAffectedVersions(EasyList.build(versionManager.getVersion(new Long(10000)), versionManager.getVersion(new Long(10001))));
issueObject.setFixVersions(EasyList.build(versionManager.getVersion(new Long(10002))));
issueObject.setComponents(EasyList.build(projectManager.getComponent(new Long(10000)), projectManager.getComponent(new Long(10001))));

// Custom Fields
CustomField customField = customFieldManager.getCustomFieldObject(new Long(10020));
issueObject.setCustomFieldValue(customField, "Test Value");

Map params = new HashMap();
params.put("issue", issueObject);
GenericValue issue = issueManager.createIssue(authenticationContext.getUser(), params);

This code example uses a lot of Manager objects. You can get a reference to them by declaring a dependency in the constructor of your plugin.

Classes not managed by Picocontainer (eg. workflow conditions / functions, Services and Listeners, or JSP scriptlets) can still get pico-instantiated objects statically using static methods on ComponentManager. For example:

The code above also sets a value for a custom field on the issue. Please note that the value must be an object of the type that the Custom Field expects. As the above code was using a Text custom field, a simple java.lang.String is fine. For more information on working with custom fields please see Working with Custom Fields.

Editing an existing Issue

The code below edits a Due Date field of an issue and sets it to 24 hours from now. A comment that is visible by everyone who has permission to see the issue is also added.

MutableIssue issue = issueFactory.getIssue(issueGV);
issue.setDueDate(new Timestamp(System.currentTimeMillis() + 24*60*60*1000));
Map actionParams = EasyMap.build("issue", issue.getGenericValue(), "issueObject", issue, "remoteUser", authenticationContext.getUser());
actionParams.put("comment", "Test Comment");
actionParams.put("commentLevel", null);
ActionResult aResult = CoreFactory.getActionDispatcher().execute(ActionNames.ISSUE_UPDATE, actionParams);

You can also specify a group name as the commentLevel parameter to restrict the visibility of comments. If you use the above code to update an issue all the relevant change history entries will be made, and an Updated Issue Event generated.

The code above created an issue object from an issue GenericValue.

Unable to render {include} Couldn't find a page to include called: How to retrieve an issue with an ID or an IssueKey

To learn how to update custom fields please see Working with Custom Fields.

Events

It's worth noting that in the examples above, when the issues are created or modified events are fired, and any notifications that are associated with those events will be triggered.

Document generated by Confluence on Mar 27, 2011 18:54