Count Words in a multi-line Custom Text Field

DOC February 11, 2019

Hi All,

I am trying to return a count of all of the words in a multi-line text field using a "Calculated Text Field". Is there an in-built function to do this? The ".length" function returns the character count for the contents of the custom field. ".size" does not appear to work on the same custom field. 

If there is not an in built way to do this, I was trying to loop through each character in the field looking for ' ' or '\n' which would indicate a new word, however this is error prone e.g. newlines appended to the end of the text. 

Appreciate any guidance.

Regards

1 answer

0 votes
Luc O. January 8, 2020

Hi,

Not sure what version of JIRA you are using and what add-ons you have installed, but as far as I know, this isn't possible out of the box.  We've done a similar thing using the ScriptRunner add-on.  We simply created a Script Field and have the following code in it:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObjectByName('fieldName')
String cFieldValue = issue.getCustomFieldValue(cField)

if(cFieldValue) {
return cFieldValue.split().size()
}

return 0

Hope this helps.

Suggest an answer

Log in or Sign up to answer