Forums

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

Scriptrunner listener that updates a date field when a specific custom field is updated

Duane Cronkite
Contributor
July 7, 2023

I am trying to figure out the best way to automatically update a date field when another field is updated.

The scenario:

We have an “approved by” field that is a user picker.

When the user fills out this field I would like a date field called “date approved” update to the date that the field above was updated.

I got this answer from scriptrunner but I don't have much knowledge with writing scripts:

"I believe using Custom Script Listener would be suitable for this case. So essentially, the Listener should be using the Issue Updated event, which will trigger it any time updates are made to the Issue. The triggered script should then have some logic to check if the required field (in this case the “approved by” field) is amongst the fields that were updated in the event. Should it be, then the script will execute some actions. If not, it will exit and not do anything."

1 answer

1 accepted

1 vote
Answer accepted
Evgenii
Community Champion
July 7, 2023

Hi, @Duane Cronkite 

It can be made with scriptrunner, but maybe it's better to make with Jira Automation?

You won't need to write scripts, it can be done much easier.

Duane Cronkite
Contributor
July 7, 2023

currently on server unfortunately. Is there a way to get it for server?

Evgenii
Community Champion
July 7, 2023

Oops, it's bad. No automation for server.

Ok, then let it be script. Will send it later :)

Like Duane Cronkite likes this
Evgenii
Community Champion
July 7, 2023

It will be something like:

/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/

package Examples.Listeners

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager

IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
UserManager userManager = ComponentAccessor.getUserManager()
IssueIndexingService issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)

def changeItem = event.changeLog.getRelated("ChildChangeItem")
MutableIssue issue = issueManager.getIssue(event.getIssue().id) as MutableIssue

// Set field names
String approverFieldName = "Approved By"
String approvedDateFieldName = "Approved Date"

CustomField approvedDateField = customFieldManager.getCustomFieldObject(approvedDateFieldName)
// Set service account, that will make changes in issues
ApplicationUser user = userManager.getUserByName('serviceAccount')

if (changeItem['field'].first() == approverFieldName) {
Date currentDate = new Date()
issue.setCustomFieldValue(approvedDateField, currentDate.toTimestamp())
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
issueIndexingService.reIndex(issue)
}
Duane Cronkite
Contributor
July 7, 2023

thanks for taking the time to help!

 

Received this error log:

2023-07-07 17:52:31,199 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2023-07-07 17:52:31,205 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
groovy.lang.MissingMethodException: No signature of method: Script383$_run_closure1.call() is applicable for argument types: (org.ofbiz.core.entity.GenericValue) values: [[newvalue:duanec, field:Approval, oldstring:null, newstring:Duane Cronkite, ...]]
Possible solutions: any(), any(), doCall(java.util.List), tap(groovy.lang.Closure), any(groovy.lang.Closure), each(groovy.lang.Closure)
at Script383.run(Script383.groovy:33)

Evgenii
Community Champion
July 7, 2023

I updated script, found some problems there. Try it again

Duane Cronkite
Contributor
July 7, 2023

New error logs:

 

2023-07-07 18:06:14,645 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2023-07-07 18:06:14,647 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
java.lang.IllegalArgumentException: [GenericEntity.get] "setCustomFieldValue" is not a field of Issue
at org.ofbiz.core.entity.GenericEntity.get(GenericEntity.java:246)
at com.atlassian.jira.ofbiz.IssueGenericValue.get(IssueGenericValue.java:63)
at org.ofbiz.core.entity.GenericEntity.get(GenericEntity.java:1051)
at com.sun.proxy.$Proxy4716.setCustomFieldValue(Unknown Source)
at com.atlassian.jira.issue.MutableIssue$setCustomFieldValue.call(Unknown Source)
at Examples.Listeners.Script461.run(Script461.groovy:37)

Duane Cronkite
Contributor
July 7, 2023

I have been changing your code to say "Approval" instead of approved by as I found that is the correct title of the field. I was still getting the error but letting you know as an FYI.

 

Approved Date is a Date Picker field

Approval is a user picker field

Evgenii
Community Champion
July 7, 2023

Well, it was stupid error in function to receive custom field object ) Fixed it.

Checked, it's working )

/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/

package Examples.Listeners

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager

IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
UserManager userManager = ComponentAccessor.getUserManager()
IssueIndexingService issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)

def changeItem = event.changeLog.getRelated("ChildChangeItem")
MutableIssue issue = issueManager.getIssueObject(event.getIssue().id) as MutableIssue

// Set field names
String approverFieldName = "Approved By"
String approvedDateFieldName = "Approved Date"

CustomField approvedDateField = customFieldManager.getCustomFieldObjectByName(approvedDateFieldName)
// Set service account, that will make changes in issues
ApplicationUser user = userManager.getUserByName('serviceAccount')

if (changeItem['field'].first() == approverFieldName) {
Date currentDate = new Date()
issue.setCustomFieldValue(approvedDateField, currentDate.toTimestamp())
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
issueIndexingService.reIndex(issue)
}
Like # people like this
Duane Cronkite
Contributor
July 7, 2023

Awesome you are a life saver! It appears to be working!

Like Evgenii likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events