script listener update checkbox =yes?

Hi Team,

I am trying set checkbox value =yes, did I am missing anything in the following script. I am trying and doesn't setting as per the script.

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfig

def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Early Entry")//checkBox
def cf2 = customFieldManager.getCustomFieldObjectByName("EA Values")//text field
//check if cf2 not empty,then set Early Entry(checkbox) = Yes
if(cf2!=null){
def config = cf.getRelevantConfig(issue)
def options = ComponentAccessor.getOptionsManager().getOptions(config)
def optionsToSelect = options.findAll { it.value in ["Yes"] }
issue.setCustomFieldValue(cf, optionsToSelect)
}

 

1 answer

0 vote

Hello,

setCustomFieldValue is not enough. You need to use the following method after it

issueManager.updateIssue(userManager.getUser("user"), myIssue, EventDispatchOption.DO_NOT_DISPATCH, false)

You can find an example here

https://community.atlassian.com/t5/Marketplace-Apps-questions/Scriptrunner-Update-summary-field-from-a-custom-listener/qaq-p/461017

HI Alexey,

I have added following still doesnot work, I am missing any thing here.

 

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.event.type.EventTypeManager;

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Early Entry")//checkBox
def cf2 = customFieldManager.getCustomFieldObjectByName("EA Values")//text field
//check if cf2 not empty,then set Early Entry(checkbox) = Yes
if(cf2!=null){
Issue immutableIssue = event.getIssue();
IssueManager issueManager = ComponentAccessor.getIssueManager();
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
MutableIssue issue = issueManager.getIssueObject(immutableIssue.getKey());

def config = cf.getRelevantConfig(issue)
def options = ComponentAccessor.getOptionsManager().getOptions(config)
def optionsToSelect = options.findAll { it.value in ["Yes"] }
issue.setCustomFieldValue(cf, optionsToSelect)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false);

}
Alexey Matveev Community Champion Jan 09, 2018

Are there any errors on the listener? Are you sure that issueManager.updateIssue was executed?

After issueManager.updateIssue try to reindex the issue. The code would be like this

boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
log.warn("Reindex issue ${issue.key} ${issue.id}")
issueIndexingService.reIndex(issueManager.getIssueObject(issue.id));
ImportUtils.setIndexIssues(wasIndexing);

You can read more about reindexing here

https://community.atlassian.com/t5/Answers-Developer-Questions/Issue-reindex-from-groovy-script/qaq-p/509901 

Alexey Matveev Community Champion Jan 09, 2018

By the way try to reindex the project where the issue was updated and have a look if the value appeared.

I have removed if logic for now to set the checkbox value.

throwing null pointer exception at ,Am i passing right argument.

java.lang.NullPointerException: Cannot invoke method getRelevantConfig() on null object


def config = cf.getRelevantConfig(issue)

Issue immutableIssue = event.getIssue();

def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Early Entry")//checkBox
def cf2 = customFieldManager.getCustomFieldObjectByName("EA Values")//text field
//check if cf2 not empty,then set Early Entry(checkbox) = Yes

IssueManager issueManager = ComponentAccessor.getIssueManager();
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
MutableIssue issue = issueManager.getIssueObject(immutableIssue.getKey());

def config = cf.getRelevantConfig(immutableIssue)
def options = ComponentAccessor.getOptionsManager().getOptions(config)
def optionsToSelect = options.findAll { it.value in ["Yes"] }
issue.setCustomFieldValue(cf, optionsToSelect)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false);
boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
log.warn("Reindex issue ${issue.key} ${issue.id}")
issueIndexingService.reIndex(issueManager.getIssueObject(issue.id));
ImportUtils.setIndexIssues(wasIndexing);

 

Alexey Matveev Community Champion Jan 09, 2018

issue variable does not exist in your code. Do not use it in your code. You used it in serveral places. You defined only immutableIssue and you should use it. That is why it should be

def config = cf.getRelevantConfig(immutableIssue)

Fix all your code. 

The error means that you pass null to the getRelevantConfig method.

Sorry , I have coded 
def config = cf.getRelevantConfig(issue) only, but same null point error

Alexey Matveev Community Champion Jan 09, 2018

It means that event.getIssue(); returns null. Could you check it? Is it null? On what event your listener fires?

Thank you I am able to set checkbox =yes,

But need to set based on if condition.

 

//check if 'eaIssueValue ' not empty,then set Early Entry(checkbox) = Yes

MutableIssue immutableIssue = issueManager.getIssueObject(issue.getKey());
def eaIssueValue = immutableIssue.getCustomFieldValue(eaIssues);

def eaIssueValue = issue.getCustomFieldValue(eaIssues);
if(eaIssueValue){

then set Early Entry(checkbox) = Yes

}

the if block is not executing the eaIssues is not empty.I am missing any thing here.

Alexey Matveev Community Champion Jan 09, 2018

I can not see how you set eaIssues. 

def issue = event.issue as Issue

def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def earlyAc = customFieldManager.getCustomFieldObjectByName("Early Entry")//checkBox
def eaIssue = customFieldManager.getCustomFieldObjectByName("EA values")//text field
//check if cf2 not empty,then set Early Entry(checkbox) = Yes
IssueManager issueManager = ComponentAccessor.getIssueManager();
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
MutableIssue immutableIssue = issueManager.getIssueObject(issue.getKey());
def eaIssueValue = immutableIssue.getCustomFieldValue(eaIssue);

if(eaIssueValue){
def config = earlyAc.getRelevantConfig(immutableIssue)
def options = ComponentAccessor.getOptionsManager().getOptions(config)
def optionsToSelect = options.findAll { it.value in ["Yes"] }
immutableIssue.setCustomFieldValue(earlyAc, optionsToSelect)
issueManager.updateIssue(user, immutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false);

boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
log.warn("Reindex issue ${issue.key} ${issue.id}")
issueIndexingService.reIndex(issueManager.getIssueObject(issue.id));
ImportUtils.setIndexIssues(wasIndexing);
}
Alexey Matveev Community Champion Jan 09, 2018

Check the values of eaIssue, eaIssueValue with log.error. Do you have any errors in the log?

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Wednesday in Jira

Join our webinar: How 1B+ feature flag events helped us build the new Jira

Every time you release software, there's a bit of risk – that there's a bug, that something breaks, or that the feature doesn't resonate with customers. Feature flagging helps make high stakes s...

101 views 0 1
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you