Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Round Robin script works only if the precondition is true

Santa Aga
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 22, 2023

Hello!

I found this script that puts random assignee for issues. https://library.adaptavist.com/entity/round-robin-assign

Brut I need this script to work only on specific Customer request types, not on all issues that are created - 

So precondition would be -

if Customer request type is Type1, Type2 or Type 3,
it runs and puts random assignee (chosen from project role) for issue

Otherwise it uses default assignee (set in request form).

 

I would really appreciate if someone can help me with this precondition.

1 answer

0 votes
Christopher Maxwell
Community Champion
November 23, 2023

Hi @Santa Aga , welcome to the Community Forums!

While I can't help with specifics, I did some research and ScripRunner does have conditions. Both built-in and custom.

Check out these two resources:

https://docs.adaptavist.com/sr4js/latest/features/workflows/conditions

https://docs.adaptavist.com/sr4js/latest/features/workflows/workflow-functions-tutorial/conditions-tutorial

Santa Aga
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 5, 2023

Hi! Thank you!

I have been playing around with the script and also asked chatGPT.

1. Script is used for Create step

2. There is already set assignee for each customer request type by default when creating an issue

3. Script logic meets requirements:
- If Customer Request Type is Type1, it should proceeds with the reassignment logic. It finds the latest created issue with the same request type,
-sets the default assignee to null, and then assigns the issue to the next user in the round-robin sequence.
- else says that it does not allow to change the assignee.

4. The script works without errors, but it still does not changes the original assignee to the one with RoundRobin role

 


import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.security.roles.ProjectRoleManager

 

import com.onresolve.scriptrunner.runner.customisers.WithPlugin

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.servicedesk.api.requesttype.RequestType

 

import groovy.transform.Field

 

def cfRequestType = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10702")

def requestType = issue.getCustomFieldValue(cfRequestType)

 

def issueManager = ComponentAccessor.issueManager

def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

 

if (requestType == "Type1"){

 

    // The role you want assignees to set from

    final roleName = 'RoundRobin'

 

    // If it is true, the assigned issues will be reassigned

    final reassignedIssues = true





    // Get all of the users associated with the specified project role

    def projectRole = projectRoleManager.getProjectRole(roleName)

 

    // Sort the users of the project role using the user key

    def users = projectRoleManager.getProjectRoleActors(projectRole, issue.projectObject)

        .applicationUsers

        .toSorted { it.key }

 

    // There are no users in the specific project role

    if (!users) {

        log.info ("No users for project role $roleName")

        return

    }

 

   if (reassignedIssues) {

        // Find the latest created issue id that has an assignee

        def lastIssueIdWithAssignee = issueManager.getIssueIdsForProject(issue.projectObject.id)

            .findAll { foundIssueId ->

                def foundIssue = issueManager.getIssueObject(foundIssueId)

                foundIssue.requestType.name == "Type1"

            }

            .max()

 

        if (!lastIssueIdWithAssignee) {

            // Set the original assignee to null before reassigning

            issue.setAssigneeId(null)

 

            issue.setAssignee(users.first())

            return

        }

 

        // Set the original assignee to null before reassigning

        issue.setAssigneeId(null)

 

        def lastAssignee = issueManager.getIssueObject(lastIssueIdWithAssignee).assignee

        def lastAssigneeIndex = users.indexOf(lastAssignee)

        def nextAssignee = users[(lastAssigneeIndex + 1) % users.size()]

 

        issue.setAssignee(nextAssignee)

    }

} else {

    log.info('Reassignment not allowed, or the issue is already assigned')

}
Like Christopher Maxwell likes this

Suggest an answer

Log in or Sign up to answer