Listener - Condition for CustomField change AND custom date field in range

Andrew Zimmerman _Appfire_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 15, 2019

Hi there. My organization is using Jira Server v7.12.3 and uses the Scriptrunner add-on. Is there a way to set up a Listener that fires an event when one custom field (number) on an issue changes AND another custom field (date) is in a specific range? I have the first part of the condition set up, but not the second.

Here's what I have so far:

  • I'm using Scriptrunner's "Fires an event when condition is true" Listener
  • Set to run in Project "PROJECT ABC"
  • Event Trigger: "Issue Updated"
  • Condition:
    • def change = changeItems.find {it.field == "Field A"}
      if (change) {
      def oldValue = change.oldstring
      def newValue = change.newstring
      }
  • Event to fire: "Custom Event ABC"

So, everything is working thus far, but I'm looking to add the below to the condition:

"Field B" > -1d AND "Field B" <= 3d

"Field B" is a custom date field in this case. So, I'd like "Custom Event ABC" only to fire if "Field A" changes AND if "Field B" is in the range of time I specify. Anyone know the right way to script for what I'm trying to do.

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Alejandro Suárez García
Atlassian Partner
February 17, 2019

Hi @Andrew Zimmerman _Appfire_ , yo could use something like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.changehistory.ChangeHistoryItem
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.fields.CustomField
import org.joda.time.DateTime
import org.joda.time.Days
import org.ofbiz.core.entity.GenericValue

import java.sql.Timestamp

IssueEvent event = event as IssueEvent
MutableIssue issue = event.getIssue()

ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

GenericValue changeLogs = event.getChangeLog()
ArrayList<ChangeHistoryItem> changedItems = changeHistoryManager.getAllChangeItems(issue).findAll {
it.changeGroupId == changeLogs.id
}
ChangeHistoryItem change = changedItems.find { it.field == "Field A" }

CustomField cfFieldB = customFieldManager.getCustomFieldObject(11111L) // Field B
Timestamp fielB = issue.getCustomFieldValue(cfFieldB) as Timestamp

Date date = new Date()
DateTime now = new DateTime(new Timestamp(date.getTime()))
DateTime fieldBDate = new DateTime(fielB.getTime())
Integer days = Days.daysBetween(now, fieldBDate).getDays()

if (change && days > -1 && days <= 3) {

//do whatever

}
Andrew Zimmerman _Appfire_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 19, 2019

Thank you @Alejandro Suárez García! In the area where you wrote {{11111L}}, is that where I would input the field name of "Custom Field B"?

Alejandro Suárez García
Atlassian Partner
February 19, 2019

Hi @Andrew Zimmerman _Appfire_ , you have to input the ID of the field B

Andrew Zimmerman _Appfire_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 19, 2019

Awesome, thanks for all of your help - @Alejandro Suárez García, one more question for now. Do you know if this will work if field B is a Script Field itself? (It's a scripted date field).

Essentially, it adds "Field A" (a number field) to "Field C" (a date field), applies Field A as "business days" and spits out "Field B" as a new scripted date.

Alejandro Suárez García
Atlassian Partner
February 19, 2019

Yes, if the script field gives you a Date Time, it should work.

TAGS
AUG Leaders

Atlassian Community Events