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

Scriptrunner console get the value of a customfield

Mahdi Challouf December 12, 2018

I have a custom field of type project picker and i want to get the value selected, here my code 

 

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.project.Project

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11001");// ID of my custom field.

def value = (String)issue.getCustomFieldValue(customField);
log.info("the project selected is"+value)

 

and i get an error : the variable issue is undeclared 

any help !!

 

1 answer

Suggest an answer

Log in or Sign up to answer
3 votes
Joshua Yamdogo @ Adaptavist
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.
December 12, 2018

Hi Madhi,

Yes, the error seems correct. If you are doing this in the script console, you need to get the issue that you want to work with. Something like this:

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.issueManager

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11001")

def issue = issueManager.getIssueByCurrentKey("GIB-1") // your issue key here

def value = (String) issue.getCustomFieldValue(customField)
log.info("the project selected is" + value)

Regards,

Josh

Mahdi Challouf December 13, 2018

Hi Joshua, 

thank you i works fine i can get the value of the project selected in the project picker field it return the key of a project slected in a transition screen.

now I want to clone this issue in the project selected, write this code

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.CustomFieldManager
import org.apache.log4j.Logger
import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue

def issueManager = ComponentAccessor.issueManager

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11001")

def issue = issueManager.getIssueByCurrentKey("NEXT-4") // the issue key here
def value = (String) issue.getCustomFieldValue(customField)
log.warn("the key project selected is" + value)

//clone issue
def originalIssueKey = issueManager.getIssueByCurrentKey("NEXT-4")
def destinationProjectKey = ComponentAccessor.projectManager.getProjectObjByKey("value")
log.warn("the distination is " + destinationProjectKey)

cloneIssue(originalIssueKey, destinationProjectKey)

void cloneIssue (MutableIssue issue, Project project) {
def params = [
issue : issue,
(CloneIssue.FIELD_TARGET_PROJECT) : project.key,
(CloneIssue.FIELD_SELECTED_FIELDS) : null, //clone all the fields
] as Map<String, Object>

new CloneIssue().doScript(params)
}

 

but I get errors, java.lang.NullPointerException: Cannot get property 'key' on null object 

any idea please !!!

Joshua Yamdogo @ Adaptavist
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.
December 13, 2018

Are you sure you have the right project key? For this line, you have the project key as "value"

def destinationProject = ComponentAccessor.projectManager.getProjectObjByKey("value")

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.Project
import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue

def issueManager = ComponentAccessor.issueManager

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11001")

def issue = issueManager.getIssueByCurrentKey("NEXT-4") // the issue key here
def value = (String) issue.getCustomFieldValue(customField)
log.warn("the key project selected is" + value)

//clone issue
def originalIssue = issueManager.getIssueByCurrentKey("NEXT-4")
def destinationProject = ComponentAccessor.projectManager.getProjectObjByKey("value")
log.warn("the distination is " + destinationProject)

cloneIssue(originalIssue, destinationProject)

void cloneIssue(MutableIssue issue, Project project) {
def params = [
issue : issue,
(CloneIssue.FIELD_TARGET_PROJECT) : project.key,
(CloneIssue.FIELD_SELECTED_FIELDS): null, //clone all the fields
] as Map<String, Object>

new CloneIssue().doScript(params)
}
Mahdi Challouf December 13, 2018

 

it works thanks

the error was in the project key value   

TAGS
AUG Leaders

Atlassian Community Events