Script to Add a set of Reviewers to different Request Types.

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 needds 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).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)
    }
}
1 This is how you would add a single user as a reviewer. Note the special character '$' needs to be escaped
2 This is how you would add more than one user as a reviewer.
3 If you completely want to override this behaviour delete this line and make it "def list = []"

Have questions? Visit the Atlassian Community to connect, share, and learn with other Atlassian users and experts, including Adaptavist staff.

Ask a question about ScriptRunner for JIRA, Bitbucket Server, or Confluence.

Want to learn more? Check out courses on Adaptavist Learn, an online platform to onboard and train new users for Atlassian solutions.