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,361,231
Community Members
 
Community Events
168
Community Groups

Only set custom field if null [Script Runner]

Hey guys,

I'm still learning how to write custom scripts using Script Runner for Jira server, and I'm having trouble finding documentation to help me with this one: I am trying to only set a custom field to the current date/time (timestamp) if it is currently empty (null). If there is already a value set, no action should occur. I'm not getting any errors, and the post function runs without issue, but it's not posting the timestamp as expected. Any ideas?

 

import com.atlassian.jira.component.ComponentAccessor

import java.sql.Timestamp

def customFieldManager = ComponentAccessor.getCustomFieldManager()


//declare variable
def cfcomplete = customFieldManager.getCustomFieldObjectByName("DevCompleteDtTm") //Set custom text field with current date & time if blank
if (cfcomplete == null) {
issue.setCustomFieldValue(cfcomplete, new Timestamp(new Date().time))
}

2 answers

1 accepted

1 vote
Answer accepted

Hi @Emily B_ 

You are checking if the field exists or not. You need to further check the value in that field.

Try this method.

issue.getCustomFieldValue(cfcomplete)

I hope it helps.

Ravi

Spot on, replacing the variable with this within the If statement worked like a dream. Thank you!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

205 views 1 6
Read article

Atlassian Community Events