Listener script

Anele May 22, 2024

Hi everyone, I wrote a script that changes the follow up date to from what it is to the next follow up date when a comment is added.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.MutableIssue
import java.sql.Timestamp
import java.util.Calendar


IssueEvent event = ComponentAccessor.getIssueEventManager().getEvent(issue.id)
MutableIssue issue = event.issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def dateCf = customFieldManager.getCustomFieldObjectsByName("Follow up date") // Replace with your custom field ID


def calendar = Calendar.getInstance()
calendar.time = new Date()


while (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
    calendar.add(Calendar.DAY_OF_MONTH, 1)
}


issue.setCustomFieldValue(dateCf, new Timestamp(calendar.timeInMillis))


I am getting this error:

Variable "event" masks a binding variable of the same name. Please choose a different name for variable: "event"

2 answers

1 accepted

0 votes
Answer accepted
Tansu Akdeniz
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 22, 2024

Hi @Anele 

Welcome to the community.

Can you try getting issue object in this way? 

Issue issue = event.getIssue()
MutableIssue mIssue = (MutableIssue) issue

Also, check this documentation

Anele May 22, 2024

Hi @Tansu Akdeniz thank you

0 votes
Radek Dostál
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 22, 2024
IssueEvent event = ComponentAccessor.getIssueEventManager().getEvent(issue.id)

Remove this, event is already in the binding, and that is what the static type checking is telling you.

 

def dateCf = customFieldManager.getCustomFieldObjectsByName("Follow up date")

This gives a list, not a single field. You could do .getCustomFieldObjectsByName("Follow up date").first() to get the first field from that list. All in all you'd probably be better off using an ID getter such as .getCustomFieldObject("customfield_11111") which will return just a single field.

 

def calendar = Calendar.getInstance()
calendar.time = new Date()

That's redundant, calendar already comes with current time so you don't need to override the time.

 

I am getting this error:
Variable "event" masks a binding variable of the same name. Please choose a different name for variable: "event"

No, that's not an error. It's static type checking which is more of a warning, but that's already covered in first block.

 

Lastly, you're setting the field value on the issue, but your issue is just in "memory" so to speak, basically you're just editing something in memory but never committing that change back to Jira.

 

After you modify the issue object, you can use IssueManager to make Jira save the changes:

mutableIssue.setCustomFieldValue(CustomField customField, Object value) // your line
issueManager.updateIssue(ApplicationUser updatingUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
issueIndexingService.reIndex(mutableIssue, IssueIndexingParams.INDEX_ISSUE_ONLY) //optional, if you care about index being readily available

 

Which will require for you to have a user that will update the issue (updatingUser). Fortunately you could just reuse the same user that triggered the event (https://docs.atlassian.com/software/jira/docs/api/9.4.19/com/atlassian/jira/event/issue/IssueEvent.html) using event.getUser()

 

Suggest an answer

Log in or Sign up to answer