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

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

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?

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.

@Jonny Carter - Thanks. I really appreciate it!

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

}

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 21, 2018 in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

1,197 views 10 18
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you