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

Custom error message in Scriptrunner Behaviours?

Tomáš Vrabec February 10, 2021

Hello guys,

looking for solution to throw custom error message into the portal request screen via behaviours instead of generic one. 

My behaviour is like:

def projectComponentManager = ComponentAccessor.getComponent(ProjectComponentManager)
def projectManager = ComponentAccessor.getComponent(ProjectManager)

def mandatory = false
def component = getFieldById("components").getValue() as List

if (component?.any{it.name == "ABC"}) {mandatory = true}
if (component?.any{it.name == "XYZ"}) {mandatory = true}

if (mandatory == true)
{
getFieldById("attachment").setRequired(true)
getFieldById('customfield_10201').setRequired(true)
}
else
{
getFieldById("attachment").setRequired(false)
getFieldById('customfield_10201').setRequired(false)
}

And yes, I know that attachments dont work in here, but I need to remove the optional word as well and I am checking for attachment in validator.

Need to throw custom message for CF_10201.

Tried .seterror but without success.

Lastest Scriptrunner at JSD 4.13.2

Thanks, Tom

1 answer

1 vote
Amirul _Adaptavist_ February 13, 2021

Hi Tom,

 

I am not sure which ScriptRunner version that you have tested with. I tested below script with Latest ScriptRunner (6.19) and the error appear correctly if I used setError() method.

import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.bc.project.component.ProjectComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.project.component.ProjectComponent


def projectComponentManager = ComponentAccessor.getComponent(ProjectComponentManager)
def projectManager = ComponentAccessor.getComponent(ProjectManager)

def mandatory = false
List <ProjectComponent> component = getFieldById("components").getValue() as List

if (component?.any{it.name == "ABC"}) {mandatory = true}
if (component?.any{it.name == "XYZ"}) {mandatory = true}

if (mandatory == true)
{
getFieldById("attachment").setRequired(true)
getFieldByName("TextField").setRequired(true)
getFieldByName("TextField").setError("TextField is required")
}
else
{
getFieldById("attachment").setRequired(false)
getFieldByName("TextField").setRequired(false)
getFieldByName("TextField").clearError()
}

When ABC Component fill in:

Get IT help - IT Service Management - Service project 2021-02-14 01-48-38.png 

Otherwise:

Get IT help - IT Service Management - Service project 2021-02-14 01-49-39.png

Is this the same requirement that you wanted to achieve?

 

Regards
Amirul

Tomáš Vrabec February 17, 2021

Hi Amirul! You are kind of right, but this will show the error immediately - not after hitting create button. Is there a way how to achieve that?

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
4.13.2
TAGS
AUG Leaders

Atlassian Community Events