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

Updating a a cascading select field via IssueInputParameters

Tom Jackson
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.
April 15, 2014

In Jira 6.1.5, I could use IssueInputParameters to set a cascading select field this way:

public void setCascadingSelect(
			 String[] fieldValue,                        // parent/child field values, with parent in fieldValues[0], child in fieldValue[1]
			 IssueInputParameters issueInputParameters,  // the parameters I’m trying to set a cascading select field in
			 long customFieldId,                         // the id of my cascading select field
			 String projectKey,                          // the key of the project where the select values are configured for use
	         String issueTypeId) {                       // the id of the issue type where the select values are configured for use

	if (fieldValue!=null && fieldValue.length>=1) {
		 
		 // convert the fieldValues into two options ids, one for parent, one for child 
		 Long[] optionIds = getCascadingSelectOptionIds(projectKey, issueTypeId, customFieldId, fieldValue);
		 
		 // add the options to the input params. See explanation here for the :1 craziness
		 // https://developer.atlassian.com/display/JIRADEV/Performing+Issue+Operations
		 issueInputParameters.addCustomFieldValue(customFieldId, optionIds[0].toString());
		 
		 if (optionIds.length==2 && optionIds[1]!=null)
			   issueInputParameters.addCustomFieldValue(customFieldId + ":1", optionIds[1].toString());
		 else
			   issueInputParameters.addCustomFieldValue(customFieldId + ":1", (String) null);
	}             
}

I can’t get it to work in 6.1.7.

Has anyone else run into this??

5 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Bryan Karsh
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.
June 27, 2016

Okay – think I figured it out (JIRA 6.2.5). This works for copying Multilevel Cascading Select field, and creating a new ticket with that value. Assuming it will work for normal cascading select too?

 

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option

def issueManager = ComponentAccessor.getIssueManager()
def projectManager = ComponentAccessor.getProjectManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()


def results = ""
def myIssue = 'FOOBAR-1234'

MutableIssue issue = issueManager.getIssueObject(myIssue)

// .... but first we need to copy existing myCascadingField field values from original ticket

CustomField myCascadingField = customFieldManager.getCustomFieldObjects(issue).find { it.name == "Cascading Field Example" }

def region = issue.getCustomFieldValue(myCascadingField)[0].toString()
def customer = issue.getCustomFieldValue(myCascadingField)[1].toString()
def country = issue.getCustomFieldValue(myCascadingField)[2].toString()

//In order to set the above  field values in the target ticket, I need to get the Option Ids that correspond to them.


Long levelOneId = null
Long levelTwoId = null
Long levelThreeId = null

def cfConfig = myCascadingField.getRelevantConfig(issue)

def cascadingFieldOptions = optionsManager.getOptions(cfConfig)


for (Option regionOption : cascadingFieldOptions) {
    if (regionOption.getValue().equals(region)) {
        levelOneId = regionOption.getOptionId()
        def customers = regionOption.getChildOptions()
        for (Option customerOption : customers) {
            if (customerOption.getValue().equals(customer)) {
                levelTwoId = customerOption.getOptionId()
                def countries = customerOption.getChildOptions()
                for (Option countryOption : countries) {
                    if (countryOption.getValue().equals(country)) {
                        levelThreeId = countryOption.getOptionId()
                    }
                }
            }
        }
    }
}



def projectName = "Target Project" //Target project name

def project = projectManager.getProjectObjByName(projectName)

User currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

def issueService = ComponentAccessor.getIssueService()

def issueInputParameters = issueService.newIssueInputParameters();

// System fields
issueInputParameters.setProjectId(project.id)
issueInputParameters.setIssueTypeId('34')  // 'FOOBAR' issue type in Target Project
issueInputParameters.setSummary(issue.summary)
issueInputParameters.setReporterId(issue.getReporterId())
issueInputParameters.setDescription(issue.description)

// Custom fields
issueInputParameters.addCustomFieldValue(myCascadingField.getId() + ":0", levelOneId.toString())
issueInputParameters.addCustomFieldValue(myCascadingField.getId() + ":1", levelTwoId.toString())
issueInputParameters.addCustomFieldValue(myCascadingField.getId() + ":2", levelThreeId.toString())


try {

    def newIssueResult = issueService.validateCreate(currentUserObj, issueInputParameters);
    if (!newIssueResult.isValid()) {
        results += "could not create target issue: " + newIssueResult.getErrorCollection()
    }

    newIssue = issueService.create(currentUserObj, newIssueResult).getIssue()
    results += "Created issue ${newIssue.key} ."
}

catch (e) {

    results += e.message

}


return results

 

 

 

 

 

Mash Huang August 13, 2019

for 7.3.4, Cascading select field should be:

issueInputParameters
.addCustomFieldValue(myCascadingField.getId(), parentOptionId)
.addCustomFieldValue(myCascadingField.getId() + ":1", childOptionId)

Like # people like this
0 votes
Bryan Karsh
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.
June 24, 2016

Bummer. I may try some lame hack of creating the issue first, then updating the field via a different api. We'll see.

0 votes
Tom Jackson
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.
June 24, 2016

Hey Bryan,

I never did sad

0 votes
Bryan Karsh
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.
June 24, 2016

Did anyone figure this out?

0 votes
Tom Jackson
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.
April 15, 2014

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events