I need someone help me write a script for a listener to clear two custom fields based on selection from another field.
What's currently happening:
I have a single select field called Blocked with options Yes or No. If user selects Yes, then a single select field called Blocked Reason is required and free text field called Additional Text is also required.
When user selects No, the values in Blocked Reason and Additional Text are still there. They'd have to be manually reverted back to the default to remove the irrelevant data entry.
Where I need help with a script:
If user selects No, I want the listener to automatically clear the values for both the Blocked Reason and Additional Text anytime No is selected from the Blocked field.
Hi,
Below might help you to updates another CF select field value. Maybe you can make changes to it.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.Issue
// the name of the custom field (single select list type)
final cf_blocked_name = 'Blocked'
final cf_blockedR_name = 'Blocked Reason'
//copy the same for your additional field
// the value of the new option to set
Issue issue = event.issue
def issueKey = issue.key
def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cf_blocked = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName(cf_blocked_name)
assert cf_blocked: "Could not find custom field with name $cf_blocked"
def cf_blockedR = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName(cf_blockedR_name)
assert cf_blockedR: "Could not find custom field with name $customFieldNameB"
if(issue.getCustomFieldValue(cf_blocked).toString() == 'No' && issue.getCustomFieldValue(cf_blockedR) == null ){
//def userCf = customFieldManager.getCustomFieldObjectByName("user picker")
issue.setCustomFieldValue(cf_blockedR,"")
//cf_blockedR.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf_blockedR), ""), new DefaultIssueChangeHolder())
issueManafer.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
Event: Issue Created and Issue Updated
@Piyush A (STR) I was actually able to find an alternative way to do this and it was an oversight on my end once I figured it out. I'm using the Dynamic Forms add-on which allows me to show fields based on the option selected from another field. What I noticed was when I flip the Blocked Reason back to No, it removes the fields from showing up and actually wipes the data causing a reset in case I need to flip it back to Yes.
However, this could be used for other use cases so I'll save this for now.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.