Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Copy field-value to reporter

Antonio D'Errico February 6, 2019

Hi everyone,

i need to set the reporter on create issue based on a custom field which is read over nfeed. the custom field E-Mail is the username of the user in jira itself.

we have the addon script runner so we can user a groovy script.

can somebody help me please. i tried several scripts here but it does not work.

 

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

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

def newreporter = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("E-Mail")
def newreportervalue = issue.getCustomFieldValue(newreporter)
issue.setReporter(newreportervalue)

 

1 answer

0 votes
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 6, 2019

Hi @Antonio D_Errico,

the problem should be related to getCustomFIeldValue that should return a generic value and not a ApplicationUser.

 

Please, try this code :

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;


Issue issue = getUnderlyingIssue();
MutableIssue mIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(issue.getKey());

CustomField newreporter = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("E-Mail");
String newreportervalue = issue.getCustomFieldValue(newreporter)!=null?issue.getCustomFieldValue(newreporter).toString():null;

ApplicationUser user = ComponentAccessor.getUserManager().getUserByName(newreportervalue);
if(user!=null){
mIssue.setReporter(user);
}

 Hope this helps,

Ciao,

Fabio

Antonio D'Errico February 6, 2019

hi @Fabio Racobaldo _Herzum_

now this error occurs:

Time (on server): Wed Feb 06 2019 13:09:45 GMT+0100 (Central European Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-02-06 13:09:45,054 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-02-06 13:09:45,054 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: INUBIT-20, actionId: 1, file: <inline script>
java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object
 at Script60.run(Script60.groovy:8)
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 6, 2019

Are you sure hthat CustomFIeld name is correct?

 

try this :


import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.fields.CustomField;


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

CustomField newreporter = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("E-Mail");
String newreportervalue = issue.getCustomFieldValue(newreporter)!=null?issue.getCustomFieldValue(newreporter).toString():null;

ApplicationUser user = ComponentAccessor.getUserManager().getUserByName(newreportervalue);
if(user!=null){
issue.setReporter(user);
}
Antonio D'Errico February 6, 2019

yes i'm sure :->

2019-02-06 14:21:48,390 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-02-06 14:21:48,390 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: INUBIT-22, actionId: 1, file: <inline script>
java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object
 at Script71.run(Script71.groovy:10)
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 6, 2019

got it! issue object is null.

Try this :

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;


Issue issue = getUnderlyingIssue();
MutableIssue mIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(issue.getKey());

CustomField newreporter = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("E-Mail");
String newreportervalue = issue.getCustomFieldValue(newreporter)!=null?issue.getCustomFieldValue(newreporter).toString():null;

ApplicationUser user = ComponentAccessor.getUserManager().getUserByName(newreportervalue);
if(user!=null){
mIssue.setReporter(user);
}
Antonio D'Errico February 6, 2019
2019-02-06 14:33:01,573 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-02-06 14:33:01,573 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: INUBIT-23, actionId: 1, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getUnderlyingIssue() is applicable for argument types: () values: []
 at Script76.run(Script76.groovy:7)
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 6, 2019
Issue issue = getUnderlyingIssue();

this method is provided by ScriptRunner. Are u using this plugin or is it something custom? In the second option you need to retrieve the issue key based on the context.

Ciao,

Fabio 

Antonio D'Errico February 6, 2019

yes i'm sure, scriptrunner is installed and up2date.

Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 6, 2019

strange, it should work. 

Try using directly issue (delete line "Issue issue = getUnderlyingIssue();" from previous script.

Let me know,

Ciao,

Fabio

Antonio D'Errico February 6, 2019

there is no error now but the reporter is not set / changed :-<

Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 6, 2019

You need to persist the change :

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;


IssueManager issueManager = ComponentAccessor.getIssueManager();
MutableIssue mIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(issue.getKey());

CustomField newreporter = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("E-Mail");
String newreportervalue = issue.getCustomFieldValue(newreporter)!=null?issue.getCustomFieldValue(newreporter).toString():null;

ApplicationUser user = ComponentAccessor.getUserManager().getUserByName(newreportervalue);
if(user!=null){
mIssue.setReporter(user);
issueManager.updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), mIssue, EventDispatchOption.DO_NOT_DISPATCH, false);
}
Antonio D'Errico February 6, 2019

and again no errors but the reporter remains unchanged :-<

Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 6, 2019

Please debug your code in order to figure out if user has a value

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events