Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,296,502
Community Members
 
Community Events
165
Community Groups

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

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

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

320 views 0 10
Join discussion

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