How to resolve an issue using Java Rest API

Hi,

I am trying to apply a transition flow for an issue. This flow is like:
Open the issue → Start Progress → Add Work Log → Resolve the issue

I successfully completed the first three steps, however, I cannot resolve the issue.

To resolve the issue, “Time Spent” field is mandatory on the GUI screen.

When I don’t send any FieldInput to the TransitionInput, As expected I get an error like that:

RestClientException{statusCode=Optional.of(400), errorCollections=[ErrorCollection{status=400, errors={}, errorMessages=[Time Spent is required.]}]}
       at com.atlassian.jira.rest.client.internal.async.DelegatingPromise.claim(DelegatingPromise.java:47)
       at Main_Clean.main(Main.java:54)

However, when I send a FieldInput (timespent, resolution or any other field) to the TransitionInput, I get an error like:

RestClientException{statusCode=Optional.of(400), errorCollections=[ErrorCollection{status=400, errors={timespent=Field 'timespent' cannot be set. It is not on the appropriate screen, or unknown.}, errorMessages=[]}]}
       at com.atlassian.jira.rest.client.internal.async.DelegatingPromise.claim(DelegatingPromise.java:47)
       at Main_Clean.main(Main.java:54)

The code that I used:

public static void main(String[] args) {

             try {
 
                    URI jiraServerUri = URI.create("http://companyjiratest:8080/");
                    AsynchronousJiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
                    AuthenticationHandler auth = new BasicHttpAuthenticationHandler(username, password);
                    JiraRestClient restClient = factory.create(jiraServerUri, auth);
                    AsynchronousIssueRestClient issueClient = (AsynchronousIssueRestClient) restClient.getIssueClient();
                   
//                  /* Creating issue */
//                  IssueInput issue = createJIRAIssue("ESD", "test1", "Issue from code.","id123", "Department", "");
//                  BasicIssue issueObj = issueClient.createIssue(issue).claim();
 
                    Promise<Issue> prom = issueClient.getIssue("ESD-113");
                    Issue theIssue = prom.claim();

                    Collection<FieldInput> fieldInput = (Collection) Arrays.asList(new FieldInput("timespent", "2h"));
                    Collection<FieldInput> fieldInputs = (Collection) Arrays.asList(new FieldInput("resolution", ComplexIssueInputFieldValue.with("name", "Resolve Issue")));  // Line 54 of Main.java file is that line

//                  /* Adding worklog */
//                  WorklogInputBuilder wiBuilder = new WorklogInputBuilder(theIssue.getWorklogUri());
//                  wiBuilder.setMinutesSpent(20);
//                  wiBuilder.setComment("comment for worklog");
//                  wiBuilder.setAdjustEstimateAuto();
//                  restClient.getIssueClient().addWorklog(theIssue.getWorklogUri(), wiBuilder.build());
               
                    TransitionInput transitionInput = new TransitionInput(21, fieldInput, Comment.valueOf("The comment")); // 21 is the id of resolving issue; got it from .../rest/api/2/issue/ESD-113/transitions

                    restClient.getIssueClient().transition(theIssue, transitionInput).claim();

                    restClient.close();

             } catch (Exception e) {
                    e.printStackTrace();
             }
       }

For “Resolve” an issue, my JIRA server requires a field called “Time Spent”. The screen is same with “Log Work” screen, basically, when you resolve the issue, you should add a final time spent. When do this resolving manually from UI, the value that we entered on “Time Spent” field added as another work log.

So, in “Resolve” screen of UI, if I did not enter any value to “Time Spent” field and push the “Resolve” button, I get error with message “Time Spent is required.” (Exactly same with the message in exception.) as we can see from the image below.

In summary, I need to enter a “Time Spent” value to the required field but I cannot. How can I solve that problem? What am I missing in my code?

Hi @salihtuc,

It’s not possible to update the Time Spent field directly via the API. You should update the remaining estimate field, it should look something like this:

"timetracking": [
          {
            "edit": {
              "originalEstimate": "1w 1d",
              "remainingEstimate": "4d"
            }
          }

Let me know if that works.

Hi, Dave.

First of all, thank you for your reply.

I tried to do that but I still got the same error: “Time Spent is required”. Also, I got the following exception when I use “FieldInput” object in my code:

RestClientException{statusCode=Optional.of(400), errorCollections=[ErrorCollection{status=400, errors={timetracking=Field 'timetracking' cannot be set. It is not on the appropriate screen, or unknown.}, errorMessages=[]}]}
       at com.atlassian.jira.rest.client.internal.async.DelegatingPromise.claim(DelegatingPromise.java:47)
       at Main_API.main(Main.java:102)

How can I update these values as you said? With a POST or PUT request using JSON like you showed (and to where/which URI)? Using FieldInputs as I mentioned in the question? Or are there any other ways?

Also, I want to ask that; are there any guides/tutorials for this kind of operations in baby steps? The documentation of REST API is kind of shallow one.

Sorry for lots of questions; thanks for your patience and interest already. I am waiting for your inspiring answers.

Have a nice day!

Hi @salihtuc,

The example I provided would be part of the fields object in the body of a POST request to the Do Transition endpoint.

Assuming you are an administrator of your Jira instance, you should also verify that the “Time Tracking” field is on the edit screen in your project, that would explain why you are seeing the error from the API.

Unfortunately we don’t have documentation for this specific case, definitely room for improvement here.

Cheers,
Dave

Hi anyone, I’m facing exactly the same issue.
I’m using curl to do a transition to “resolved”

I have tried with the following json file:

 {
 	"transition" : {
 		"id" : "151"
 	},
 	"update" : {
 		"comment" : [
 			{
 			"add": { 
 				"body":"COMMENT"
 			}
 			}
 		],
 "timetracking": [
           {
             "edit": {
               "originalEstimate": "2d",
               "remainingEstimate": "0d"
             }
          }
 ]
 	}
 }

And I get the following message:

 {"errorMessages":[],"errors":{"timetracking":"Field 'timetracking' cannot be set. It is not on the appropriate screen, or unknown."}}

I have then tried just setting the timespent and I get a different error:

{
	"transition" : {
		"id" : "151"
	},
	"update" : {
		"comment" : [
			{
			"add": { 
				"body":"COMMENT"
			}
			}
		],
"timespent": [
          {
            "edit": {
              "value": "2d"
            }
          }
]
	}
}
{"errorMessages":[],"errors":{"timespent":"Field 'timespent' cannot be set. It is not on the appropriate screen, or unknown."}}

If I do not set anything (see following json) I just get the response that there I need to update time spent:

{
	"transition" : {
		"id" : "151"
	},
	"update" : {
		"comment" : [
			{
			"add": { 
				"body":"COMMENT"
			}
			}
		]
	}
}
{"errorMessages":["Time Spent is required."],"errors":{}}

Has there been any update to this issue the last 6 years?

Best Regards,
Gui