Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Status is not updating after the transition.

Manish Kumar October 25, 2018

we are using script runner heavily in our organization and recently while working on one of the requirement, we encountered an issue where we have to transition a workflow using one groovy script which is triggered from one transition as post function and perform multiple transitions using issueService.transition() method. The script ran fine but it was not updating the status, However, everything looks good in the change history. To fix this I used   issue.setStatsObject(status[1]) method and everything looks good now. My question is though the status should have been mapped automatically, is it ok to use setStatusObject() method, using this method will not cause any impact on the JIRA database or overall to JIRA?  I have added the snippet of my code below.

if (validationResult.isValid()) { issueService.transition(user, validationResult) issue.setStatusObject(status[1]) issueIndexMgr.reIndex(validationResult.getIssue()) }

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Aidan Derossett [Adaptavist]
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.
October 26, 2018

Hiya Manish!

Having done some transitions using the IssueService myself, I don't believe you should need to manually set the status with another function call. As long as the data is all in line, I don't think it would have much impact on your instance but it shouldn't be necessary. To me, it sounds like some sort of reindexing problem. Have you always had the line:

issueIndexMgr.reIndex(validationResult.getIssue())

In your code, or did you just recently add it along with the setStatusObject call? 

If it would be alright, I'd like to take a look at the rest of your code as well for testing. :D

Best,
Aidan

Manish Kumar October 26, 2018

Thanks, Aidan for responding. It's not well-written code. I still need to make it more readable. However, I am still attaching it over here

Manish Kumar October 26, 2018
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.crowd.embedded.api.CrowdService;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.TransitionValidationResult
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.workflow.WorkflowTransitionUtil
import com.atlassian.jira.issue.IssueConstant
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.config.ConstantsManager
import com.atlassian.jira.config.ResolutionManager
import org.apache.log4j.Logger
import org.apache.log4j.Level






ApplicationUser validuser
def jiraflag = true
def confluenceflag = true
def resultTransition
def componentCount = 0
def jiraIndex = 0
def confluenceIndex = 0
def startProgress = 341
def allAccountsAction = 431
def jiraAccountAction = 401
def confluenceAccountAction = 411
def addConfluenceGroup = 421
def analystReviewingAction = 451
def closeAction = 2



/**Getting the ulti user account**/
CrowdService crowdService = ComponentAccessor.getCrowdService();
User user = crowdService.getUser("ulti")
def userManager = ComponentAccessor.getUserManager();
ApplicationUser appUser = userManager.getUserByKey("ulti");
CommentManager commentMgr = ComponentAccessor.getCommentManager()

IssueManager issueManager = ComponentAccessor.getComponentOfType(IssueManager);
def customFieldManager = ComponentAccessor.getCustomFieldManager()
MutableIssue issue = issueManager.getIssueByCurrentKey("BIZ-17166");


def type = customFieldManager.getCustomFieldObject("customfield_11290")
typeValue = issue.getCustomFieldValue(type)

def userId = customFieldManager.getCustomFieldObject("customfield_14621")
userNameValue = issue.getCustomFieldValue(userId).toLowerCase()
userName = userNameValue.split(" ")

def groupsField = customFieldManager.getCustomFieldObject("customfield_10521")
groupsFieldValues = issue.getCustomFieldValue(groupsField)

def contactEmail = customFieldManager.getCustomFieldObject("customfield_10610")
contactEmailFieldValues = issue.getCustomFieldValue(contactEmail)


/**Reading the Component size and checking if JIRA and COnfluence is added as Component on the rquest **/
componentSize = issue.getComponents().name.size
for( int j=0; j<=componentSize; j++){
if(issue.getComponents().name[j] == "JIRA")
{
componentCount = componentCount + 1;
jiraIndex = j /**Getting the JIRA index on the Component list **/
}
if(issue.getComponents().name[j] == "Confluence")
{
componentCount = componentCount + 1;
confluenceIndex = j /**Getting the COnfluence index on the Component list**/
}
}

/**checking for existing user
validuser = userManager.getUserByKey(userNameValue);**/

