Forums

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

Put custom field Email in Reporter if user exists

Kevin Verheyden
Contributor
May 23, 2019

Put custom field Email in Reporter if user exists. I am trying to create a script but I can't seem to get it to work.

 

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

def issue = issue as MutableIssue

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Email")
def cfValue = issue.getCustomFieldValue(cf) as ApplicationUser

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

2 answers

1 accepted

2 votes
Answer accepted
Fabio Racobaldo _Herzum_
Community Champion
May 23, 2019

Hi @Kevin Verheyden ,

You missed to store this change to the issue.

The correct script should be the following :

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

MutableIssue issue = issue;
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
CustomField cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Email");
ApplicationUser cfValue = cf!=null?(ApplicationUser)issue.getCustomFieldValue(cf):null;
if(cfValue!=null){
issue.setReporter(cfValue);
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false);
}

Try and let me know.

Hope this helps,

Fabio 

Kevin Verheyden
Contributor
May 23, 2019

Hi Thanks for the help but I get this error.

2019-05-23 10:06:46,566 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'kevin.script2@test.com' with class 'java.lang.String' to class 'com.atlassian.jira.user.ApplicationUser'
 at Script658.run(Script658.groovy:10)
Fabio Racobaldo _Herzum_
Community Champion
May 23, 2019

The email field is not an application user but an email. We need to retrieve user by email.

This code should work :

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.fields.CustomField;
import java.util.Set;


MutableIssue issue = issue;
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
CustomField cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Email");
String cfValue = cf!=null?(String)issue.getCustomFieldValue(cf):null;
if(cfValue!=null){
Set<ApplicationUser> users = ComponentAccessor.getUserManager().getAllUsers();
for(ApplicationUser emailUser : users){
if(cfValue.equalsIgnoreCase(emailUser.getEmailAddress())){
issue.setReporter(emailUser);
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false);
}
}
}
Like Tarun Sapra likes this
Kevin Verheyden
Contributor
May 23, 2019

Same error.

2019-05-23 10:30:08,256 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'kevin.script2@test.com' with class 'java.lang.String' to class 'com.atlassian.jira.user.ApplicationUser'
at Script658.run(Script658.groovy:10)

Fabio Racobaldo _Herzum_
Community Champion
May 23, 2019

Please make sure that you updated your script.

Kevin Verheyden
Contributor
May 23, 2019

I updated the script but forgot to publish the workflow. Thanks for the help!

0 votes
Tarun Sapra
Community Champion
May 23, 2019

Hello @Kevin Verheyden 

You are getting the following error because you are trying to store string in place of Appliction user object, the code shared by @Fabio Racobaldo _Herzum_  should work

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'kevin.script2@test.com' with class 'java.lang.String' to class 'com.atlassian.jira.user.ApplicationUser'
at Script658.run(Script658.groovy:10)

 Can you please share the exact code snippet which you are using right now.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events