Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,460,008
Community Members
 
Community Events
176
Community Groups

Populating Date picker field using SR listener

Hello,

We are on Jira Data Center 8.20.1 and ScriptRunner 6.45.0. We recently upgraded SR after more than 6/8 months.

We have several date picker custom fields that are auto-populated using SR listener. The listener script was using "now" function to populate current date/time on these date fields. When are users use Jira JQL to do filters on these date fields, it worked just fine. Recently, the JQL doesnt seem to find all candidates when filtered based on these date fields.

 

Here is an example of a JQL that does not pull all relevant Issues -

type = DBCC AND "Deployed in Environment" in (UAT) AND "Actual UAT Deployment Date" >= 2022-03-07 AND status not in (Rejected) ORDER BY "Actual UAT Deployment Date" DESC, key ASC

 

When I go into these Issues that are missed, to edit the date field and re-pick the same date, now these Issues start showing up in the JQL result.

 

Here is the SR listener snippet that populates these date fields -

if (("UAT" in selectedValues) && (!(change.getString("oldstring")) || !(change.getString("oldstring").contains("UAT")))) {
def cf1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Actual UAT Deployment Date")
def changeHolder1 = new DefaultIssueChangeHolder()
cf1.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf1), now),changeHolder1)

def cf2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("MT Tag in UAT")
def cf3 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("MT Deployment Tag")
def changeHolder2 = new DefaultIssueChangeHolder()
cf2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf2), issue.getCustomFieldValue(cf3)),changeHolder2)

def cf4 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Deployed in UAT By")
def changeHolder4 = new DefaultIssueChangeHolder()
cf4.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf4), currentUser),changeHolder4)
}

 

My Questions -

1) Did anything change recently in newer versions of SR that changed how the "now" function works with Date fields?

2) I need assistance with modifying the script so that going forward these date fields are going to be populated with the correct date/time format?

3) Is there a way to change the above JQL date check ("Actual UAT Deployment Date" >= 2022-03-07) to pull Issues that are currently missed? This will avoid me having to fix active tickets with incorrect date format.

4) If I have to go back and update the date fields manually on all active tickets of type = DBCC, what is the easy way to do this? In Script console, can I do a filter to pull all active DBCCs and then read current date value and replace with the same date/time value? If yes, I would appreciate if I can get help with such a script also.

 

Thanks much!

Venkat

 

 

1 answer

1 accepted

1 vote
Answer accepted

I'm not aware of a change in SR or Jira that would account for what you are describing.

I don't think that "now" has ever been a valid keyword in groovy. Unless some older version of scriptrunner included this as a built-in variable. But I'm not sure why it would have, this is hardly a shortcut for the normal "new Date()" way to get the current date/time.

Also, unless you have some critical code in your listener that you are not sharing I'm surprised this would have ever worked.

The "cusstomField.updateValue()" is the lowest level api for changing an issue's custom field value and has never included a refresh of the Lucene index that JQL uses to find issues.

So what happens is that your issue is updated, but the index still contains the old value. The next time you edit the issue or apply a workflow transition, the index probably gets refreshed then the JQL should work.

You have 2 approaches to solve this issue.

1) Add to your listener script a manual call to the indexing service to refresh the index for the issue

import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
ComponentAccessor.getComponent(IssueIndexingService.class).reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)

2) Change your listener script to use a higher level api such as IssueService.update() that will take care of the indexing.

def issueService = ComponentAccessor.issueService
if (("UAT" in selectedValues) && (!(change.getString("oldstring")) || !(change.getString("oldstring").contains("UAT")))) {
def iip = issueService.newIssueInputParameters()
def cf1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Actual UAT Deployment Date")[0]
def cf2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("MT Tag in UAT")[0]
def cf3 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("MT Deployment Tag")[0]
def cf4 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Deployed in UAT By")[0]
def dateTimeFormat = 'dd/MMM/yyyy hh:mm:ss'
iip.addCustomFieldValue(cf1.idAsLong, new Date().format(dateTimeFormat))
iip.addCustomFieldValue(cf2.idAsLong, issue.getCustomFieldValue(cf3))
iip.addCustomFieldValue(cf4.idAsLong, currentUser.key)

def validationResult = issueService.validateUpdate(currentUser, issue.id, iip)
assert validationResult.isValid()
issueService.update(currentUser, validationResult)
}

