Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Set a Customfield type Checkboxes through scriptrunner

pasquale.scandurra October 27, 2022 edited

Hi folks,

I'm trying to set a checkbox through scriptrunner at creation of a subtask and when the parent is a specific ticlet type.

Here follows what I wrote in the listener. the triggering Event is "issue creation"error scriptrunner2.PNG

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.fields.FieldManager
import com.atlassian.jira.issue.CustomFieldManager;

def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager
def issueManager = ComponentAccessor.issueManager
def currentIssue = event.issue

def customField = customFieldManager.getCustomFieldObjectsByName("Richiesta Formazione")[0]


def fieldConfig = customField.getRelevantConfig(currentIssue)
def options = optionsManager.getOptions(fieldConfig)
def optionsToSet = options.findAll { it.value in ["SI"]}


def parent = event.issue.getParentObject()

// if the ticket is a subtask 

if(currentIssue.issueTypeId.equalsIgnoreCase("10101")){


// il the parent is a type learning

if(parent.issueTypeId.equalsIgnoreCase("10502")){

 

// set checkbox to YES

currentIssue.setCustomFieldValue(customField, "SI")

}

}

the name of the Checkbox filed is "Richiesta formazione" and has one single possible value: "SI"

the error I get is: Cannot find matching method 

 

error scriptrunner.PNG

Could you please help me?

Thank you in advance

/Pas

1 answer

1 vote
Kai Becker
Community Champion
October 29, 2022

Hi Pas,

I don't have an environment to test it right now, but looking at your code, I guess the problem is setting the value. 

First thing you should try: log the option value you have, when searching for "SI"

When this returns a valid value/id, you should pass this id to

currentIssue.setCustomFieldValue(customField, "SI")

 instead of "SI", in your case optionsToSet

That should hopefully do the trick.

pasquale.scandurra November 3, 2022

Hi Kai Sören,

thank yoy for your reply. I'm not sure If got what do you mean by log the option value when searching for "SI".

Sorry but I'm not familiar with scriptrunner.

BR Pas

Kai Becker
Community Champion
November 3, 2022

Hi Pas,

no problem. I updated your code. Line 22 is the log I meant and line 33 shows the update with the option itself, instead of the option value. That should do the trick:

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.fields.FieldManager
import com.atlassian.jira.issue.CustomFieldManager;

def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager
def issueManager = ComponentAccessor.issueManager
def currentIssue = event.issue

def customField = customFieldManager.getCustomFieldObjectsByName("Richiesta Formazione")[0]


def fieldConfig = customField.getRelevantConfig(currentIssue)
def options = optionsManager.getOptions(fieldConfig)
def optionsToSet = options.findAll { it.value in ["SI"]}

//to check if its not empty
log.warn("option: " + optionsToSet.getValue())

def parent = event.issue.getParentObject()

// if the ticket is a subtask
if(currentIssue.issueTypeId.equalsIgnoreCase("10101")){

// il the parent is a type learning
if(parent.issueTypeId.equalsIgnoreCase("10502")){

// set checkbox to YES
currentIssue.setCustomFieldValue(customField, optionsToSet)
}
}
pasquale.scandurra November 4, 2022 edited

Hi and thank you again.

I'm still getting a couple of errors . Not sure it's a library issue or a type issue.

Do you have an idea?

/Paerrors.PNG

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events