It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Change issue reporter with ScriptRunner

Hi Community,

I am completely new regarding to ScriptRunner topics and need some good start. In the end I want to perform a simple postfunction as soon as a issue was created. I have an customer field "On behalf of" there customers could create a issue for their colleagues.

My intention is, as soon as this field "On behalf of" != null the system field creator/reporter should be replaced. "On behalf of" is a single-user-picker field as well. A business case could be that a colleague has currently no time to create an issue by themself and another colleagues jumps in without being notified the whole time.

If you have an idea by any chance I would appreciate it.

Best,

Kristian

2 answers

1 accepted

2 votes
Answer accepted
Mark Markov Community Leader Jul 02, 2018

Hello, 

As @Danyal Iqbal says scriptrunner documentation is good place to start.

Second one is this community, there are tons of quiestions and answers with code, just google it :)

And for your example you can use this code in posfunction

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("")

def onbehalfCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("On behalf of")
def onbehalfValue = issue.getCustomFieldValue(onbehalfCustomField)
if (onbehalfValue){
issue.setReporter(onbehalfValue)
}

And do not forget to place this postfunction right befoge "Update change history for an issue and store the issue in the database."

Hi Mark,

Just quickly two different questions.

issue.setReporter(onbehalfValue)

I receive an error [Static type checking] - cannot find matching method. I also checked the API which is actually fine with this function. You mentioned

And do not forget to place this postfunction right befoge "Update change history for an issue and store the issue in the database."

Means I cant do this post-function right after "create", before a customized status steps in? Because I actually need this before any ticket reaches the first status. I am asking because the "Update change history for an issue and store the issue in the databse" action is not mentioned during the first post-function after "create".

Best,

Kristian

Mark Markov Community Leader Jul 03, 2018

Hi Kristian,

This is static type checking error, you can ignore that. This is happens because method getCustomFieldValue defined return object value (actually it can returns Options object Application User object, String object), so compiler doesnt know which one returns and set this error, but we know that from User picker field it will return Application User.

Yes, you can place this after create (main requiriment is that issue object must be created), but if something esle will change reporter after, it will not work.

Hi Mark,

Thanks for your help and time.

The script runs now for each new created issue but unfortunately the reporter doesn´t change. Maybe I did something wrong from the field perspective, anyway thanks.

Best,

Kristian

Mark Markov Community Leader Jul 03, 2018

It seems you have some logic between creates issue and update change history.

Could you provide more information about it?

Hi Mark,

Thanks for your help.

Actually there is no other logic behind. Later during different transitions yes, but non of them affects the reporter. Just email notifications.

As you can see below the script itself executes after each issue. I also switched the position of the script right after "Creates the issue originally". The other steps are default steps during the service desk creation.

Transition Create issue.JPG

I only changed line 4, because the script should affect each issue and not just one specific. I will do some other tests and will inform you about my outcome.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

MutableIssue issue = issue

def onBehalfOfCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("ASD: On behalf of")
def onBehalfOfValue = issue.getCustomFieldValue(onBehalfOfCustomField)

if (onBehalfOfValue){
    issue.setReporter(onBehalfOfValue)
}

log.info("Issue reporter: " + issue.reporter)

Best,

Kristian

Hi Mark,

A quick update.

I adapt some changes which I found in the API. I am able to set the reporter and assignee static as a test but as soon as I put a reference to my customfield the logs says "null" and the ticket assignee changing to "Unassigned" and the reporter to "Anonymous".  We also forgot to store the issue after this changes.

Obvously the reference to the value of the customfield seems wrong. I will keep searching how to set a user picker field proberly.

Below the static code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("HOSD-276")

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("ASD: On behalf of")
def newReporter = customField.getValueFromIssue(issue)

if (customField){
    //Reference currently not working
    issue.setReporterId(newReporter)
    issue.setAssigneeId(newReporter)
} else {
    //Static works fine
    issue.setReporterId('ldrew')
    issue.setReporterId('ldrew')
}

issue.store()

log.info("Issue reporter: " + issue.reporter)
log.info("Issue assignee: " + issue.assignee)

Best,

Kristian

Danyal Iqbal Community Leader Jul 04, 2018
def newReporter = customField.getValueFromIssue(issue)

use 

def onbehalfValue = issue.getCustomFieldValue(customField)

 instead.

Write a proper postfunction if you want to implement it for every issue on creation or take the easier route below:

Fire a custom event on creation e.g issue_created_set_reporter and . Catch the event in the listener and use event.getissue to  get every issue on creation instead of getting the issue by key.

Mark Markov Community Leader Jul 04, 2018 • edited

Hi, Kristian

Regarding your first of two last posts

As i can see on screenshot, postfunction placed before issue creates and obviously that will not work, cause you tryin to set reporter to issue that doesnt exist. Place your postfunction after "Creates the issue originally"

MutableIssue issue = issue

 you can remove this line, postfunction scope itself already contains issue variable.

after this changes your postfunction will be work on all issues.

 

Regarding second

@Danyal Iqbal already answer you :)

And do not use issue.store()

it is deprecated and may cause some unexpected bugs

see ref

https://docs.atlassian.com/software/jira/docs/api/7.6.1/index.html?com/atlassian/jira/issue/MutableIssue.html

use 

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor;

ComponentAccessor.getIssueManager().updateIssue(issue.getReporter(), issue, EventDispatchOption.ISSUE_UPDATED, false)

instead.
But in postfunction, if you place postfunction before "Update change history" and Re-index, you dont need to store changes in code.

Hi Mark,

Thanks for the hints. I adjust my script.

I realized as soon as I leave my custom field empty and the if condition should be ignored, I receive "Anonymous" as a reporter. I have two explanations for that:

  1. The custom field throws a value even than not touched
  2. I need to add a else condition with the currently logged user as reporter

Best,

Kristian

0 votes
Danyal Iqbal Community Leader Jul 02, 2018

a good place to start would be the script runner documentation

and the 101 code example below:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue


def currentAppUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()


issue.setReporter(currentAppUser);

 

 

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Jira Service Desk

Jira Service Desk Admin Certification (ACP-400) now $150 until 31 July

More and more people are building their careers with Atlassian, and we want you to be at the front of this wave! Important Dates Start the free Certification Prep Course by 30 June 2019 (earlier ...

201 views 0 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you