Change assignee Script-Runner global automation

Dears

I am planing to create a global automation rule that change the assignee to Designated Employee( user custom filed) in any issue if the assignee is on the out of office group the value of Designated Employee get fetch from out of office Ticket the code does not give me an Error but doe not change the assignee can you please advise

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;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.UserManager
import java.util.Arrays;
import java.util.List;

def keyiss=  issue.getKey()
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(keyiss)
def assignee = issue.getAssignee().getDisplayName()
//return assignee
def reporter 
def cfUpdate
def assignee2

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def query = jqlQueryParser.parseQuery("project = OOO AND status = \"Out Off Office\"");
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter());//gets results of query
double NumResults = results.total;

results.getIssues().each { 
  documentIssue ->

def issue2 = issueManager.getIssueObject(documentIssue.id)
reporter = issue2.getReporter().getDisplayName() 
//return reporter
  
  if( reporter == assignee){
  cfUpdate = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Designated Employee") 
 assignee2 = issue2.getCustomFieldValue(cfUpdate) as ApplicationUser 
] 
    }


}
issue.setAssignee(assignee2)



Hey there!

This appears to be a request for Adaptavist’s support team. You should probably file it there :slight_smile:

Just a shot in the dark… but maybe a typo in your status parameter? Out Off Office instead of Out of Office ?

thank you I will try to contact them

thanks, unfortunately the status has typo
when I try it on the scripted filed it return the right value but I don’t know why it does not work

This is a Scripted Service? A Listener? You can’t just get a MutableIssue and call #setAssignee(ApplicationUser applicationUser).

You should be leveraging the class IssueService to perform updates to tickets like this.

sorry, but can you explain more?

The scenario goes like this when the employee want to take a vacation He open out of office ticket and In the ticket he will mention the date his vacation will start and who is the employee will replace him ( Designated Employee) When his vacation start the ticket will go to out of office status And the system will Add him to out of office group After that there is a global automation When any ticket get assigned to if the assignee is in out of office group run this code

Is this a listener? Is this a service? Where are you configuring this in scriptrunner.

I configure it on the scriptrunner option on the automation

Okay. You are probably best reaching out to Adaptavist or CodeBarrel then. Adaptavist creates the ScriptRunner plugin, and CodeBarrel creates the Automation plugin. I’ve never used them in tandem.

It’s still not clear what is ACTUALLY triggering this. You haven’t provided information around what actually triggers this code. What triggers the automation?

Sure, thank you
I chose the issue assigned trigger
“When any ticket get assigned to if the assignee is in out of office group run this code”

Okay: My previous statement stands then. This is being called as an Event Listener. You cannot just call #setAssignee(ApplicationUser applicationUser).: It will not work.

Instead, follow this documentation: https://developer.atlassian.com/server/jira/platform/performing-issue-operations/
Something like

final IssueInputParameters input = issueService.newIssueInputParameters();
input.setAssigneeId(String username);

final IssueService.IssueValidationResult validationResult = 
    issueService.validateUpdate(currentUser, issueId, input);
if (!validationResult.isValid()) {
    return null;
}

final IssueService.IssueResult updateResult = 
    issueService.update(currentUser, validationResult);
if (!updateResult.isValid()) {
    return null;
}

final ApplicationUser newAssignee = updateResult.getIssue().getAssignee();
1 Like