Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner for Jira cloud: Copy a value from one custom field to another field - check if Null

Alex Kuterman
Contributor
October 6, 2019

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

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Tanya Gordon
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.
October 7, 2019

Try adding ? before value

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

Alex Kuterman
Contributor
October 7, 2019

Unbelievable!

It's working

TAGS
AUG Leaders

Atlassian Community Events