Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
  • Community
  • Q&A
  • Jira
  • Questions
  • Using ScriptRunner, set component default value dynamically based on changes to the issue type

Using ScriptRunner, set component default value dynamically based on changes to the issue type

Chaya November 22, 2022
I want to change the default value of component field based on issue type

import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE
import static com.atlassian.jira.issue.IssueFieldConstants.COMPONENTS
 


import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE
import static com.atlassian.jira.issue.IssueFieldConstants.COMPONENTS
 
if (underlyingIssue) {
    return // the issue already exists, therefore this is not the initial action, so don't set 
}

def issue_type = getFieldById(ISSUE_TYPE).value


if(issue_type == "Bug" || issue_type == "Enhancement"){
    getFieldById(COMPONENTS).setFormValue(["Test"])
} else{
    getFieldById(COMPONENTS).setFormValue([])
}
Above code is not working
How do I update default value based on selected issue type?

1 answer

1 accepted

0 votes
Answer accepted
Florian Bonniec
Community Champion
November 22, 2022

Hi @Chaya 

 

Not sure if it's the only issue but you do a comparaison between an issue type object and a string. You should use 

getFieldById(ISSUE_TYPE).value?.name

 

It may display an error because you did not declare it as IssueType but it should work.

 

Regards

Chaya November 22, 2022

@Florian Bonniec I tried with 

getFieldById(ISSUE_TYPE).value.name but it is giving error
Florian Bonniec
Community Champion
November 22, 2022

Your code seems to be valid except that getFieldById(ISSUE_TYPE).value return the issue type id not the issuetype name

 

so def issue_type = getFieldById(ISSUE_TYPE).value.toString()

Then

if(issue_type == "XXXX" || issue_type == "YYYY"){

 

WHere XXXX and YYYY are issue type ids, not name

Florian Bonniec
Community Champion
November 22, 2022

You can try ussing issueContext instead "getIssueContext().issueType.name"

 

import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE
import static com.atlassian.jira.issue.IssueFieldConstants.COMPONENTS
 
import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE
import static com.atlassian.jira.issue.IssueFieldConstants.COMPONENTS
if (underlyingIssue) {
    return // the issue already exists, therefore this is not the initial action, so don't set
}
def issue_type = getIssueContext().issueType.name
if(issue_type == "Bug" || issue_type == "Enhancement"){
    getFieldById(COMPONENTS).setFormValue(["Test"])
} else{
    getFieldById(COMPONENTS).setFormValue([])
}

Suggest an answer

Log in or Sign up to answer