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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across Jira Cloud instance.   📢 Get y...

287 views 12 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you