/**Executing the All Account Transition if component size is more then 1**/
if((typeValue.toString() == "New Hire" || typeValue.toString() == "Account Request") && componentCount >=1){
def actionID = startProgress /**Iniating Start Progress Transition**/
issueService = ComponentAccessor.getIssueService()
issueIndexMgr = ComponentAccessor.getIssueIndexManager()
issueInputParameters = issueService.newIssueInputParameters()
TransitionValidationResult validationResult = issueService.validateTransition(user, issue.id, actionID, issueInputParameters)
workflow = ComponentAccessor.getWorkflowManager()
jworkflow = workflow.getWorkflow(issue)
status = jworkflow.getLinkedStatusObjects()
if (validationResult.isValid()) {


issueService.transition(user, validationResult)
issue.setStatusObject(status[1])/**setting the issue stauts to In Progress**/
issueIndexMgr.reIndex(validationResult.getIssue())
}




/**Execute All Account Transition if it is requested for both JIRA and Confluence**/
if(componentCount == 2 && userName.size() == 1){
issueInputParametersValue = issueInputParameters.addCustomFieldValue("customfield_18820","confluence-users")
validUpdateResult = issueService.validateUpdate(user,issue.id, issueInputParametersValue)
if(validUpdateResult.isValid()){
issueService.update( user, validUpdateResult)

}
actionID = allAccountsAction/**Iniating All Account Transition**/
validationResult = issueService.validateTransition(user, issue.id, actionID, issueInputParameters)

if (validationResult.isValid()) {


resultTransition = issueService.transition(user, validationResult).getErrorCollection().getAt("errorMessages")[0]
issueIndexMgr.reIndex(validationResult.getIssue())
}
if(resultTransition == null){/**Checking if All account Tranistion is succesfully executed, adding groups to Confluence account**/


/**Adding Multiple groups for Confluence**/
if(groupsFieldValues.size()>0){
size = groupsFieldValues.size()
for(int i = 0; i<size; i++){
issueInputParametersValue = issueInputParameters.addCustomFieldValue("customfield_18820",groupsFieldValues.name[i])
validUpdateResult = issueService.validateUpdate(user,issue.id, issueInputParametersValue)
if(validUpdateResult.isValid()){
issueService.update( user, validUpdateResult)
}
actionID = addConfluenceGroup/**Iniating Add Confluence Group transition**/
issueInputParameters = issueService.newIssueInputParameters()
validationResult = issueService.validateTransition(user, issue.id, actionID, issueInputParameters)
def confluenceTransition
if (validationResult.isValid()) {
confluenceTransition = issueService.transition(user, validationResult).getErrorCollection().getAt("errorMessages")[0]
issueIndexMgr.reIndex(validationResult.getIssue())
}

}
}

}

if( resultTransition == "User Profile already exists in Confluence!!"){

confluenceflag = false
def body = userNameValue+ " already exist in Confluence"
commentMgr.create(issue, appUser, body, true)

actionID = jiraAccountAction/**Iniating the JIRA transition**/
validationResult = issueService.validateTransition(user, issue.id, actionID, issueInputParameters)


if (validationResult.isValid()) {

jiraTransition = issueService.transition(user, validationResult).getErrorCollection().getAt("errorMessages")[0]
issueIndexMgr.reIndex(validationResult.getIssue())
}


}
if(resultTransition == "User Profile already exist in JIRA!!"){

def body = userNameValue+ " already exist in JIRA"
commentMgr.create(issue, appUser, body, true)
jiraflag = false



actionID = confluenceAccountAction/**Iniating the confluence transition**/
validationResult = issueService.validateTransition(user, issue.id, actionID, issueInputParameters)
def confluenceTransition
if (validationResult.isValid()) {
confluenceTransition = issueService.transition(user, validationResult).getErrorCollection().getAt("errorMessages")[0]
issueIndexMgr.reIndex(validationResult.getIssue())

}
if( confluenceTransition == "User Profile already exists in Confluence!!"){
confluenceflag = false
body = userNameValue+ " already exist in Confluence"
commentMgr.create(issue, appUser, body, true)
}


if (confluenceTransition == null){
/**Adding Multiple groups for Confluence**/

/**Making Comment if Confluence Account is created**/


if(groupsFieldValues.size()>0){
size = groupsFieldValues.size()
for(int i = 0; i<size; i++){
issueInputParametersValue = issueInputParameters.addCustomFieldValue("customfield_18820",groupsFieldValues.name[i])
validUpdateResult = issueService.validateUpdate(user,issue.id, issueInputParametersValue)
if(validUpdateResult.isValid()){
issueService.update( user, validUpdateResult)
}
actionID = addConfluenceGroup/**Iniating Add Confluence Group transition**/
issueInputParameters = issueService.newIssueInputParameters()
validationResult = issueService.validateTransition(user, issue.id, actionID, issueInputParameters)

if (validationResult.isValid()) {
confluenceTransition = issueService.transition(user, validationResult).getErrorCollection().getAt("errorMessages")[0]
issueIndexMgr.reIndex(validationResult.getIssue())
}

}
}
}
}
}





/**Exececute if component value is either JIRA or Confluence**/
if(componentCount==1 && userName.size() == 1){
/**Iniating the Jira Account transition**/
if(issue.getComponents().name[jiraIndex] == "JIRA"){
actionID = jiraAccountAction
validationResult = issueService.validateTransition(user, issue.id, actionID, issueInputParameters)
def resultTransitionJIRA
if (validationResult.isValid()) {


resultTransitionJIRA = issueService.transition(user, validationResult).getErrorCollection().getAt("errorMessages")[0]
issueIndexMgr.reIndex(validationResult.getIssue())
}


if(resultTransitionJIRA == "User Profile already exist in JIRA!!"){
def body = userNameValue+ " already exist in JIRA"
commentMgr.create(issue, appUser, body, true)
jiraflag = false
}

}
/**Iniating the confluence transition**/
if(issue.getComponents().name[confluenceIndex] == "Confluence"){

issueInputParametersValue = issueInputParameters.addCustomFieldValue("customfield_18820","confluence-users")
validUpdateResult = issueService.validateUpdate(user,issue.id, issueInputParametersValue)
if(validUpdateResult.isValid()){
issueService.update( user, validUpdateResult)

}

actionID = confluenceAccountAction
validationResult = issueService.validateTransition(user, issue.id, actionID, issueInputParameters)
def resultTransitionConfluence
if (validationResult.isValid()) {
resultTransitionConfluence = issueService.transition(user, validationResult).getErrorCollection().getAt("errorMessages")[0]
issueIndexMgr.reIndex(validationResult.getIssue())
}

if( resultTransitionConfluence == "User Profile already exists in Confluence!!")
{
confluenceflag = false
def body = userNameValue+ " already exist in Confluence"
commentMgr.create(issue, appUser, body, true)

}

if( resultTransitionConfluence == null) {
/**Adding Multiple groups for Confluence**/


if(groupsFieldValues.size()>0){
size = groupsFieldValues.size()
for(int i = 0; i<size; i++)
{
issueInputParametersValue = issueInputParameters.addCustomFieldValue("customfield_18820",groupsFieldValues.name[i])
validUpdateResult = issueService.validateUpdate(user,issue.id, issueInputParametersValue)
if(validUpdateResult.isValid()){
issueService.update( user, validUpdateResult)

}
actionID = addConfluenceGroup/**Iniating Add Confluence Group transition**/
issueInputParameters = issueService.newIssueInputParameters()
validationResult = issueService.validateTransition(user, issue.id, actionID, issueInputParameters)
def confluenceTransition
if (validationResult.isValid()) {

confluenceTransition = issueService.transition(user, validationResult).getErrorCollection().getAt("errorMessages")[0]
issueIndexMgr.reIndex(validationResult.getIssue())
}

}

}

}
}
}



/**Checking Confluence and JIRA Flag**/
if(confluenceflag == true && jiraflag == true && userName.size() == 1){



actionID = closeAction /**If both the flags is set to true then close the Issue**/
validationResult = issueService.validateTransition(user, issue.id, actionID, issueInputParameters)

if (validationResult.isValid()) {


issueService.transition(user, validationResult)
issue.setStatusObject(status[3])
issueIndexMgr.reIndex(validationResult.getIssue())
}



}
else{
actionID = analystReviewingAction/**If either of the flag is set to False then trnaistion to Analyst Reviewing Status**/
validationResult = issueService.validateTransition(user, issue.id, actionID, issueInputParameters)

if (validationResult.isValid()) {


issueService.transition(user, validationResult)
issue.setStatusObject(status[6])
issueIndexMgr.reIndex(validationResult.getIssue())

}

}

if(userName.size() > 1){


def body ="There is a space in the UserName"
commentMgr.create(issue, appUser, body, true)
}

}

 

