ScriptRunner Behaviour - Set field to required at certain status

SMAtlassianMber July 10, 2019

I have an existing behaviour that I need to update to include a check for transition to "In Requirements', then set the field to required. Any suggestions would be greatly appreciated. 

import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.workflow.JiraWorkflow
import com.opensymphony.workflow.loader.ActionDescriptor
import com.opensymphony.workflow.loader.StepDescriptor
def log = Logger.getLogger("com.onresolve.jira.groovy")
log.setLevel(Level.DEBUG)

def WorkTypeField = getFieldByName("Work Type")
def projectCat = issueContext.projectObject.projectCategory
def defaultValue = null
log.debug ("project category: " + projectCat)

if (transition to "In Requirements"){
if (projectCat.name.toString() in ["Software Delivery Teams","Business Prioritized"]) {
WorkTypeField.setRequired(true)
WorkTypeField.setHelpText ("Work Type must be filled in once moved to 'In Requirements'")
}
}
else {
WorkTypeField.setRequired(false)
WorkTypeField.setHelpText ("")
WorkTypeField.setFormValue(defaultValue)
}

 

1 answer

0 votes
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 11, 2019

Hi @SMAtlassianMber ,

I would suggest you test against the transition name, and if not enough the current status : 

if (getActionName() == "In Requirements" && underlyingIssue?.getStatus().getName() == "Your status")

 Antoine

SMAtlassianMber July 12, 2019

Hello @Antoine Berry - I tried replacing my note below

if (transition to "In Requirements"){

with your example and the behaviour didn't work. I'm not sure how it will force the user to input the work type at the transition when we have no screen assigned. 

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 12, 2019

Oh, I thought you had a screen assigned.

Then you can simply add a script validator in the transition : 

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

int customFieldId = 13302
def customField = customFieldManager.getCustomFieldObject(customFieldId)
def customFieldValue = issue.getCustomFieldValue(customField)

if (customFieldValue == null || customFieldValue == ""){
throw new InvalidInputException(customFieldId,"This field is mandatory.")
}

(replace the id of the custom field with yours)

Not sure what type your custom field is but this should work with most.

SMAtlassianMber July 12, 2019

@Antoine Berry  - Created a new screen with just work type and added it to the transition. Now if I could just hide the field. This is close, but it always hides the field, so it's not capturing the 1st part of the if statement. 

import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.workflow.JiraWorkflow
import com.opensymphony.workflow.loader.ActionDescriptor
import com.opensymphony.workflow.loader.StepDescriptor
def log = Logger.getLogger("com.onresolve.jira.groovy")
log.setLevel(Level.DEBUG)

def WorkTypeField = getFieldByName("Work Type")
def projectCat = issueContext.projectObject.projectCategory
def defaultValue = null
log.debug ("project category: " + projectCat)

if (projectCat.name.toString() in ["Software Delivery Teams","Business Prioritized"]
&&
(getAction().id == "13600"))
{
WorkTypeField.setRequired(true)
WorkTypeField.setHidden(false)
WorkTypeField.setHelpText ("Work Type must be filled in once moved to 'In Requirements'")
}
else {
WorkTypeField.setRequired(false)
WorkTypeField.setHelpText ("")
WorkTypeField.setHidden(true)
WorkTypeField.setFormValue(defaultValue)
}

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 15, 2019

You should add :

log.error("project category: " + projectCat)
log.error("action : " + getAction().id)

before the if and check the logs when triggering another transition.

Antoine

SMAtlassianMber July 16, 2019

Thank you @Antoine Berry .  I have located the action id and updated the script. Still not working. It still always hides the field. Works fine with just the first part of the if once I add the 2nd condition it stops working. I just noticed it works for the issue types not mapped. 

def WorkTypeField = getFieldByName("Work Type")
def projectCat = issueContext.projectObject.projectCategory
def defaultValue = null
log.error ("project category: " + projectCat)
log.error ("action : " + getAction().id)

if (projectCat.name.toString() in ["Software Delivery Teams","Business Prioritized"]
&& (getAction().id == "291"))
{
WorkTypeField.setRequired(true)
WorkTypeField.setHidden(false)
WorkTypeField.setHelpText ("Work Type must be filled in once moved to 'In Requirements'")
}

else {
WorkTypeField.setRequired(false)
WorkTypeField.setHidden(true)
WorkTypeField.setHelpText ("")
WorkTypeField.setFormValue(defaultValue)
}

SMAtlassianMber July 16, 2019

@Antoine Berry  - Nothing is being logged when I use a different transition, but the log is prior to the if. 

SMAtlassianMber July 16, 2019

Problem solved. I replaced && with || and now it's working. 

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 17, 2019

Good job !

SMAtlassianMber August 12, 2019

@Antoine Berry  - Well it's working, but for some reason it is also making work type required at creation, but not at any other transition. 

SMAtlassianMber August 12, 2019

@Antoine Berry 

Here is my latest update. || is for OR. No longer having problems with create or issue that do not meet the project category, but now it's hiding the work type field when the criteria is meet and the transition is 291. I believe my new problem is related to the transition && (getAction().id == "291")). I think it needs to be more defined to work, but nothing I try is working. 

import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.workflow.JiraWorkflow
import com.opensymphony.workflow.loader.ActionDescriptor
import com.opensymphony.workflow.loader.StepDescriptor
def log = Logger.getLogger("com.onresolve.jira.groovy")
log.setLevel(Level.DEBUG)

def WorkTypeField = getFieldByName("Work Type")
def projectCat = issueContext.projectObject.projectCategory
def defaultValue = null
log.debug ("WorkType Behaviour")
log.debug ("action : " + getAction().id)

if (projectCat.name.toString() in ["Software Delivery Teams","Business Prioritized","IT Prioritized"]
&& (getAction().id == "291"))
{
WorkTypeField.setRequired(true)
WorkTypeField.setHidden(false)
WorkTypeField.setHelpText ("Work Type must be filled in once moved to 'In Requirements'")
}
else {
WorkTypeField.setRequired(false)
WorkTypeField.setHidden(true)
WorkTypeField.setHelpText ("")
WorkTypeField.setFormValue(defaultValue)
}

Like Antoine Berry likes this
SMAtlassianMber August 27, 2019

It was a syntax error - 

Before: if (projectCat.name.toString() in ["Software Delivery Teams","Business Prioritized","IT Prioritized"]
&& (getAction().id == "291"))
{

After: if (getAction().id == 381 && projectCat.name.toString().contains ("Software Delivery Teams","Business Prioritized","IT Prioritized")
{

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 27, 2019

Well

contains

is more permissive than

in

since the latest checks exactly for the string. Both syntaxes should work but will not have the same result.

SMAtlassianMber August 28, 2019

@Antoine Berry  - That is a good point. Thank you for bring that to my attention. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events