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?
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
Great idea! For some reason the vaildator code isnt working now, so i need to have a look at that.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes its a select list (cf[10405])
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try replacing line
def cF = customFieldManager.getCustomFieldObjectByName("District Council")
with this
def cF = customFieldManager.getCustomFieldObjectsByName("District Council").first()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Excellent!! Worked perfect thanks!!....... Do you fancy showing me the IF statement to override while youre there? Haha! Sorry to be cheeky
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Bingo works perfectly! Thanks for all your help mate.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No worries, can you accept one of my answer please?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)}
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.