Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

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
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

116 views 1 9
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