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."
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.
currently on server unfortunately. Is there a way to get it for server?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Awesome you are a life saver! It appears to be working!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.