Auto Add Reviewers Based on Request Type

Case Study:

When an issue is created, users have to manually select the reviewer of their task. The admin wants to set it automatically depending on the request.

This makes sense if, for example, a purchase over 100 dollars needs to be cleared by one department and a travel request needs to be cleared by your manager.

Steps:

1. Select the onCreate transition of your workflow.

Remember this can only be accessed through the diagram, and not the text editor.

2. Click Add Scripted Function>Custom Scripted Function.

It is very important that this is the FIRST function if set on the onCreate transition, else it will not update your value.

3. Add the following code:

Please note that in this case study, we want to add reviewers for the two basic request types that require reviewers: "Purchase Over $100", and "Travel Request".

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.servicedesk.api.requesttype.RequestTypeService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.opensymphony.workflow.WorkflowContext

@WithPlugin("com.atlassian.servicedesk")

/*This is a map of request-Type -> Request Reviewers*/
def userMap = [
    "Purchase over \$100": ["admin"], // <1>
    "Travel request"     : ["anuser", 'user2', 'user3'] // <2>
]

def currentUserId = ((WorkflowContext) transientVars.get("context")).getCaller()
def currentUser = ComponentAccessor.getUserManager().getUserByKey(currentUserId)

/*----------------------------------------------------------------*/

if (issue.issueType.name == "Service Request with Approvals") {

    def requestTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(RequestTypeService)

    def sourceIssueRequestTypeQuery = requestTypeService
        .newQueryBuilder()
        .issue(issue.id)
        .requestOverrideSecurity(true)
        .build()

    def changeHolder = new DefaultIssueChangeHolder()

    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def requestTypeEither = ComponentAccessor.getOSGiComponentInstanceOfType(RequestTypeService).getRequestTypes(currentUser, sourceIssueRequestTypeQuery)

    if (requestTypeEither.isLeft()) {
        log.error "${requestTypeEither.left().get()}"
        return
    }

    def requestType = requestTypeEither.right.results[0]
    def cfChange = customFieldManager.getCustomFieldObjectByName("Approvers")
    def userManager = ComponentAccessor.getUserManager()

    /*This is done, so that if a user adds a reviewer, it doesn't get replaced, and instead it's added to the list*/
    def list = issue.getCustomFieldValue(cfChange) ?: [] // <3>

    if (userMap.keySet().contains(requestType.name)) {
        def users = userMap[requestType.name]
        def toAdd = users.findResults {
            userManager.getUserByKey(it) ?: log.warn("User with key ${it} does not exist")
        }

        list.addAll(toAdd)
        issue.setCustomFieldValue(cfChange, list)
    }
}

Line 10: This is how you would add a single user as a reviewer. Note the special character '$' needs to be escaped

Line 11: This is how you would add more than one user as a reviewer

Line 44: If you completely want to override this behaviour delete this line and make it "def list = []"

On this page