Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do you update a Script Field in the workflow post function?

Diana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 27, 2023

In ScriptRunner, I'm using a Script Field using "Date of First Transition" to track the custom field "In Progress Date"  (a Date Time Range picker) when an issue first moved from 'To Do' to 'In Progress'

(our workflow is simple: 
'To Do' >> 'In Progress'
'In Progress' >> 'Review' or 'To Do'
'Review' >> 'Done' or 'In Progress')

Now, the teams need to track anytime the issue moves to 'In Progress'. So not just from ToDo >> In Progress, but also from Review >> In Progress.

I have tried following these examples to use in the workflow post-function to In Progress transition, but none seem to work. I've also set this custom post function script as the first order. 

Here's what I have

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
import com.atlassian.jira.issue.fields.CustomField

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customDateField = customFieldManager.getCustomFieldObject("In Progress Date")
def oldDate = issue.getCustomFieldValue(customDateField)
def newDate = new Date().toTimestamp()

issue.setCustomFieldValue(inProgress, newDate)
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

Could it also be that since it's a Script Field, that I can't overwrite it with a new Date? If so, is there a way we can still keep the custom field rather than making a new one?

1 answer

0 votes
Florian Bonniec
Community Champion
March 27, 2023

Hi @Diana Gorv 

You will have to update the script field code directly to take this new scenario into account. You cannot update ScriptField value as it's calculated base on the script behind the ScriptField.

 

Regards

Diana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 27, 2023

@Florian Bonniec Thank you for the quick response.

If I were to create a new Custom Script Field (let's call it "Moved to In Progress", is there a way to copy all my old custom field value from "In Progress Date" to the new Script Field?

Florian Bonniec
Community Champion
March 27, 2023

You do not have to worries about that, script field do not really "store" data, the value calculated is base on the script you have. So, if you create a new script field, then it should calculate the value for the issue when you display the issue as long as the issue is in the scope of the context applied to the script field.

 

Regards

Diana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 27, 2023

So I created a new custom Script Field, but after I updated the context, it appears under the Field Tab of the issue rather than under the Dates portion in the view screen. Is it because it's a custom Script Field and not using Date of First Transition script field?

Also, in my new custom script field, I followed this example, but it still would not update to the current date when I try testing:

import com.atlassian.jira.component.ComponentAccessor
import static java.time.Instant.ofEpochMilli

final statusName = "In Progress"

final changeHistoryManager = ComponentAccessor.changeHistoryManager
def transitionTime = changeHistoryManager.getChangeItemsForField(issue, "status").find {
it.toString == statusName
}?.created?.time

transitionTime ? Date.from(ofEpochMilli(transitionTime)) : null

How do I set it so that it tracks the date when the issue moved to In Progress? And how can I get the custom script field to be under Dates rather than the Field Tab? 

Diana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 29, 2023

@Florian Bonniec 

also, I tested if I could update the ScriptField via External System Import in the admin settings, but the field did not update. So I'm guessing built-in ScriptFields really cannot be altered unless you make a custom one?

Suggest an answer

Log in or Sign up to answer