getFieldByName() in validators

Maro Hamamjyan November 28, 2019

I want to use the exact same code as in this ticket: https://community.atlassian.com/t5/Answers-Developer-Questions/Check-if-a-date-inserted-in-the-create-screen-is-compliant-to-my/qaq-p/469905    

by

 Kristian Walker -Adaptavist-

 

I want to add it in validators, I tried in both "Custom Script Validator" and "Simple scriped validator", but getFieldByName() in not recognized in the script.

 

How can this be solved? Thansk in advance

1 answer

1 accepted

0 votes
Answer accepted
Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 28, 2019

Hi @Maro Hamamjyan,

I suppose the script you mentioned is for Behaviour and some methods are vary from beviours to post-function/validator/listerner

hopefully below snippet will solve your getting custom field value 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
import java.util.Date.*

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Custom Field Name")
def value = issue.getCustomFieldValue(cf) as Date

 

BR,

Leo

Maro Hamamjyan November 28, 2019

Hi @Leo 

Thank you for the code. I want to check if my custom field named "End date" is less than today+30 days, the reporter should get validation error and should not be able to create the ticket. For that I used this (Attaching the screenshot), I know I must write the error message in the error message box, but how can I connect it with the condition?

Thankserror.JPG

Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 28, 2019

@Maro Hamamjyan,

you are almost there, "setError" method will work in behaviour but not in validator

Just place the message in Error field and "return false" in your script that will throw your error message in the screen

if(validation passed){

    return true // will allow the transition

} else{

    return false //will block the transition

}

hope it gives some idea

 

BR,

Leo 

Maro Hamamjyan November 28, 2019

HI @Leo 

Thank you very much, it worked!

Suggest an answer

Log in or Sign up to answer