Aidan Derossett [Adaptavist]
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.
October 30, 2018

No worries, I'm not one for scrutinizing others' code. That would be incredibly hypocritical considering my own coding atrocities haha.  ;D

That's a big'ol script, so it's going to take me some time to work through it and test around, but I'll let you know what I find. :)

Best,
Aidan

Aidan Derossett [Adaptavist]
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.
November 2, 2018

Hey again Manish! :D

I've been playing around with some similar transition code and haven't run into any problems:

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def doTransition(actionId) {
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
MutableIssue issue = issue
IssueService issueService = ComponentAccessor.getIssueService()
def transitionValidationResult
def transitionResult

transitionValidationResult = issueService.validateTransition(currentUser, issue.id, actionId, issueService.newIssueInputParameters())

if (transitionValidationResult.isValid()) {
transitionResult = issueService.transition(currentUser, transitionValidationResult)
if (transitionResult.isValid()) {
log.debug("Transitioned issue $issue through action $actionId")
} else {
log.debug("Transition result is not valid")
}
} else {
log.debug("The transitionValidation is not valid")
}
}

doTransition(61)
doTransition(41)

However, I have noticed something that may be of note in your case. When the post-function is placed anywhere after the "Set issue status to the linked status of the destination workflow step" in the execution order, it breaks and doesn't set the status of the issue correctly.

Where is your post-function located in the execution order? Would you mind sending over a screenshot of your post-function execution list?

Best,
Aidan

0 votes
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
October 26, 2018

To solve this I usually use the built-in script that allow you to do the transition and aditionally, you can do whatever you want to do next in "aditional issue actions"

TAGS
AUG Leaders

Atlassian Community Events