Script runner - Fast-track transition based on custom field value

Greetings :

I am currently looking to achieve a situation where workflow auto transition would be done based on triggers based on custom field value. I am using Script Runner add-on which has in-built feature to do so.

I have chosen Generic Event to fire the trigger and condition as cfValues['customfield_12345'] == 'xyz'

provided target field is a free text field. However i am not able to make it working, any idea what am i missing?

 

 

1 answer

1 accepted

0 vote
Mahesh S Community Champion Jun 07, 2016

To be more precise, can you please explain with a complete example?

My understanding is, you need a script to push an Issue from one state to another state when the value of 'customfield_12345' is set to 'xyz'. Am I right?

Yes Mahesh, you got my requirement.

Mahesh S Community Champion Jun 07, 2016

Basically, you need to fire a trigger. But, since it happens from a simple field value edit, you cannot go via a workflow post function. Please question me if this is not clear. smile

It can be solved using a Scripted field.

  • Create a scripted field 'xyz'. Add to field configuration & hide it from the screens.
  • This scripted field should take the 'customfield_12345' value and check whether it is 'xyz' or not.

IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField samp = customFieldManager.getCustomFieldObjectByName("customfield_12345_Name")

String sampVal = (String) issue.getCustomFieldValue(samp)


if(sampVal.equals("xyz")
{
issue.setResolutionId("7")
log.debug ("issue.statusObject.name: " + issue.statusObject.name)
workflowTransitionUtil.setIssue(issue);
workflowTransitionUtil.setUsername(currentUser);
workflowTransitionUtil.setAction(161)
CommentManager commentManager = (CommentManager) ComponentManager.getComponentInstanceOfType(CommentManager.class);
String comment = "Moving to B state since Sample value is xyz";
commentManager.create(issue, currentUser, comment, true);
workflowTransitionUtil.validate();
workflowTransitionUtil.progress();
}

  • Specify the ResolutionId and JIRA status Id in setResolutionId and setAction respectively.

Kindly let me know the results. The above code could be used as a reference. Apologies, I couldn't test and provide the exact code since I'm not in my work station. smile

Hi Mahesh, 

Thanks for your reply, unfortunately we cannot create any new custom fields since this project is already live and we could not make any such changes.

However i found one solution to do this is with Script Runner plugin's listener (Fast-track transition an issue). As of now its not working for me but i am trying it sort it out. Thanks for your help so far.

@Jamie Echlin (Adaptavist)

Hello Jamie,

Can you please help in sorting the issue with Script Runner plugin's listener (Fast-track transition an issue).

 

Mahesh S Community Champion Jun 08, 2016

In the 'Fast-Transition an Issue', we can provide the condition and a transition Id, which moves when it is true. Please provide the below script in the condition.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueImpl
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
  
  boolean status = false
  
	IssueManager issueManager = ComponentAccessor.getIssueManager()
	CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
	CustomField myCustomField = customFieldManager.getCustomFieldObjectByName("custom_field_Name")
	String myCustomFieldVal = (String) issue.getCustomFieldValue(myCustomField)
	if(myCustomFieldVal.equals("xyz"){
	status = true
	}
	
	return status

And then, provide your transition Id in the Action dropdown.

 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

59 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you