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
Community Members
Community Events
Community Groups

How to update custom field value and select more than one user in custom scheduled jobs


I need to update my custom field value for every 24 hours. So I decided to use scheduled jobs but I couldn't use update function here. It's throwing error whenever I'm trying to update the custom field value from here. Added to this, I can't select more than one user name in scheduled jobs. I expect this program should allow 5 users to run it.

Can anyone please help me out 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import java.sql.Timestamp
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
JiraServiceContext sourceUserServiceCtx = new JiraServiceContextImpl(user)
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def issueManager = ComponentAccessor.getIssueManager()
def issue = event.getIssue() as MutableIssue
def event = event as IssueEvent;
def issuetype = issue.getIssueType()
def changeHolder = new DefaultIssueChangeHolder()

def now = new Timestamp(new Date().getTime())

def field = customFieldManager.getCustomFieldObjectByName("custom field name")
def fieldValue = issue.getCustomFieldValue(field)

field.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field),now),changeHolder)


Thanks in advance

1 answer

0 votes

Your script is throwing errors because you have not told it to do a lot, and you have got invalid calls in it.  I think you have misunderstood the job definition as well.  A job is a block of code that runs regularly in Jira.

So the misunderstandings here seem to be

  • The single user the job is asking you to configure is who the job is going to run as.  Multiple users in there makes no sense - it needs to know who to run as when the schedule comes around.
  • A job runs independently, it's not part of an issue.  Your code is asking an "event" to tell it what issue to run against, but there is no event in a job, it is not triggered by an event, it just runs when scheduled.  (I think you've copied a chunk of a scripted listener from somewhere, which will not work in a job)
  • Your code does not try to make any changes to the issue.  You are rightly trying to work out some data, but you do not actually do anything with it.  (You have some of that in your current script.  It creates a changeholder, and it applies it to the issue, but you are not actually putting anything into the change, so it's not going to do anything)

So, I would take a step back and think through:

  • When the job runs, who should it be run as?
  • When it runs, what issue(s) should it affect?  And hence, how you're going to get them.
  • When it runs against an issue, you want to change a custom field, so you'll need to work out what you want to change it to, and put that into a change

Suggest an answer

Log in or Sign up to answer
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 ...

198 views 1 6
Read article

Atlassian Community Events