How to specify time spent with IssueInputParameters during transition of an issue?

Parashar Joshi
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 15, 2016

Hi,

i have been trying to transition issues with parameters programmatically. I am using IssueInputParameters to update system field values. I have been able to update almost all system fields except for timespent(log effort)

I am unable to set that effort using the setTimeSpent() of IssueInputParameters, however it does not reflect in the Issue and i do not notice any error.

Code Snippet:

IssueInputParameters issueInputParameters = new IssueInputParametersImpl();
//Populate IssueInputParameters with field values
...        
//Validate if the transition is valid for the issue
IssueService.TransitionValidationResult validationResult = issueService.validateTransition(user, issue.getId(), actionId, issueInputParameters);
if (validationResult.isValid()) {
	log.debug("Executing valid transition ");
	transResult = issueService.transition(user, validationResult);
	...
}else {
	//transition failed
}

Any pointers/help in this case is really appreciated.

Thanks in advance.

1 answer

1 accepted

1 vote
Answer accepted
Mahesh S
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 15, 2016

Try the below script. Change the value of time spent and let me know the results.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager
import com.opensymphony.workflow.WorkflowContext
import org.apache.log4j.Category
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.util.JiraUtils;
  
IssueService issueService = ComponentAccessor.getIssueService()
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.setTimeSpent(2L)
workflowTransitionUtil.validate()
workflowTransitionUtil.progress()
Parashar Joshi
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 15, 2016

Hi Mahesh,

Thank you for the suggestion. One quick question.

How is "issueInputParameters" passed as a param or related to "workflowTransitionUtil"?

workflowTransitionUtil as i know requires a mutableIssue object. Please correct me incase.

-Parashar

Mahesh S
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 15, 2016

The method setTimeSpent comes from the interface issueInputParameters. This sets the value for the field. But, workflowTransitionUtil.validate() and workflowTransitionUtil.progress() helps for ErrorCollection while storing the values, since issue.store() is deprecated.

FYI, I have updated this comment.

Parashar Joshi
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 15, 2016

Sure, let me try to set a mutableIssue object to validate with workflowTransitionUtil.validate().

Parashar Joshi
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 15, 2016

Here is the code snippet

WorkflowTransitionUtil workflowTransitionUtil =
              ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class );
            issueInputParameters.setTimeSpent(2L*3600);
            workflowTransitionUtil.setUsername(user.getEmailAddress());
            workflowTransitionUtil.setAction(actionId);
            com.atlassian.jira.issue.MutableIssue opt= ComponentAccessor.getIssueManager().getIssueObject(issueKey);
			opt.setTimeSpent(2L*3600);
            workflowTransitionUtil.setIssue(opt);
            Collection<String> validateErrors = workflowTransitionUtil.validate().getErrorMessages();
            for (String error : validateErrors) {
                System.out.println("ParaTransitionItr - Validate error   "+ error);
            }
            Collection<String> progressErrors =workflowTransitionUtil.progress().getErrorMessages();
            for (String error : progressErrors) {
                System.out.println("ParaTransitionItr - Progress error   "+ error);
            }

 

There are no errors, the transition is successful but time spent is not updated! Same as with IssueinputParameters in transition.

issueService.transition(user, validationResult);
Mahesh S
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 15, 2016

Yes, it sets the value but it is not storing I guess. However workflowTransitionUtil.validate() helps to store the value to db.

Parashar Joshi
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 16, 2016

Correct. But here is where i am stuck since it does not write to database.

Mahesh S
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 16, 2016

Cool. Enjoy now! smile

Parashar Joshi
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 16, 2016

Thank you very much smile 

I was able to set the time. But I have to use time logging API to be able to log time/effort properly.
Updated the code above that worked for me.

Mahesh S
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 16, 2016

Nice! Please share your new code as well. It might help others. smile

Parashar Joshi
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 16, 2016

Updated code that worked with WorkflowTransitionUtil to set the Time spent with MutableIssue object.

WorkflowTransitionUtil workflowTransitionUtil =
              ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class );
            workflowTransitionUtil.setUsername(userName);
            workflowTransitionUtil.setAction(actionId);
            com.atlassian.jira.issue.MutableIssue mutIssueObj= ComponentAccessor.getIssueManager().getIssueObject(issueKey);
			//set time to 2 hours
            mutIssueObj.setTimeSpent(2L*3600);
            workflowTransitionUtil.setIssue(mutIssueObj);
			workflowTransitionUtil.validate();
            workflowTransitionUtil.progress();

Thank you for the pointers smile

-Parashar

stuck June 18, 2018

Hey, the post is 2 years old but you may help me; which jars you used?

Ivan Shkabara December 5, 2019

i get validation errors by the Issue Service while tried to change the status of an issue (transition). This error is "Time spent is required", but setTimeSpent(Long) method of IssueInputParameters does not work...

Ivan Shkabara February 8, 2021

I make the transition without any checks (rights, conditions, validation) and then apply the manager https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/issue/worklog/WorklogManager.html

 

any ideas?

Suggest an answer

Log in or Sign up to answer