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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scriptrunner console get the value of a customfield

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

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

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 !!!

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)
}

 

it works thanks

the error was in the project key value   

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Comparison between Zephyr & Xray

I am sharing this article with both Zephyr and Xray information and everything should be in one place. It will be easy to know when selecting Zephyr or Xray. Zephyr and Xray are both Jira plug-ins u...

180 views 1 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you