Forums

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

Override needed for current Script Runner vaildator

Rob B
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 Leaders.
August 7, 2018

I have a script runner validator that stops users creating issue if there is an open issue with a custom field value selected......

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

//get user
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//get the issues in project SEW of type Notification that have a value in the District council field
def JQLQuery = jqlQueryParser.parseQuery("project = SEW AND issuetype = \"Notification\" AND \"cf[10405]\" is not EMPTY")
def results = JQLQuery ? searchProvider.search(JQLQuery, user, PagerFilter.getUnlimitedFilter()) : null
def issues = results? results.issues : null
// get all the values of the District council field from the issues
def cF = customFieldManager.getCustomFieldObjectByName("District council")
def values = []
issues.each {
values.add(it.getCustomFieldValue(cF))
}
//get the issue that is trying to be created, find the value of District council they have entered, check it isnt in the values list
// if it is then throw the error
Issue issue = issue
def cFValue = issue.getCustomFieldValue(cF)
if(cFValue in values) throw new InvalidInputException("CANNOT LOG ON - SEWER ISSUE ONGOING")

I now need to know if there a way the user can override this after seeing the prompt? 

 

2 answers

1 accepted

1 vote
Answer accepted
JohnsonHoward
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 Leaders.
August 7, 2018

Hi Robert,

 

You could add a custom field called Override with the options yes or no, maybe select list or radio button. Then in the validator code you can add an if statement to check if the value of that custom field is yes, if it is then don't continue with the validation.

 

Would that work?

 

Thanks

Johnson Howard

Rob B
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 Leaders.
August 7, 2018

Great idea! For some reason the vaildator code isnt working now, so i need to have a look at that.

Rob B
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 Leaders.
August 8, 2018

Any idea what this failure error means?...

 

