Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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
Asked in Jira Service Management

JSM June ask me anything (AMA)

Hello Community members! We’re wrapping up the end of JSM June with an Ask Me Anything (AMA) with the Jira Service Management product team. This is your chance to ask all your ITSM questions to o...

215 views 11 13
View question

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