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,557,621
Community Members
 
Community Events
184
Community Groups

Scriptrunner listener not fulfilling request

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 03, 2023 • edited

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!

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 04, 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