Forums

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

Calculated Text Field

Roy Chapman March 22, 2019

I have a calculated text field, using this code

This field displays only the first names of the assignees

<!-- @@Formula:




StringBuilder sb = new StringBuilder();

sb.append("* ");

List people = issue.get("customfield_10427");




if (people == null) {

   sb.append(" unassigned ");

   return sb.toString();

}


//Count resolved items

for (Object  item : people) {

    com.atlassian.jira.user.ApplicationUser user = (com.atlassian.jira.user.ApplicationUser) item;

    String[] fullName = user.getDisplayName().split(" ", 2);

    String firstname = fullName[0];

    String surname = fullName[1];

    sb.append(firstname).append(" ").append(surname.charAt(0));

    sb.append(", ");

}

 

The problem I have found is that if the assignee in customfield_10427 is departed or inactive, the "com.atlassian.jira.user.ApplicationUser user" returns nothing and this generates an error

Target exception: java.lang.ArrayIndexOutOfBoundsException

How can I test if the list fullName is empty?  I have tried various but the script skips over (i.e. doesn't see the list empty) and continues to fail.

Ideally I am trying to achieve the following (but using a method that works)

// Check if details returned are null




if (fullName == null) {

   sb.append(" Departed ");

   return sb.toString();

}

I have drilled into this a bit more.  the issue isn't that the user is deactivated (although the error happens on all deactivated users, this is unrelated) but that user name only has a single name (no forename, surname).  As a result, this fails

    String[] fullName = user.getDisplayName().split(" ", 2);

    String firstname = fullName[0];

    String surname = fullName[1];

Because there is no second element in the list.  I have tried using assert statements to test fullName[1] and I have tried counting the number of elements.  Both don't seem to conform to groovy standards and generate syntax errors

Thoughts?

 

1 answer

1 accepted

0 votes
Answer accepted
Antoine Berry
Community Champion
March 22, 2019

Hi, 

You could use this instead

if (!user.isActive()){

sb.append(" Departed ")

return sb.toString()

}
Roy Chapman March 25, 2019

Antoine,

I have drilled into this a bit more.  the issue isn't that the user is deactivated (although the error happens on all deactivated users, this is unrelated) but that user name only has a single name (no forename, surname).  As a result, this fails

    String[] fullName = user.getDisplayName().split(" ", 2);

    String firstname = fullName[0];

    String surname = fullName[1];

Because there is no second element in the list.  I have tried using assert statements to test fullName[1] and I have tried counting the number of elements.  Both don't seem to conform to groovy standards and generate syntax errors

Thoughts?

Antoine Berry
Community Champion
March 25, 2019

Weird, testing fullName[1] against null should work.

Anyway, you can use 

String[] fullName = user.getDisplayName().split();

And don't forget to log to debug and see if anything is weird : 

log.error("fullName : " + fullName)
log.error("fullName size : " + fullName.size())
log.error("fullName class : " + fullName.getClass())
log.error("fullName[0] : " + fullName[0])
log.error("fullName[1] : " + (fullName[1] ? fullName[1] : "null"))

If you still face the same issue,  you can try 

String firstname 
String surname
if (fullName.contains(" ")){
String[] fullName = user.getDisplayName().split()
firstname = fullName[0]
surname = fullName[1]
}
else {
firstname = user.getDisplayName()
}

Let me know if that helped.

Roy Chapman March 25, 2019

Antione,

Huge thanks.  Using 

if (fullName.contains(" ")){

Sorts the problem.  This avoids a check on the second element.

All working now

Like Antoine Berry likes this

Suggest an answer

Log in or Sign up to answer