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?
Try adding ? before value
def SourceValue = (fields[SourceField.id] as Map)?.value
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.