Time (on server): Tue Aug 07 2018 14:26:24 GMT+0100 (British Summer Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2018-08-07 14:26:24,757 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-08-07 14:26:24,759 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.NullPointerException
 at com.atlassian.jira.issue.DocumentIssueImpl.getCustomFieldValue(DocumentIssueImpl.java:326)
 at com.atlassian.jira.issue.Issue$getCustomFieldValue$5.call(Unknown Source)
 at Script40$_run_closure1.doCall(Script40.groovy:24)
 at Script40.run(Script40.groovy:23)
JohnsonHoward
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 Leaders.
August 8, 2018

It looks like you are trying to pass the getCustomFieldValue the wrong object, it needs a custom field object but you have given a DocumentIssueImpl.

Show me the code that is throwing this error and I will check it out.

Rob B
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 Leaders.
August 8, 2018
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

//get user
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//get the issues in project SEW of type Notification that have a value in the District council field
//def JQLQuery = jqlQueryParser.parseQuery("project = SEW AND issuetype = \"Notification\" AND status =Open AND \"cf[10405]\" is not EMPTY")
def JQLQuery = jqlQueryParser.parseQuery("project = SEW and issuetype = \"Notification\" AND status = \"Open\" and \"cf[10405]\" is not EMPTY")
def results = JQLQuery ? searchProvider.search(JQLQuery, user, PagerFilter.getUnlimitedFilter()) : null
def issues = results? results.issues : null
// get all the values of the District council field from the issues
def cF = customFieldManager.getCustomFieldObjectByName("District Council")
def values = []
issues.each {
values.add(it.getCustomFieldValue(cF))
}
//get the issue that is trying to be created, find the value of District council they have entered, check it isnt in the values list
// if it is then throw the error
Issue issue = issue
def cFValue = issue.getCustomFieldValue(cF)
if(cFValue in values) throw new InvalidInputException("CANNOT LOG ON - SEWER ISSUE ONGOING")
JohnsonHoward
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 Leaders.
August 8, 2018

Where are you trying to run this? It looks the the issue variable might be null. Have you changed anything since the error. 

Also does the query return any issues?

Rob B
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 Leaders.
August 8, 2018

The JQL returns one notification issue that is open (removed the '\').

I am running this on the create transition, custom script validator second row down.

JohnsonHoward
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 Leaders.
August 8, 2018

Is there custom field District Council in the system? I only get the same error as you if I change the name of the custom field to something that doesn't exist. 

Rob B
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 Leaders.
August 8, 2018

Yes its a select list (cf[10405])

districtcouncil.jpg

JohnsonHoward
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 Leaders.
August 8, 2018

Try replacing line 

def cF = customFieldManager.getCustomFieldObjectByName("District Council")

with this

def cF = customFieldManager.getCustomFieldObjectsByName("District Council").first()
Rob B
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 Leaders.
August 8, 2018

Excellent!! Worked perfect thanks!!....... Do you fancy showing me the IF statement to override while youre there? Haha! Sorry to be cheeky

JohnsonHoward
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 Leaders.
August 8, 2018

No problem at all.

You would do something like this:

Issue issue = issue
def cFOverride = customFieldManager.getCustomFieldObjectsByName("Override").first()
def overrideValue = issue.getCustomFieldValue(cFOverride)
if(overrideValue.toString() == "Yes"){
return
}else{
//dp validation
}
Rob B
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 Leaders.
August 8, 2018

Bingo works perfectly! Thanks for all your help mate.

JohnsonHoward
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 Leaders.
August 8, 2018

No worries, can you accept one of my answer please?

1 vote
Mark Markov
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 Leaders.
August 7, 2018

Hello @Rob B

May be I am not understand question correctly.

But when user changes field value that matches condition (in your case custom field value that did not match already existing issues), user can be able to create issue.

Rob B
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 Leaders.
August 7, 2018

Yes but i would like there to be an override with the field still poplated, rather than creating the case with the field not populated and then having to go back in and populate it. If there is no option then i guess thats it ha

JohnsonHoward
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 Leaders.
August 7, 2018

I

PS July 8, 2020

@Rob B 

I would like to populate the text fields based on SelectList Field Value.

But the script is not working as expected it is working only for BQ Datasets.

Please correct the script if needed

def textField1 = getFieldById("customfield_15115")
def textField2 = getFieldById("customfield_15121")
def textField3 = getFieldById("customfield_15117")
def textField4 = getFieldById("customfield_15118")
def textField5 = getFieldById("customfield_15119")
def textField6 = getFieldById("customfield_15120")
def selectList = getFieldByName("Sub-Issue Type")

def selectListValue1 = selectList.getValue()

if (selectListValue1 == ("Service Accounts"))
textField1.setHidden(false)
textField2.setHidden(false)
textField3.setHidden(true)
textField4.setHidden(true)
textField5.setHidden(true)
textField6.setHidden(true)

if (selectListValue1 == ("GCS Buckets"))
textField1.setHidden(true)
textField2.setHidden(true)
textField3.setHidden(false)
textField4.setHidden(false)
textField5.setHidden(false)
textField6.setHidden(false)

if (selectListValue1 == ("BQ Datasets")) {
textField1.setHidden(true)
textField2.setHidden(true)
textField3.setHidden(true)
textField4.setHidden(false)
textField5.setHidden(false)
textField6.setHidden(false)}
else
{ textField1.setHidden(true)
textField2.setHidden(true)
textField3.setHidden(true)
textField4.setHidden(true)
textField5.setHidden(true)
textField6.setHidden(true)}

JohnsonHoward
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 Leaders.
July 9, 2020

Hi Sreekala,

I am not sure if this is the exact issue you are facing but you are unlikely to get the outcome you desire unless you put brackets around each if statement, not just the if else statements. The set hidden methods before the BQ Database if statement will all be executed regardless of the proceeding statement unless you add the brackets.

def textField1 = getFieldById("customfield_15115")
def textField2 = getFieldById("customfield_15121")
def textField3 = getFieldById("customfield_15117")
def textField4 = getFieldById("customfield_15118")
def textField5 = getFieldById("customfield_15119")
def textField6 = getFieldById("customfield_15120")
def selectList = getFieldByName("Sub-Issue Type")

def selectListValue1 = selectList.getValue()

if (selectListValue1 == ("Service Accounts")){
textField1.setHidden(false)
textField2.setHidden(false)
textField3.setHidden(true)
textField4.setHidden(true)
textField5.setHidden(true)
textField6.setHidden(true)}

if (selectListValue1 == ("GCS Buckets")){
textField1.setHidden(true)
textField2.setHidden(true)
textField3.setHidden(false)
textField4.setHidden(false)
textField5.setHidden(false)
textField6.setHidden(false)}

if (selectListValue1 == ("BQ Datasets")) {
textField1.setHidden(true)
textField2.setHidden(true)
textField3.setHidden(true)
textField4.setHidden(false)
textField5.setHidden(false)
textField6.setHidden(false)}
else
{ textField1.setHidden(true)
textField2.setHidden(true)
textField3.setHidden(true)
textField4.setHidden(true)
textField5.setHidden(true)
textField6.setHidden(true)}

Suggest an answer

Log in or Sign up to answer