Forums

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

How to trim leading/trailing white space from custom field options for a single custom field

Bryan Guffey
Contributor
February 1, 2023

Hi all - 

I'm trying to trim leading whitespace from the select list of custom field options for a single custom field in my Jira instance. It has several contexts, but I'm happy working at through one context at a time. 

I was able to crib a script for ScriptRunner that lists all the custom field options, but when I try to implement the loop to trim the white space, it just returns null.

Here's the script:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.context.IssueContextImpl;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.customfields.option.*;
CustomFieldManager cFM = ComponentAccessor.getCustomFieldManager();
CustomField cf = cFM.getCustomFieldObject("customfield_xxxxx"); // Place your custom field id here
Issue issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("ABCD-123"); // place an issue key (that uses that custom field)
IssueContextImpl issueContext = new com.atlassian.jira.issue.context.IssueContextImpl(issue.getProjectId(), issue.getIssueTypeId());
FieldConfig fieldConfig = cf.getRelevantConfig(issueContext);
OptionsManager optionsManager = ComponentAccessor.getOptionsManager();
List<Option> options = optionsManager.getOptions(fieldConfig);
for (Option option in options) {
    println option.getValue()
    option.setValue(option.getValue().trim())
    println option.getValue()
    }

If I comment out the for (Option... section, it delivers me the list of field options for the given custom field and context. 

Uncommenting the for (Option... section, I just get null

Can anyone assist with the part of the code here that looping to trim the options? Thank you!

1 answer

0 votes
Craig Nodwell
Community Champion
February 1, 2023

Hi @Bryan Guffey .
So in my instance if I try to enter an option in a pull-down list field with leading spaces the system removes those, I tried this a number of times to ensure I was seeing correctly.

Another thing I did was to change your for loop into a closure it no longer throws a null.
But again my options won't take in leading spaces so I don't know if this is your solution.

options.each { option->
//for (Option option in options) {
    println option.getValue()
    option.setValue(option.getValue().trim())
    println option.getValue()
    }

Suggest an answer

Log in or Sign up to answer