Set custom field in post function based on content of summary?

Timothy Harris May 16, 2017

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...

1 answer

1 accepted

3 votes
Answer accepted
Jonny Carter
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.
May 16, 2017

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>

Timothy Harris May 16, 2017

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?

Jonny Carter
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.
May 17, 2017

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.

Timothy Harris May 17, 2017

@Jonny Carter - Thanks. I really appreciate it!

Timothy Harris May 18, 2017

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)
    }

}
Timothy Harris May 18, 2017

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

Suggest an answer

Log in or Sign up to answer