Hello,
There is the multi-user field Specialists. It's necessary to add its list items separated by comma and space, to the Summary via a post-function..
The following script gives the exception:
java.lang.NullPointerException
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("Specialists")
ApplicationUser user = issue.getCustomFieldValue(cField) as ApplicationUser
cField.each {
summary.add(user + " ")
}
What's wrong with it?
BR
Hi @Ivan D
I've tried the below script on the console and works fine. I've deleted the dedicated issue variable pointing towards a single issue, and I think that it would do the trick:
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.event.type.EventDispatchOption
def cfmanager = ComponentAccessor.getComponent(CustomFieldManager)
def issueManager = ComponentAccessor.getComponent(IssueManager)
String summary = issue.getSummary()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cFieldObj = cfmanager.getCustomFieldObjectByName("Specialists")
def mySize = issue.getCustomFieldValue(cFieldObj).size()
for (int i = 0 ; i < mySize; i++){
summary = summary + ", " + issue.getCustomFieldValue(cFieldObj)[i]
}
issue.setSummary(summary)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
Hi Alex,
Thanks a lot, it works. However for some reason it puts usernames twice: as username(username in brackets) like user1(user1), user2(user2) etc. Can full names be used instead and once?
Also can the script be modified for using in behaviours?
BR
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ivan D
Just place .getName() after the issue.getCustomFieldValue(cFieldObj)[i] :
issue.getCustomFieldValue(cFieldObj)[i].getName()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Alex,
Thank you a lot, it's fixed the issue.
Actually the use case is that - now there is the behaviour that takes a current user to the summary .
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.SUMMARY
@BaseScript FieldBehaviours fieldBehaviours
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser()
def summary = getFieldById("summary")
summary.setFormValue("Education Request ${currentUser.name}")
It's necessary to change it so that instead of the current user it would put Specialists.
Using a post-function is acceptable but it would be better taking values from Specialists during filling in the Create issue form.
BR
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can write the following script on the Specialist field behavior side script:
import com.atlassian.jira.component.ComponentAccessor
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def summary = getFieldById("summary")
def specField = getFieldById(getFieldChanged()).getValue()
summary.setFormValue("Education Request ${currentUser.name} ${specField}")
Let me know if that's what you wanted.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Alex,
Yes, thank you a lot, that's it. The only thing to improve would be getting a full name instead of username. But changing
getValue()
to
getName()
doesn't work here. Is there any substitution?
BR
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ivan D
Sorry for the delayed response but I had little time on my disposal. Here you go:
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def summary = getFieldById("summary")
def description = getFieldById("description")
def specField = getFieldById(getFieldChanged()).getFormValue() as String
String [] myUsersString = specField.split(", ")
def user
def finalString = ""
for (int i=0 ; i < myUsersString.size() ; i++){
if (myUsersString.size() > 0){
user = ComponentAccessor.userManager?.getUserByName(myUsersString[i])
if (user){
finalString = finalString + ", " + user.getDisplayName()
}
}
}
if (finalString){
summary.setFormValue("Education Request " + finalString)
}
I've tested it on my instance and works like the way you want it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Ivan D kindly let me know if you succeeded with the above script I gave you. It would be great if you could mark my answer as accepted, in order to help others in the community. Thank you!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Alex,
Sorry for the delay, postponed this task.
Thank you a lot for the update, it works with adding full names as intended.
The only drawback is it adds comma after "Education Request , ", that is before the 1st added full name.
If changing it to
finalString = finalString + user.getDisplayName() + ", "
it instead adds the extra comma after the last full name in the list.
I've marked the answer as accepted because it works in common. My wishes are rather for its improvement.
Thank you!
BR
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Ivan D your requirement, if not mistaken was:
It's necessary to add its list items separated by comma and space, to the Summary via a post-function.
Then your requirement changed to behaviour script, instead of a workflow post function. I want you to tell me the exact result of what it should appear on the summary. Otherwise we are going to circle around our tail.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Alex,
Sorry for being unclear.
The main goal is: a user shouldn't put anything in the Summary, it's being composed automatically of "Education Request for" and a list of display names from Specialists (multi user picker) separated by a comma and a space. That is like "Education Request for John Johnson, Ann Anderson" where comma and space are only between names.
So there can be 2 approaches by my opinion:
1 (better) - using the Behaviours. Then a user can see what appears in Summary during populating Create screen. Besides editing Summary is possible with adding/removing Specialists if need be.
2 - using a post-function. Then as I see Behaviours should be used anyway to put something in Summary and then a post-function will update it. But it seems in this case editing Summary will reset it by Behaviours value.
Sorry but pros and cons of each approach are being clarified during trials and errors, that's why the initial task can be changed to the more appropriate one.
BR
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you want to go by behaviors then try the following. On my instance it's working as expected:
Try it out and let me know if we nailed it this time.
import com.atlassian.jira.component.ComponentAccessor
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def summary = getFieldById("summary")
summary.setFormValue("Education Request for ")
def description = getFieldById("description")
def specField = getFieldById(getFieldChanged()).getFormValue() as String
String [] myUsersString = specField.split(", ")
def user
def finalString = getFieldById("summary").getFormValue()
for (int i=0 ; i < myUsersString.size() ; i++){
if (myUsersString.size() > 0){
user = ComponentAccessor.userManager?.getUserByName(myUsersString[i])
if (user){
finalString = finalString + user.getDisplayName()
if (i < myUsersString.size()-1){
finalString = finalString + ", "
}
}
}
}
if (finalString){
summary.setFormValue(finalString)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Alex,
Thanks a lot, now it works as intended - Summary can be edited any time but only with Specialists list. The only small adding was needed.
def finalString = getFieldById("summary").getFormValue() as String
The question is closed.
BR.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ivan D ,
if the field is multi-user, you will get list of users, so you should work with the field like this:
def cField = customFieldManager.getCustomFieldObject("Specialists")
List<ApplicationUser> users = issue.getCustomFieldValue(cField) as List<ApplicationUser>
if (users && users.size() > 0) {
users.each {ApplicationUser user ->
// continue here with getting user name...
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Hana,
Thanks for the feedback. However the script still returns
java.lang.NullPointerException
Also can you please specify the part
// continue here with getting user name...
Will this be working?
summary.add(user + " ")
BR
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.