How to get checkbox value with scriptrunner in post-function?

ANN LEE May 12, 2016

I need to use scriptrunner to create number of sub-task based on checkbox value. According to an example in Atlassian Answers, I copied following code in my script with some of my modification:

============================================================

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption

// get checkboxes fields value

def subComponentFld = getFieldByName("Additional Access")
customField = customFieldManager.getCustomFieldObject(subComponentFld.getFieldId())
config = customField.getRelevantConfig(getIssueContext())
options = optionsManager.getOptions(config)
def optionsToSelect = options.findAll { it.value in ["Email Required", "Network Login Required", "SAP Access Required"] }

if (optionsToSelect ???  ) { }

 

==============================================================

Problem encountered:

1) scriptrunner cannot find matching method for "getFieldByName". Which extra library need to be imported?

2) I guess the value has been read into optionsToSelect , but how to check? 

 

I am very new to JIRA script. Please help!

Thanks lots!!!!!!!!!!!

 

 

 

 

 

3 answers

1 accepted

1 vote
Answer accepted
adammarkham
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.
May 17, 2016

You can get what checkboxes have been selected by using: 

import com.atlassian.jira.component.ComponentAccessor

def issue = ComponentAccessor.getIssueManager().getIssueObject("ISSUE-1")
 
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Additional Access");
def selectedValues = customField.getValue(issue)*.value
 
if ("Email Required" in selectedValues) {
	// Do something
}
...
ANN LEE May 19, 2016

Thanks lot!!!!!!!!!!!

2 votes
Ashraful Hasan [Adaptavist] May 19, 2016

Hi - The following example is a post function script which reads custom field [in this case checkbox] values and create subtasks based on selected values. The script contains important inline comments which explain code lines or functions:

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.project.Project
import groovy.transform.Field
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.user.ApplicationUser

def customFieldManager = ComponentAccessor.getCustomFieldManager()
@Field
SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager()
@Field
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

//name of the custom field
def customFieldName = 'Checkboxes'
//get the custom field object
def customFieldObject = customFieldManager.getCustomFieldObjectByName(customFieldName)

Issue issue = issue
//get all the values of the custom field
def customFieldValues = customFieldObject.getValue(issue)

//loop through the values and create sub tasks
//change the case value as per requirements such as "Email Required" or "SAP Access Required"
customFieldValues.each { LazyLoadedOption it ->
    def optionValue = it.getValue()
    switch (optionValue){
        case "Yes":
            def newIssue = createIssue(issue, optionValue)
            createSubTask(issue, newIssue)
            break
        case "No":
            def newIssue = createIssue(issue, optionValue)
            createSubTask(issue, newIssue)
            break
        case "Maybe":
            def newIssue = createIssue(issue, optionValue)
            createSubTask(issue, newIssue)
            break
    }
}

/**
 * Create a new sub task
 * @param issue
 * @param newIssue
 * @return
 */
private createSubTask(Issue issue, Issue newIssue){
    if(newIssue) {
        subTaskManager.createSubTaskIssueLink(issue, newIssue, user)
    }
}

/**
 * This function creates a new issue. issueInputParameter should be set according to
 * the requirement
 * @param issue
 * @param optionValue
 * @return
 */
private Issue createIssue(Issue issue, String optionValue) {
    Project project = issue.getProjectObject()
    IssueService issueService = ComponentAccessor.issueService
    IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()
    def constantsManager = ComponentAccessor.getConstantsManager()

    //get issue type such as 'Bug'
    def bugIssueType =  constantsManager.getAllIssueTypeObjects().findByName("Bug")

    issueInputParameters.setProjectId(project.id)
        .setIssueTypeId(bugIssueType.id)
        .setReporterId(user.name)
        .setSummary("Test issue _" + optionValue)

    IssueService.CreateValidationResult createValidationResult = issueService.validateCreate(user, issueInputParameters);
    Issue newIssue
    if (createValidationResult.isValid()) {
        IssueService.IssueResult createResult = issueService.create(user, createValidationResult);
        if (createResult.isValid()) {
            newIssue = createResult.issue
        }

    }
    return newIssue
}
ANN LEE May 19, 2016

Thank you so much for your help! It works for me now!

0 votes
ANN LEE May 16, 2016

I need to send the sub-ticket according to 3 check-box value. But I tried many times still get customField null. Please let me know if following code is all right? Is there any easy way to get value from the checkbox?

Thanks ahead!


def fieldid = new String[3]

fieldid[0] = "Email Required"
fieldid[1] = "Network Login Required"
fieldid[2] = "SAP Access Required"


List optionsList = (List)issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Additional Access"));

if(optionsList != null) {
def i
for (i = 0; i <3; i++) {
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(fieldid[i]);
if (customField != null && i == 0) {
assigneeIdList.add("sduffy") 
summariesList.add("Email Required for New Hired")
}
if (customField != null && i == 1) {
assigneeIdList.add("drobinson") 
summariesList.add("Network Login Required for New Hired")
}
if (customField != null && i == 2) {
assigneeIdList.add("coschbach")
summariesList.add("SAP Access Required for New Hired")
}
}
}

Suggest an answer

Log in or Sign up to answer