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)
}
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
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Please make sure that you updated your script.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I updated the script but forgot to publish the workflow. Thanks for the help!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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.