Update Date & Time field based on Checkbox

Drishti Maharaj August 15, 2022

Hi,

I am looking to update a custom Date & Time Field based on if a user clicks the "Yes" option in a Checkbox field.

I tried Project Automation but we have the free version and not all features are available.

I thought the alternative may be a scripted custom Date & Time Field that updates when the choice is made?

I have used this code previously when an issue is created but unsure how to modify the code for a checkbox field?

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
 
def issue = event.issue
 
// the name of the custom field to update
final customFieldName = 'Created Date & Time'
 
// the new value of the field
final newValue = issue.created
 
def customFieldManager = ComponentAccessor.customFieldManager
 
def customField = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)
assert customField : "Could not find custom field with name $customFieldName"
 
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), newValue), new DefaultIssueChangeHolder())
Any help is appreciated
Thanks.

2 answers

1 accepted

0 votes
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 15, 2022

This script shows you how to work with all sorts of custom fields.  Note that check-boxes work the same way as multi-select lists!

I think you're probably right about using a scripted field - this would stop the users entering the wrong data into it.  With a scripted field, the two tricks you need to know when coding it are 1) Remember to set the output template to the appropriate type - you're calculating a date, so I'd use a date/time template, 2) the value of the scripted field is what the script "returns", you do not have to set a value in the field, just calculate and return it. 

0 votes
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 15, 2022

Hi @Drishti Maharaj  May i know for which event you are trying to update Date Time custom field

If you are looking to set field value for Issue Created event then i suggest you use custom Date Time field instead of scripted field and apply scripted post function, it will set value if checkbox is having value "Yes" . 

If you are looking to update Date Time field for Issue Updated event then try to apply Scripted Listener, which will set value when user select "Yes" value in  checkbox field.

 

Thanks

V Y

Suggest an answer

Log in or Sign up to answer