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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,459,882
Community Members
 
Community Events
176
Community Groups

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

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

0 votes
Answer accepted

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 Annadate 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 Annadate likes this

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events