Hi
I have a Yes/No select box that on Yes I want to show 3 fields and on No I want to hide the fields.
scriptrunner: 4.1.3.29
Jira server: 6.4.10
With the script below when I display my ticket the helper text is displayed as expected (the current value in the field is Yes). However when I change the value to No the helper text is removed but not displayed the fields don't change.
Any help would be appreciated.
Thanks,
Neil
=== script ===
log.warn("setting conditionField");
def EARequiredfield = getFieldById("customfield_14819");
def EARequirements = getFieldById("customfield_14821");
def EAStatus = getFieldById("customfield_14822");
def EAComments = getFieldById("customfield_14823");
def EARequired = EARequiredfield.getValue();
if (EARequired == "No"){
EARequiredfield.setHelpText("test: " + EARequired );
}
else {
EARequiredfield.setHelpText("test 1: " + EARequired );
}
EARequired.each { selectVal ->
if (selectVal == "No"){
EARequirements.setHidden(false);
EAStatus.setHidden(false);
EAComments.setHidden(false);
log.warn("source value: " + EARequirements.isHidden());
}
else {
EARequirements.setHidden(true);
EAStatus.setHidden(true);
EAComments.setHidden(true);
}
}
Hi @Kevin Bouman ,
the problem is in the mergeQuery method - it expects com.atlassian.query.Query to be returned, but you variable returnArray has some list type.
I believe instead of modify mergeQuery method you should create getValues method and put your code there (the part with searching issues and filtering based on the attachment categories) - please look at these two examples, which are I believe more suitable to your situation:
Thanks for your direction. You are correct that I needed to create a new method. I also changed the query type to the same as the "Project Versions" query in the ScriptRunner documentation. Here is the working code.
/*********************
Description: This script is a custom JQL query that returns issues that have at least 1
attachment in the associated SmartAttachments category. This query requires the SmartAttachments plugin.
JQL structure: key in attachmentsInCategory(arg1, arg2)
- arg1: This needs to be a subquery to limit the result set. An example is, "project = ABC"
- arg2: This needs to be the name of the Smart Attachments category that we are looking for. An Example is "Source"
Author: Kevin Bouman
Created: 3/4/2020
Updated:
**********************/
package com.onresolve.jira.groovy.jql
import com.atlassian.jira.JiraDataType
import com.atlassian.jira.JiraDataTypes
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.issue.search.SearchQuery
import com.atlassian.jira.jql.operand.QueryLiteral
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.jql.query.IssueIdCollector
import com.atlassian.jira.jql.query.QueryCreationContext
import com.atlassian.jira.jql.validator.NumberOfArgumentsValidator
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.util.MessageSet
import com.atlassian.jira.util.MessageSetImpl
import com.atlassian.query.clause.TerminalClause
import com.atlassian.query.operand.FunctionOperand
import groovy.util.logging.Log4j
import org.apache.log4j.Category
//Smart Attachments specific imports
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.stiltsoft.jira.attachcategory.facade.SmartAttachmentsFacade
import com.stiltsoft.jira.attachcategory.facade.entity.issue.AttachmentCategories
@Log4j
class attachmentsInCategory extends AbstractScriptedJqlFunction implements JqlFunction {
@Override
public MessageSet validate(ApplicationUser searcher, FunctionOperand operand, TerminalClause terminalClause) {
def i18n = ComponentAccessor.getI18nHelperFactory().getInstance(searcher)
def numberValidMessage = new NumberOfArgumentsValidator(2, i18n).validate(operand);
if (numberValidMessage.hasAnyErrors()) {
return numberValidMessage
}
def messageSet = new MessageSetImpl();
JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
try {
def query = jqlQueryParser.parseQuery(operand.getArgs().get(0))
} catch (any) {
messageSet.addErrorMessage("not valid jql:${operand.getArgs().get(0)}");
messageSet.addErrorMessage("${any}");
}
return messageSet
}
@Override
List<QueryLiteral> getValues(QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause) {
final List<QueryLiteral> literals = new LinkedList<>();
//Define the JQL to get the issues you want to affect. This is the first argument from the JQL
String jql = operand.getArgs().get(0);
getIssuesByJQL(jql, operand, queryCreationContext.getApplicationUser()).each { issue ->
literals << new QueryLiteral(operand, issue.key)
issue.getSubTaskObjects().each { subTask ->
literals << new QueryLiteral(operand, subTask.key)
}
}
return literals
}
private Collection<MutableIssue> getIssuesByJQL(String jql, FunctionOperand operand, ApplicationUser user) {
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def query = jqlQueryParser.parseQuery(jql)
SearchQuery searchQuery = SearchQuery.create(query, user)
IssueIdCollector collector = new IssueIdCollector()
searchProvider.search(searchQuery, collector)
// Initializing the SmartAttachments app components
@WithPlugin("com.stiltsoft.jira.smart-attachments")
SmartAttachmentsFacade facade = ScriptRunnerImpl.getPluginComponent(SmartAttachmentsFacade)
IssueManager issueManager = ComponentAccessor.getIssueManager()
//Second Argument from the JQL. Needs to be the name of a SmartAttachments Category
def myCategory = operand.getArgs().get(1)
def returnMe = []
//Loop through the issues and only return the ones that have at least 1 attachment in the provided category
for (def i in collector.getIssueIds()){
def iss = ComponentAccessor.issueManager.getIssueObject(i as Long) //getIssue(i as Long)
MutableIssue myIssue = issueManager.getIssueObject(iss.key);
//Get the categories for the current issue
AttachmentCategories attachmentCategories = facade.getAttachmentCategories(myIssue)
def selectedCategory = attachmentCategories.categories.find { category -> category.name.equalsIgnoreCase(myCategory) }
def attachments = selectedCategory.getAttachments(true)
//Check if there are any attachments in the category
if(attachments.size() > 0){
//Add issue to the return array
returnMe.add(myIssue)
}
}
return returnMe
}
@Override
Integer getMinimumNumberOfExpectedArguments() {
0
}
@Override
JiraDataType getDataType() {
JiraDataTypes.ISSUE
}
@Override
String getDescription() {
"Search for issues with no attachments in specific Smart Attachment category."
}
@Override
List<Map> getArguments() {
[
[
description: "Subquery to limit results, Example: (project = abc)",
optional : false,
],
[
description: "Smart Attachment category name.",
optional : false,
]
]
}
@Override
boolean isList() {
true
}
@Override
String getFunctionName() {
"attachmentsInCategory"
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.