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

Alex_Priyan July 27, 2020

Hi,

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

Suggest an answer

Log in or Sign up to answer
0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 28, 2020

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
TAGS
AUG Leaders

Atlassian Community Events