Hi
I am new to Scriptrunner but we have it as a plugin and I want to change the reporter of an issue to another user via a post function in the workflow. Could someone supply working groovy script for Jira 8.7?
Thanks
Gordon
Thank you Fabio that is working perfectly for me and I really appreciate your time on this.
Could I just ask if I wanted to combine your code with my code below which is to create a sub task and amend the name and assign it to a user, what would that look like?
Thanks again
Gordon
I'm happy that it worked. Please could you specify if you need to create the subtask in the same postfunction that update the reporter.
And please accept my answer and not yours :)
Ciao,
Fabio
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Apologies re the accepted answer! Hopefully correct now. My eyes are not what they used to be! Yes correct - same post function.
Thanks again
Gordon
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Gordon Rutherford ,
try this code, I missed import of IssueType class :
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.config.IssueTypeManager;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.issue.IssueFactory;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.issuetype.IssueType;
IssueManager issueManager = ComponentAccessor.getIssueManager();
UserManager userManager = ComponentAccessor.getUserManager();
IssueTypeManager issueTypeManager = ComponentAccessor.getComponentOfType(IssueTypeManager.class);
IssueFactory issueFactory = ComponentAccessor.getIssueFactory();
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
ApplicationUser reporter = userManager.getUserByKey("YOUR_REPORTER_KEY_HERE");
issue.setReporter(reporter);
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false);
Project project = issue.getProjectObject();
IssueType subTaskIssueType = issueTypeManager.getIssueType("YOUR_SUBTASK_ISSUE_TYPE_NAME_HERE");
MutableIssue mIssue = issueFactory.getIssue();
mIssue.setProjectObject(project);
mIssue.setIssueTypeObject(subTaskIssueType);
mIssue.setParentObject(issue);
mIssue.setSummary("Edit Promo: "+issue.getSummary());
mIssue.setReporter(reporter);
mIssue.setAssignee(reporter);
issueManager.createIssueObject(user, mIssue);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Fabio
Sorry I have only just had a chance to test and it fails
Here is the log from Jira - do you need anything else to diagnose?
Regards
Gordon
2021-03-17 16:47:40,394 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue COR-50 for user 'Gordon.Rutherford'. View here: http://jira.uk.broadcast.qvc:8080/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=POM%2FSOM%2FDOM&descriptorTab=postfunctions&workflowTransition=1&highlight=2
com.google.common.util.concurrent.UncheckedExecutionException: java.lang.NullPointerException: null value in entry: issue=null
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2050)
at com.google.common.cache.LocalCache.get(LocalCache.java:3952)
at com.google.common.cache.LocalCache$LocalManualCache.get(LocalCache.java:4871)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.getValuesForIssueId(EagerLoadingOfBizCustomFieldPersister.java:103)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.getValuesForTypeAndParent(EagerLoadingOfBizCustomFieldPersister.java:86)
at com.atlassian.jira.issue.customfields.persistence.OfBizCustomFieldValuePersister.updateValues(OfBizCustomFieldValuePersister.java:151)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.updateValues(EagerLoadingOfBizCustomFieldPersister.java:61)
at com.atlassian.jira.issue.customfields.persistence.OfBizCustomFieldValuePersister.createValues(OfBizCustomFieldValuePersister.java:108)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.createValues(EagerLoadingOfBizCustomFieldPersister.java:49)
at com.atlassian.jira.issue.customfields.persistence.OfBizCustomFieldValuePersister.createValues(OfBizCustomFieldValuePersister.java:101)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.createValues(EagerLoadingOfBizCustomFieldPersister.java:43)
at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(AbstractSingleFieldType.java:143)
at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:728)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:681)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:667)
at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:217)
at com.atlassian.jira.issue.IssueManager$updateIssue$49.call(Unknown Source)
at Script396.run(Script396.groovy:21)
Caused by: java.lang.NullPointerException: null value in entry: issue=null
at com.google.common.collect.CollectPreconditions.checkEntryNotNull(CollectPreconditions.java:32)
at com.google.common.collect.SingletonImmutableBiMap.<init>(SingletonImmutableBiMap.java:42)
at com.google.common.collect.ImmutableBiMap.of(ImmutableBiMap.java:72)
at com.google.common.collect.ImmutableMap.of(ImmutableMap.java:124)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.lambda$getValuesForIssueId$0(EagerLoadingOfBizCustomFieldPersister.java:104)
at com.google.common.cache.LocalCache$LocalManualCache$1.load(LocalCache.java:4876)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3528)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2277)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2154)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2044)
... 20 more
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Gordon Rutherford ,
try this code :
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.config.IssueTypeManager;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.issue.IssueFactory;
import com.atlassian.jira.issue.MutableIssue;
IssueManager issueManager = ComponentAccessor.getIssueManager();
UserManager userManager = ComponentAccessor.getUserManager();
IssueTypeManager issueTypeManager = ComponentAccessor.getComponentOfType(IssueTypeManager.class);
IssueFactory issueFactory = ComponentAccessor.getIssueFactory();
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
ApplicationUser reporter = userManager.getUserByKey("YOUR_REPORTER_KEY_HERE");
issue.setReporter(reporter);
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false);
Project project = issue.getProjectObject();
IssueType subTaskIssueType = issueTypeManager.getIssueType("YOUR_SUBTASK_ISSUE_TYPE_NAME_HERE");
MutableIssue mIssue = issueFactory.getIssue();
mIssue.setProjectObject(project);
mIssue.setIssueTypeObject(subTaskIssueType);
mIssue.setParentObject(issue);
mIssue.setSummary("Edit Promo: "+issue.getSummary());
mIssue.setReporter(reporter);
mIssue.setAssignee(reporter);
issueManager.createIssueObject(user, mIssue);
Please let me know if it works for you and, please, click on Accept Answer on this answer :)
Ciao,
Fabio
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Fabio
Only just came back to try this now so apologies for the delay - I get the error below on the line where I have entered the name of the sub task
Regards
Gordon
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Gordon Rutherford ,
try this code :
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserManager;
IssueManager issueManager = ComponentAccessor.getIssueManager();
UserManager userManager = ComponentAccessor.getUserManager();
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
ApplicationUser reporter = userManager.getUserByKey("YOUR_REPORTER_KEY_HERE");
issue.setReporter(reporter);
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false);I don't know if the reporter to set is within the source issue in another Field. BTW, this code should help you.
Fabio
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Fabio
I appreciate your input!
I realise I should have brought more detail - there are two use cases for this script - one when an issue is created (not updated) and perhaps that is causing this error shown in snip image shown below when I inserted it in that post function?
The other is changing a subtask reporter when the sub task is created by scriptrunner with this additional issue action which I want to add to with also the 'change of reporter'
issue.summary = 'Add Chan PD Image Request: ' + sourceIssue.summary
import com.atlassian.jira.component.ComponentAccessor
// the username of the assignee
final String userName = "q1424781"
def user = ComponentAccessor.userManager.getUserByName(userName)
issue.setAssignee(user)
Thanks for your help with this!
Best regards
Gordon
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
HI @Gordon Rutherford ,
here the code updated (the problem was that EventDispatchOption was not imported).
Please move this post function after issue creation.
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.event.type.EventDispatchOption;
IssueManager issueManager = ComponentAccessor.getIssueManager();
UserManager userManager = ComponentAccessor.getUserManager();
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
ApplicationUser reporter = userManager.getUserByKey("YOUR_REPORTER_KEY_HERE");
issue.setReporter(reporter);
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false);
Moreover, in order to set the reporter use the method issue.setReporter(user) as per Assignee.
Hope this helps,
Fabio
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Fabio
Thanks for your response! So I have inserted the new code after 'Create' in the post function order as shown below where 'q1454625' is the 'new' reporter I want for the issue. This however does not update in the created issue - Reporter shows as 'anonymous'.
Sorry if I am missing something as I also didn't understand you last comment:
'Moreover, in order to set the reporter use the method issue.setReporter(user) as per Assignee.' Does that mean I need to add some extra code to the script shown below to make it work and update the Reporter?
Thanks again for your help
Best regards
Gordon
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Gordon Rutherford ,
the problem was during finding user process. I fised this issue. Please try this code :
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.event.type.EventDispatchOption;
IssueManager issueManager = ComponentAccessor.getIssueManager();
UserManager userManager = ComponentAccessor.getUserManager();
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
ApplicationUser reporter = userManager.getUserByName("YOUR_REPORTER_KEY_HERE");
issue.setReporter(reporter);
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.