Forums

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

Use ScriptRunner to generate a comment, when a custom field is selected

lukec
Contributor
February 16, 2021 edited

Hi, 

I have a custom field that is single select dropdown. 

When the user selects Yes, I want a pop up to require a comment.

I have basic code here:

import com.atlassian.jira.component.ComponentAccessor

def SecReviewFld = getFieldByName("Security Review")
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObject(SecReviewFld.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def optionToSelect = options.find { it.value == "Yes" }
SecReviewFld.setFormValue(optionToSelect.optionId)

import com.atlassian.jira.issue.comments.CommentManager

def comment = getFieldById("comment")

if (SecReviewFld == "Yes") {
comment.setRequired(true)
comment.setHidden(false)
}
else {
comment.setRequired(false)
}

def defaultValue = """\
**Please leave a comment**

""".stripIndent()

if (!comment.formValue) {
comment.setFormValue(defaultValue)
}

When I click the custom field Security Review, the edit screen pops up, and it flips to Yes automatically, but the comment field is now required. 

Anyone know how to do this? 

 

image.png

1 answer

1 accepted

0 votes
Answer accepted
lukec
Contributor
February 16, 2021

Sort of found an answer: 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager

def SecReviewFld = getFieldByName("Security Review")
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObject(SecReviewFld.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def optionToSelect = options.find { it.value == "Yes" }
SecReviewFld.setFormValue(optionToSelect.optionId)
def comment = getFieldById("comment")

comment.setRequired(true)

 

 

This will make the field change to yes, and require the comment field. 

 

What I can't figure out... How to ONLY pop up the comment field, and not the entire Edit screen - if this is possible...

Helmy Ibrahim _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.
February 16, 2021

Hi lukec,

Behaviours only work on forms. Whenever a field is configured in behaviour, it will force the edit screen to come up instead of just inline editing. This is because behaviour only works there.

To answer your question, no this is not possible.

Like • lukec likes this
lukec
Contributor
February 16, 2021

Thanks.. I'm now making another behaviour, for a different custom field.. and when I run it, it seems to also apply to the initial behaviour. i.e :

I have one behavior to change CF 'Security review' to Yes, and requires a comment. 

I have another behaviour that changes CF '3x' to Yes, and requires CF 'ProdCat' to be filled in.

When I go to change CF 3x, it is also updating Security review. Do you know why?

Suggest an answer

Log in or Sign up to answer