Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Clear field values depending on an other field value using ScriptRunner Listener

John Diaz March 17, 2022

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. 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Piyush A (STR)
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 17, 2022

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 

John Diaz March 17, 2022

@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.  

Like Piyush A (STR) likes this
TAGS
AUG Leaders

Atlassian Community Events