Hi Peter-Dave,

Thanks for your response.

My apologies, I should have sent the complete script first up. "now" was defined variable.

I went with your second solution. However, I see the following errors in the log,

2022-03-13 23:51:11,551-0400 http-nio-48080-exec-102 ERROR krishnamoorthv@nih.gov 1431x90935x1 d3k90u 10.246.166.241,128.231.7.102 /secure/QuickEditIssue.jspa [c.o.scriptrunner.runner.AbstractScriptListener] *************************************************************************************
2022-03-13 23:51:11,552-0400 http-nio-48080-exec-102 ERROR krishnamoorthv@nih.gov 1431x90935x1 d3k90u 10.246.166.241,128.231.7.102 /secure/QuickEditIssue.jspa [c.o.scriptrunner.runner.AbstractScriptListener] Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
Assertion failed:

assert validationResult.isValid()
| |
| false
com.atlassian.jira.bc.issue.IssueService$UpdateValidationResult@3ef2b836
at org.codehaus.groovy.runtime.InvokerHelper.assertFailed(InvokerHelper.java:415)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.assertFailed(ScriptBytecodeAdapter.java:670)
at Script252.run(Script252.groovy:79)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155)
at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233)
at javax.script.ScriptEngine$eval.call(Unknown Source)

 

Here is the current listener content -

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

//import com.atlassian.jira.issue.ModifiedValue
//import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
def issueService = ComponentAccessor.issueService

//import java.sql.Timestamp
Issue issue = event.issue

if (! issue.isSubTask())
return

if ((issue.getIssueTypeId() != "11601") && (issue.getIssueTypeId() != "11602") && (issue.getIssueTypeId() != "11603"))
return

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Deployed in Environment")[0];
def selectedValues = customField.getValue(issue)*.value


def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field=="Deployed in Environment"}
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
//def now = new Timestamp(new Date().getTime())
def dateTimeFormat = 'dd/MMM/yyyy hh:mm:ss'

if (change) {
if (("UAT" in selectedValues) && (!(change.getString("oldstring")) || !(change.getString("oldstring").contains("UAT")))) {
def iip = issueService.newIssueInputParameters()
def cf1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Actual UAT Deployment Date")[0]
def cf2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("MT Tag in UAT")[0]
def cf3 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("MT Deployment Tag")[0]
def cf4 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Deployed in UAT By")[0]

iip.addCustomFieldValue(cf1.idAsLong, new Date().format(dateTimeFormat))
iip.addCustomFieldValue(cf2.idAsLong, issue.getCustomFieldValue(cf3).toString())
iip.addCustomFieldValue(cf4.idAsLong, currentUser.key)

def validationResult = issueService.validateUpdate(currentUser, issue.id, iip)
assert validationResult.isValid()
issueService.update(currentUser, validationResult)
}
}

 

Thanks

Venkat

Tried different format changes but no luck yet. Please assist.

Try

assert validationResult.isValid() : validationResult.errorCollection

This should give you some details about why the validation was not valid.

What type of field are each of your custom fields?

Here is what I see in the logs after adding your suggestion -

SR listener errors.jpg

Here are field types for the custom fields -

Actual UAT Deployment Date - Date Picker field
MT Tag in Test - Text Field (Single line)
Deployed in Test By - User Picker field (Single user)


MT Deployment Tag - Text Field (Single line)

 

Thanks!

I had assumed that your date field was a date/time picker.

So change

def dateTimeFormat = 'dd/MMM/yyyy hh:mm:ss'

to

def dateTimeFormat = 'dd/MMM/yyyy'

And I always make the mistake between username and userkey, I never know which API works with which.
Change

 iip.addCustomFieldValue(cf4.idAsLong, currentUser.key)

To

 iip.addCustomFieldValue(cf4.idAsLong, currentUser.name)

Works now! Thank you very much.

@Peter-Dave Sheehan Hi Peter, I'm trying to do something I think similar, in that I want to find out when a user has updated their 'End date' field. Then run a query to to report on changes. Could you please show me the modifications I'd need to do to the above code, or am I asking for something completely different? Thanks.

I'm not sure I fully understand your use case. Maybe you can write up a separate post and give me more context on what you're trying to achieve.

Feel free to @ me in your post.

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events