script listener update checkbox =yes?

Suresh January 8, 2018

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

Suggest an answer

Log in or Sign up to answer
0 votes
Alexey Matveev
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.
January 9, 2018

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

Suresh January 9, 2018

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
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.
January 9, 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
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.
January 9, 2018

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

Suresh January 9, 2018

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
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.
January 9, 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.

Suresh January 9, 2018

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

Alexey Matveev
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.
January 9, 2018

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

Suresh January 9, 2018

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
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.
January 9, 2018

I can not see how you set eaIssues. 

Suresh January 9, 2018
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
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.
January 9, 2018

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

TAGS
AUG Leaders

Atlassian Community Events