Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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...

@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 Jan 07, 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 :-)).

@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 Jan 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.

@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 Jan 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
Community showcase
Published in Marketplace Apps & Integrations

Why you should move agile planning to Lucidspark’s digital whiteboard

During my 17 years as a coach, mentor, and trainer of Agile teams, I’ve participated in hundreds of Agile planning meetings. The end result was a wall of backlog items annotated by an explosion of co...

116 views 0 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you