So, been a while since I have done some workflow stuff.
I seem to remember being able to set a custom field value in a post function based on some JQL.
But, for the life of me, I can't remember which plugin gives this functionality.
Pretty sure I can use script runner for this, but wanted to see, if there is a free plugin which will provide the same functionality. The customer isn't really into scripting and maintaining scripts...
FWIW, you're right that ScriptRunner can totally do this. It's pretty straightforward to execute a JQL query in your script, then assign a value derived from the results to a system or custom field.
For example, let's say you just wanted a field that displayed the number of issues returned by a particular query:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.search.SearchProvider import com.atlassian.jira.jql.parser.JqlQueryParser import com.atlassian.jira.web.bean.PagerFilter def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser) def searchProvider = ComponentAccessor.getComponent(SearchProvider) def user = ComponentAccessor.getJiraAuthenticationContext().getUser() // edit this query to suit def query = jqlQueryParser.parseQuery("project = JRA and assignee = currentUser()") def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter()) def customFieldManager = ComponentAccessor.customFieldManager def numberCf = customFieldManager.getCustomFieldObjectByName("My Number Field") issue.setCustomFieldValue(numberCf, results.total )
You could use Groovy's iterators to aggregate up particular data points.
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.search.SearchProvider import com.atlassian.jira.jql.parser.JqlQueryParser import com.atlassian.jira.web.bean.PagerFilter def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser) def searchProvider = ComponentAccessor.getComponent(SearchProvider) def user = ComponentAccessor.getJiraAuthenticationContext().getUser() // edit this query to suit def query = jqlQueryParser.parseQuery("project = JRA and assignee = currentUser()") def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter()) def customFieldManager = ComponentAccessor.customFieldManager def numberCf = customFieldManager.getCustomFieldObjectByName("My Number Field") def someOtherNumberCf = customFieldManager.getCustomFieldObjectByName("Some other number field") def issueManager = ComponentAccessor.issueManager def sum = results.issues.sum{ resultDocumentIssue -> def resultIssue = issueManager.getIssueByCurrentKey(resultDocumentIssue.key) resultIssue.getCustomFieldValue(someOtherNumberCf) as int } issue.setCustomFieldValue(numberCf, sum)
<mostly shameless sales pitch>
If the concern is just having the institutional knowledge to maintain the scripts, there is a course available on ScriptRunner for JIRA.
</mostly shameless sales pitch>
Thanks :-) One quick question. I want the user updating the field to be a specific admin user. What is the mthod for getting a specific named user?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The UserManager should do.
import com.atlassian.jira.component.ComponentAccessor def userManager = ComponentAccessor.userManager def user = userManager.getUserByName("someuser") //OR user = userManager.getUserByKey("someuser1245")
For a JIRA instance with one directory, the user name and user key will typically be the same. If you have multiple directories, though, sometimes the user key will be a generated unique identifier to help JIRA distinguish between different users that have the same username in the external directory. If that's the case for you, it's best to use the getUserByKey method to be certain you're getting the right person.
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.
Why oh why does this not update the issue? Everything seems to work but the issues custom field never gets set?
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.event.type.EventDispatchOption import com.atlassian.jira.issue.search.SearchProvider import com.atlassian.jira.jql.parser.JqlQueryParser import com.atlassian.jira.web.bean.PagerFilter def KEY = "key = " def ESCAPE = "\\" def QUOTE = '"' def LABEL = "SPU" def BRACKET_OPEN = "[" def BRACKET_CLOSE = "]" def CHANNEL = "request-channel-type = email" def CONTAINS = " ~ " def OPERATOR = " AND " def FIELD = "summary" def REQ_EX = ~".*(\\[${LABEL}\\]).*" //TODO - Use a an admin user that we are sure has access? Or is this necessary def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() // Setup and execute query def QUERY = KEY + issue.getKey() + OPERATOR + FIELD + CONTAINS + QUOTE + ESCAPE + ESCAPE + BRACKET_OPEN + LABEL + ESCAPE + ESCAPE + BRACKET_CLOSE + QUOTE def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser) def searchProvider = ComponentAccessor.getComponent(SearchProvider) def query = jqlQueryParser.parseQuery(QUERY) def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter()) // Update issue if label is exact match def issueManager = ComponentAccessor.getIssueManager() def customFieldManager = ComponentAccessor.getCustomFieldManager() def productCF = customFieldManager.getCustomFieldObjectByName("Product") def optionsManager = ComponentAccessor.getOptionsManager() results.getIssues().each { documentIssue -> log.warn(documentIssue.key) // A mutable issue to work with def issueMutable = issueManager.getIssueObject(documentIssue.id) // As JIRA does not support exact matches with the contain operator. // We need to do it with Java RegEx. sigh... if (issueMutable.getSummary() ==~ REQ_EX) { def cfConfig = productCF.getRelevantConfig(issueMutable) def option = optionsManager.getOptions(cfConfig)?.find { it.toString() == LABEL } issueMutable.setCustomFieldValue(productCF,option) //issueManager.updateIssue(user,issueMutable, EventDispatchOption.ISSUE_UPDATED,false) log.warn("Value is now: " + issueMutable.getCustomFieldValue(productCF)) // do something to the issue... log.warn("MATCHED: " + issueMutable.summary) } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
And if we place it after the reindex, because it is on the create transition and use:
issueManager.updateIssue(user,issueMutable, EventDispatchOption.ISSUE_UPDATED,false)
Then it works :D
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Spend the day sharpening your skills in Atlassian Cloud Organization Admin or Jira Administration, then take the exam onsite. Already ready? Take one - or more - of 12 different certification exams while you’re in Anaheim at Team' 25.
Learn more
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.