Include a pre-set single select custom field when automating multiple Jira issues using SR jobs

Lara Arthur January 4, 2021

Hi there Atlassian community...

Looking for an answer on two things:

  1. Create multiple jira issues in one custom scheduled job (using ScriptRunner)
  2. Including a custom single select field to be updated on creation

Here is the working script that I'm using currently:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.ModifiedValue
import groovy.transform.Field
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.issue.customfields.manager.OptionsManager

def issueService = ComponentAccessor.issueService
def projectManager = ComponentAccessor.projectManager
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager)
def optionsManager = ComponentAccessor.getOptionsManager()

***How can I include more than one task here in the same job?

def issueInputParameters = issueService.newIssueInputParameters()

issueInputParameters.with {
projectId = projectManager.getProjectObjByKey("KEY").id
summary = "Managers meeting"
issueTypeId = "3"
reporterId = user.name
assigneeId = "user"

//only the second one is being created, I've tried multiple ways to try and get this to spit out two tickets

projectId = projectManager.getProjectObjByKey("IPMO").id
summary = "Deliver Weekly Executive Report"
issueTypeId = "3"
reporterId = user.name
assigneeId = "user"

}
def validationResult = issueService.validateCreate(user, issueInputParameters)
assert !validationResult.errorCollection.hasAnyErrors()

def issueResult = issueService.create(user, validationResult)
log.info "Issue created: ${issueResult.issue}"

What I'd like to do is include adding the following code to it in order to pre-set the single select custom field...but every variation I've tried throws an error. How can I ultimately (or not) pre-set the Custom field?

def fieldConfig = cf.getRelevantConfig(issue)
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Category'}
issue.setCustomFieldValue(cf, "Quality Management")

 

Thank you,

Lara

1 answer

1 accepted

0 votes
Answer accepted
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 4, 2021

Hi @Lara Arthur ,

in general you can do something like this (xxxxx needs to be replaced by your custom field id and yyyyy by your custom field's option id):

issueInputParameters.with {...}
issueInputParameters.addCustomFieldValue("customfield_xxxxx", "yyyyy")
def validationResult = issueService.validateCreate(user, issueInputParameters)
...

Is it important for you to get the ids from the custom field using names (Category, Quality Management) or you can just use directly the ids? It happens to me, that names are changing, so for me is usually safer to use the ids, but it depends on the situation...

Lara Arthur January 7, 2021

@Hana Kučerová Thanks for the suggestion! It does indeed work and allows me to update the custom field.

Any chance you can provide some insight on creating more than one Task in one Job?

For everyone else - I literally added:  issueInputParameters.addCustomFieldValue("customfield_12303", "15410")

above the following: 

def validationResult = issueService.validateCreate(user, issueInputParameters)
assert !validationResult.errorCollection.hasAnyErrors()

def issueResult = issueService.create(user, validationResult)
log.info "Issue created: ${issueResult.issue}"

***Note - to find the custom field ID's, find your custom field > go to the admin cog to the right > hover over Configure and you'll see the ID pop up below left in a URL.

To find the ID of a list selection, click on Configure > Edit Options > navigate to the list selection you need the ID for and hover over Edit...the ID will pop up on the lower left hand corner as a URL.

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 7, 2021

Hi @Lara Arthur ,

your response with all the clarification parts is great!

Sorry, I missed the question about the second issue creation. The simplest method is just create second variable for input parameters (issueInputParameters2) and do the create operation for the second time, something like this:

// create first issue
def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.with {
projectId = projectManager.getProjectObjByKey("KEY").id
summary = "Managers meeting"
issueTypeId = "3"
reporterId = user.name
assigneeId = "user"
}

def validationResult = issueService.validateCreate(user, issueInputParameters)
assert !validationResult.errorCollection.hasAnyErrors()

def issueResult = issueService.create(user, validationResult)
log.info "Issue created: ${issueResult.issue}"

// create second issue
def issueInputParameters2 = issueService.newIssueInputParameters()
issueInputParameters2.with {
projectId = projectManager.getProjectObjByKey("IPMO").id
summary = "Deliver Weekly Executive Report"
issueTypeId = "3"
reporterId = user.name
assigneeId = "user"
}

def validationResult2 = issueService.validateCreate(user, issueInputParameters2)
assert !validationResult2.errorCollection.hasAnyErrors()

def issueResult2 = issueService.create(user, validationResult2)
log.info "Issue created: ${issueResult2.issue}"

If there are more issues, I would recommend to do some method for issue creation, which will get issue parameters (it is not very nice to duplicate the code, but for two issues I can live with it :-)).

Lara Arthur January 14, 2021

@Hana Kučerová Thanks again for your response! I updated per your instructions:

//Create first issue
def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.with {
projectId = projectManager.getProjectObjByKey("KEY").id
summary = "Deliver Monthly Program Review"
issueTypeId = "3"
reporterId = user.name
assigneeId = "NKISHNAN"
}
issueInputParameters.addCustomFieldValue("customfield_12303", "12200")
def validationResult = issueService.validateCreate(user, issueInputParameters)
assert !validationResult.errorCollection.hasAnyErrors()

def issueResult = issueService.create(user, validationResult)
log.info "Issue created: ${issueResult.issue}"

//Create second issue
def issueInputParameters2 = issueService.newIssueInputParameters()
issueInputParameters2.with {
projectId = projectManager.getProjectObjByKey("IPMO").id
summary = "Deliver Weekly Executive Report"
issueTypeId = "3"
reporterId = user.name
assigneeId = "user"
}

issueInputParameters2.addCustomFieldValue("customfield_12303", "12200")
def validationResult2 = issueService.validateCreate(user, issueInputParameters2)
assert !validationResult2.errorCollection.hasAnyErrors()

def issueResult2 = issueService.create(user, validationResult2)
log.info "Issue created: ${issueResult2.issue}"

The script shows as "green" with no script errors, but when I run it, I get the following Error:

java.lang.NullPointerException: Cannot get property 'id' on null object
at Script176$_run_closure1.doCall(Script176.groovy:22)
at Script176.run(Script176.groovy:21)

With the following log info:

2021-01-14 15:45:25,396 ERROR [jobs.AbstractCustomScheduledJob]: *************************************************************************************
2021-01-14 15:45:25,397 ERROR [jobs.AbstractCustomScheduledJob]: Script job: 'PMO Monthly Tasks: Runtime on the 20th of the month' failed
java.lang.NullPointerException: Cannot get property 'id' on null object
at Script176$_run_closure1.doCall(Script176.groovy:22)
at Script176.run(Script176.groovy:21)

 

Any insight on how to adjust for the error? Thank you!--Lara

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 14, 2021

Hi @Lara Arthur ,

it it not clear, to which row of code the error message references, but probably it is this one:

projectId = projectManager.getProjectObjByKey("KEY").id

Is the project key "KEY" correct? Is there project with such a key and are you able to access it?

Thank you for checking.

Lara Arthur January 15, 2021

@Hana Kučerová -omg...I did not even see that since I was so focused on adding the '2's in my second script! Thank you for seeing that! 

Appreciate your help....working like a charm.

Have a wonderful weekend :-) --Lara

Like Hana Kučerová likes this
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 15, 2021

@Lara Arthur I'm happy it worked for you. Thank you, have a wonderful weekend too!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events