Hello,
I am trying to update a Script runner example script for our needs and I am having trouble checking a sub-task field value. Please assist. Thanks.
import com.atlassian.jira.component.ComponentAccessor def issueService = ComponentAccessor.getIssueService() def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() def subTasks = issue.getSubTaskObjects() subTasks.each { if (it.statusObject.name == "Done" && it.cfValues['Deployed in Environment']*.value.contains('Test')) { def issueInputParameters = issueService.newIssueInputParameters() issueInputParameters.with { // setResolutionId("1") // resolution of "Fixed" setComment("Moving subtasks to ToDo from Parent Deployment Request") setSkipScreenCheck(true) } // validate and transition subtask def validationResult = issueService.validateTransition(user, it.id, 111, issueInputParameters) if (validationResult.isValid()) { def issueResult = issueService.transition(user, validationResult) if (! issueResult.isValid()) { log.warn("Failed to transition subtask ${it.key}, errors: ${issueResult.errorCollection}") } } else { log.warn("Could not transition subtask ${it.key}, errors: ${validationResult.errorCollection}") } } }
Code seems to be valid. Try to start with
subTask.getCustomFieldValue(deplInEnv)*.value.contains('Test')
I mean that this method returns Object. It is better to manually cust to respective type. What is type for field
"Deployed in Environment"?
I refactored your code:
import com.atlassian.jira.bc.issue.IssueService import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.IssueInputParameters import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.user.ApplicationUser CustomField deplInEnv = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Deployed in Environment") IssueService issueService = ComponentAccessor.getIssueService() ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getUser() for(Issue subTask:issue.getSubTaskObjects()) { if ( ( subTask.statusObject.getName() == "Done") && subTask.getCustomFieldValue(deplInEnv)*.value.contains('Test')) { IssueInputParameters issueInputParameters = issueService.newIssueInputParameters() issueInputParameters.with { // setResolutionId("1") // resolution of "Fixed" setComment("Moving subtasks to ToDo from Parent Deployment Request") setSkipScreenCheck(true) } // validate and transition subtask def validationResult = issueService.validateTransition(user, subTask.getId(), 111, issueInputParameters) if (validationResult.isValid()) { def issueResult = issueService.transition(user, validationResult) if (! issueResult.isValid()) { log.warn("Failed to transition subtask ${subTask.key}, errors: ${issueResult.errorCollection}") } } else { log.warn("Could not transition subtask ${subTask.key}, errors: ${validationResult.errorCollection}") } } }
Thanks Vasiliy.
"Deployed in Environment" is a Checkbox custom field with multiple options (Dev,Test,Stage,UAT,Prod).
I tried your script and I am still getting "Type Checking" error for that condition.
BTW, this script is used as a Workflow Transition Post-function script. I was trying to use the syntax from Script Runner documentation (https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/validators/simple-scripted-validators.html#_validating_cascading_selects)..
subTasks.each {
if ((it.statusObject.name == "Done") && (it.cfValues['Deployed in Environment']*.value.contains('Test'))) {
...
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
cfValues is only defined for the issue itself. If you're iterating through subtasks, you'll need to use the .getCustomFieldValue method as Vasiliy described.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry, I am kind of new to this. Please bear with me.
I am getting the following error for this line - [Static type checking] - Npo such property: value for class:java.lang.Object
if
(s
ubTask.getCustomFieldValue(deplInEnv)*.value.contains(
'Test'
)) {
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Static Type Checking is helpful but sometimes safe to ignore (as in your case)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Thanos, that was it. The functionality is working, I just had to ignore the error.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am trying to use a similar script for Workflow Transition Condition and having the same difficulty. Still couldnt figure out the reason.
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.fields.CustomField
passesCondition = false
CustomField deplInEnv = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Deployed in Environment")
IssueService issueService = ComponentAccessor.getIssueService()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
for(Issue subTask:issue.getSubTaskObjects()) {
if (subTask.getCustomFieldValue(deplInEnv)*.value.contains('Dev')) {
passesCondition = true
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For the future, it might be good to either update your question or post a new one when you have something like this. Posting a follow-up question as an answer makes for a confusing Q&A thread.
As to your issue, some more detail about the problem would be helpful.
1) Could you log some of the critical variables and pieces in the script, then provide the output from your logs? For example, logging the value of the deplInEnv field
//... def subtaskFieldValue = subTask.getCustomFieldValue(deplInEnv)*.value log.debug( "Subtask $subTask.key has deployment environment value $subtaskFieldValue" ) if (subTaskFieldValue.contains('Dev')) { //...
The following script from the docs can let you crank up your logging levels so that the contents show up in your atlassian-jira.log file (which you can get the contents of using one of the built-in scripts):
import org.apache.log4j.Level import org.apache.log4j.Logger Logger.getLogger("com.onresolve.jira.groovy").setLevel(Level.DEBUG)
2) More detail about the symptom. Is the issue that the condition isn't being applied at all, that it's doing the opposite of what you'd expect, only working sometimes, or what?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Jonny. This is really my second or third post in Atlassian Answers and I am still trying to get a handle on how to use it right.
I appreciate the time and patience you guys show to help folks like us get up to speed with JIRA customization.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.