Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Change of status in script listner

Rafał Baczyński October 21, 2019

Hi,

I've problem with script below:

import org.apache.log4j.Level
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.workflow.JiraWorkflow
import com.atlassian.jira.workflow.WorkflowManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.security.groups.GroupManager
CustomFieldManager cfm = ComponentAccessor.getCustomFieldManager();
DefaultIssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
CustomField lastClientCommentCf = cfm.getCustomFieldObject(11200L)
def commentManager = ComponentAccessor.getCommentManager()
def issue = event.issue
def commentList = commentManager.getComments(issue).sort{it.getCreated()}
GroupManager gm = ComponentAccessor.getGroupManager()
def comment = commentManager.getLastComment (issue)
// MODYFIKACJA-------------------------------------------------------
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.issueManager
def issue2 = issueManager.getIssueObject(event.issue.key)
IssueService issueService = ComponentAccessor.getIssueService()
def actionId = 281 // change this to the step that you want the issues to be transitioned to
def transitionValidationResult
def transitionResult
def customFieldManager = ComponentAccessor.getCustomFieldManager()

log.debug("The issue type is: " + issue2.getIssueType().name)
log.setLevel(Level.DEBUG)
//---------------------------------------------------------------------
//log.error("Dodanie znacznika do komantarzy")
//log.error((gm.isUserInGroup(comment.getUpdateAuthorApplicationUser(), "jira-servicedesk-users")) || (gm.isUserInGroup(comment.getUpdateAuthorApplicationUser(), "Jsu-partners-team")) || (gm.isUserInGroup(comment.getUpdateAuthorApplicationUser(), "jira-software-users")))
if(comment && ((gm.isUserInGroup(comment.getUpdateAuthorApplicationUser(), "jira-servicedesk-users")) || (gm.isUserInGroup(comment.getUpdateAuthorApplicationUser(), "Jsu-partners-team")) || (gm.isUserInGroup(comment.getUpdateAuthorApplicationUser(), "jira-software-users")))){

lastClientCommentCf.updateValue(null,issue,new ModifiedValue(issue.getCustomFieldValue(lastClientCommentCf),null),changeHolder);
}
else
{
lastClientCommentCf.updateValue(null,issue,new ModifiedValue(issue.getCustomFieldValue(lastClientCommentCf),findCustomFieldOption("11200L","T")),changeHolder);
//---------
log.info("znacznik")
transtionIssue(issue, currentUser, 281)
log.info("znacznik2")
//----------
}

private def findCustomFieldOption(String customFieldId, String optionString) {
OptionsManager optionsManager = ComponentAccessor.getOptionsManager();
List<Option> options = optionsManager.findByOptionValue(optionString);
if (options.iterator().hasNext()) {
Option option = options.iterator().next();
return option
} else
return optionString;
}

void transtionIssue(Issue issue, ApplicationUser user, int transitionId)
{
//def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.issueManager
IssueService issueService = ComponentAccessor.getIssueService()
def actionId = transitionId // change this to the step that you want the issues to be transitioned to
def transitionValidationResult
def transitionResult
def customFieldManager = ComponentAccessor.getCustomFieldManager()
log.debug("Function transtionIssue -> The issue type is: " + issue.getIssueType().name)


transitionValidationResult = issueService.validateTransition(user, issue.id, actionId,new IssueInputParametersImpl())

if (transitionValidationResult.isValid()) {
transitionResult = issueService.transition(user, transitionValidationResult)
}
log.info("znacznik3")

}

At the end I've got function to change status by transition, but it dosen't work and I don't know why :( 

Logs:

Time (on server): Mon Oct 21 2019 19:30:49 GMT+0200 (czas środkowoeuropejski letni)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-10-21 19:30:49,500 DEBUG [runner.AbstractScriptRunner]: The issue type is: Zgłoszenie PLZAM 2019-10-21 19:30:49,507 INFO [runner.AbstractScriptRunner]: znacznik 2019-10-21 19:30:49,507 DEBUG [runner.AbstractScriptRunner]: Function transtionIssue -> The issue type is: Zgłoszenie PLZAM

As we can see "znacznik2" and "znacznik3" did not appear in the logs.

Everything works fine until this point:

lastClientCommentCf.updateValue(null,issue,new ModifiedValue(issue.getCustomFieldValue(lastClientCommentCf),findCustomFieldOption("11200L","T")),changeHolder);

The field value is set here, and then I would like to do transition 281.

1 answer

1 accepted

0 votes
Answer accepted
fjodors
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 23, 2019

Hi

I think you can use WorkflowTransitionUtil in script runner listener if you need to update issue status

Below is code example, try something like this:


import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
...
WorkflowTransitionUtil workflowTransitionUtil = ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class );
...
MutableIssue issueToUpdate = (MutableIssue) event.issue;
ApplicationUser currentUser=event.getUser();
int actionID=999; //your transition ID
...
workflowTransitionUtil.setIssue(issueToUpdate);
workflowTransitionUtil.setUserkey(currentUser.getKey());
workflowTransitionUtil.setAction (actionID) //transition or action ID
// Validate and transition issue
workflowTransitionUtil.validate();
workflowTransitionUtil.progress();
Rafał Baczyński October 23, 2019

Thx for your reply :-) 

My code works correct :) 

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, loom, loom for training, loom for teaching, video training, async learning, online education, screen recording, loom tutorials, loom use cases, atlassian learning, team training tools, instructional video, virtual training tools

🛗 Elevate Your Training and Enablement with Loom

Join us June 26, 11am PT for a webinar with Atlassian Champion Robert Hean & Loom’s Brittany Soinski. Hear tips, stories, and get your burning questions answered. Learn how Loom makes training and enablement easier. Don’t miss it!

Register today
AUG Leaders

Atlassian Community Events