Scriptrunner listener not fulfilling request

Ashley May 3, 2023

I've set up a listener in scriptrunner to add 5 days to a date picker field every time the assignee field is changed. It isn't working...and I'm not sure where the failure is. The events it fires on are, Issue Created, Issue Updated, Issue Assigned. It is not failing, but it also isn't doing what I need it to do.

Is a listener a viable way to make this work? I didn't think post functions would, so I was hoping a listener could do the job.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import java.util.Date
import java.text.SimpleDateFormat
import java.util.Calendar

def issue = event.issue as MutableIssue
def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find{ it.field == "assignee"}

// check if the "assignee" field was changed
if (change) {
// get the custom field object by name
def baselineEndField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Baseline End")

// check if the custom field is present on the issue and has a value
if (issue.getCustomFieldValue(baselineEndField) instanceof Date) {
// get the current value of the custom field
Date baselineEnd = issue.getCustomFieldValue(baselineEndField)

// add 5 days to the custom field
Calendar calendar = Calendar.getInstance()
calendar.setTime(baselineEnd)
calendar.add(Calendar.DATE, 5)
Date newBaselineEnd = calendar.getTime()

// set the new value of the custom field
issue.setCustomFieldValue(baselineEndField, newBaselineEnd)
}
}

1 answer

1 vote
Jeroen Poismans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 3, 2023

Hi Ashley!

Seems like you are missing the logic to persist the change to the issue. Your last line is just setting the value on the issue object, you still have eto persist using the issuemanager (which you also need to import and declare):

import com.atlassian.jira.event.type.EventDispatchOption
...
...

def issueManager = ComponentAccessor.getIssueManager()

try {
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
} catch(Exception e) {
log.error("ERROR: Updating $issue.key")
}

Still to retrieve here is the user performing the update.

Hope this helps!

Ashley May 4, 2023

Thank you, I'm still learning how to do all of this - adding what you've put, would I do it like this?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import java.util.Date
import java.text.SimpleDateFormat
import java.util.Calendar
import com.atlassian.jira.event.type.EventDispatchOption

def issue = event.issue as MutableIssue
def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find{ it.field == "assignee"}

// check if the "assignee" field was changed
if (change) {
// get the custom field object by name
def baselineEndField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Baseline End")

// check if the custom field is present on the issue and has a value
if (issue.getCustomFieldValue(baselineEndField) instanceof Date) {
// get the current value of the custom field
Date baselineEnd = issue.getCustomFieldValue(baselineEndField)

// add 5 days to the custom field
Calendar calendar = Calendar.getInstance()
calendar.setTime(baselineEnd)
calendar.add(Calendar.DATE, 5)
Date newBaselineEnd = calendar.getTime()

// set the new value of the custom field
issue.setCustomFieldValue(baselineEndField, newBaselineEnd)

def issueManager = ComponentAccessor.getIssueManager()


try {
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
} catch(Exception e) {
log.error("ERROR: Updating $issue.key")
}
}
}

Jeroen Poismans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 4, 2023

Hi,

As I said, you see that the issueManager.updateIssue takes a user parameter, so you will have to get a user to provide to this method.

To get the current logged in user, triggering the script, you can use this:

 

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

Good luck!

Like Vikrant Yadav likes this

Suggest an answer

Log in or Sign up to answer