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

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
Ravi Sagar Community Leader Aug 24, 2020

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 Confluence Cloud

🎨 Add some visual life to your templates

Hi Atlassian Community, My name is Avni Barman, and I am a Product Manager on the Confluence Cloud team. Based on feedback from you, we are giving admins more power to create templates that a...

181 views 3 8
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