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 for Jira cloud: Copy a value from one custom field to another field - check if Null

I have a simple script that copies a value from one custom field to another one.

def issueKey = issue.key

def i;
def issue = get("/rest/api/2/issue/${issueKey}")
.header('Content-Type', 'application/json')
.asObject(Map)
.body

def fields = issue.fields as Map

def SourceList = [
"customfield_10800", // Compass calibration
"customfield_10089", // Compass calibration: X axis interference
"customfield_10090", // Compass calibration: Y axis interference
.

.

.]

def DestinationList = [
"customfield_11030", // Previous Compass calibration
"customfield_11031", // Previous Compass calibration: X axis interference
"customfield_11032", // Previous Compass calibration: Y axis interference
.

.

.]

def SourceSize = SourceList.size()
def DestinationSize = DestinationList.size()

if (SourceSize != DestinationSize){
return "Lists are not of the same size"
}
else {
logger.info ("${SourceSize}")
logger.info ("${DestinationSize}")
}

for (i = 0; i < SourceSize; i++){

// Get the Custom field to get the option value from
def SourceField = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).key == "${SourceList[i]}"
} as Map

// Get the Custom field to set the option value to
def DestinationField = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).key == "${DestinationList[i]}"
} as Map

// Extract and store the option from the custom field
def SourceValue = (fields[SourceField.id] as Map).value

// Write the sourceValue to the DestinationField
def result = Unirest.put("/rest/api/2/issue/${issueKey}")
.queryString("overrideScreenSecurity", true)
.header('Content-Type', 'application/json')
.body([fields:[
(DestinationField.id): SourceValue as String]])
.asString()
}

 

The problem is when one of the SourceFields is empty (Null), I'm getting the following error:

java.lang.NullPointerException: Cannot get property 'value' on null object

 

How can I check if a field is Null?

1 answer

1 accepted

0 votes
Answer accepted
Tanya Gordon Community Leader Oct 07, 2019

Try adding ? before value

def SourceValue = (fields[SourceField.id] as Map)?.value

Unbelievable!

It's working

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase

The benefits of using Jira in different departments

Jira is a great tool to use across different departments. Forget that paperwork – switch to Jira and get that tasks done smoothly. Marketing Jira allows for a complete digital transformation of you...

72 views 0 3
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