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,359,862
Community Members
 
Community Events
168
Community Groups

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

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

Hi @Andrew Zimmerman , 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

}

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

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

Awesome, thanks for all of your help - @Alejandro Suárez [Tecnofor], 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.

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

172 views 1 6
Read article

Atlassian Community Events