Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner Validator - Validate field from parent

Ricardo_Gomes
Contributor
January 10, 2022

Hi,

I'm trying to add a workflow validator for a sub-task that must look at a field from the parent and only allow the transition if a certain value is selected from a custom single select list field.

In order to accomplish this I was trying to use ScriptRunner's Simple scripted validator by trying to get the value from the sub-task parent (which is a Task issue type) but I can't seem to make it work.

It would be easy for example, to know the status of the parent ticket by doing this:
issue.parentObject.statusObject.name == "Open"

I couldn't, however, find find a way to get the value of a custom field from the parent in order to validate that.

Please help.

Thank you!

 

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Kmmaughs
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
March 30, 2020

It looks like the final answer was this format:

log.warn("Listener is fired");

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue

def issue = event.issue as MutableIssue

//grabing some methods
WatcherManager watcherManager = ComponentAccessor.getWatcherManager()
UserManager userManager = ComponentAccessor.getUserManager()
CustomFieldManager customField = ComponentAccessor.getCustomFieldManager()

//Get the custom field value. .toString() is a must for the commentManager.create function, othervise it is throwing a validation error.
def cField1 = customField.getCustomFieldObjectByName("Customer")
def cFieldValue1 = issue.getCustomFieldValue(cField1).toString()

log.warn("Show the custom field value:" + cFieldValue1)

//Adding a watcher when this condition is met (NOTE: I used contains since my custom field was a multi-select)
if (cFieldValue1.contains("Something")) {

//Alternatively, you can use what was listed above: if (cFieldValue1 == "Something") {


def users = ["username"]
def watchUsers = {usernames ->;
usernames.each {
watcherManager.startWatching(userManager.getUserByName((String)it), issue)
}
}
watchUsers(users)

log.warn("true")
} else {
log.warn("false")
}
0 votes
Daniel Yelamos [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
October 12, 2018

Hi Lubomir.

I think your postfunction is firing for sure. That is a static compilation error and I'm sure it's not what's making that postfunction fail. Could you add a 

log.debug "I'm in the postfunction" 

That way you can check the logs to make sure that it's actually getting executed.

Cheers!

DY

Lubomir Styskala
Contributor
October 15, 2018

Hi Daniel,

it is working now. I will put the solution here later today. Thank you for your effort so far.

cheers,

Lubomir Styskala
Contributor
October 15, 2018

Regarding the solution, I noticed that this definition was also marked by static type checking

def cFieldValue1 = issue.getCustomFieldValue(cField1).toString()

So a added a few lines:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue

and

def issue = event.issue as MutableIssue

and the code started to work as expected. So it seems that the issue declaration is not needed in post-function but in Scripted Listener is needed. I don't know why, but it did the job and after that the code was clear.

TAGS
AUG Leaders

Atlassian Community Events