Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How do I set a character limit on a custom field using scriptrunner in Jira Cloud?

Jessica Verge March 29, 2018

I am way out of my league here. I attempted searching the database for a script to use in scriprunner for my issue, but I have found that their answers didn't work for me. That could be because they are running scriptrunner on a server and not cloud. One solution was to use scriptrunner behaviours but I am unable to find that in the cloud version. Any help would be greatly appreciated. 

3 answers

Suggest an answer

Log in or Sign up to answer
2 votes
James Quinton April 27, 2018

Hello.  You can resolve this by adding a regular expression check to the workflow transition.  Much simpler than a groovy script.

 

Go to Project Setting > Workflows

Edit the workflow for the affected issue type

Switch to Diagram mode

On Create transition, click on the Validators

Choose Add Validator > Regular Expression Check

 

If you would like to restrict the field to a specific length:  enter [0-9]{n}$

 

(n) reflects your desired field length.

 

You just need to set this on the required workflow transition (ideally after the initial field input is required)

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 27, 2018

What app are you using to provide the regex?

James Quinton April 27, 2018

Hi. This is provided via (JSU)

1 vote
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 3, 2018

While I believe you can use scriptrunner to help with this, you could also just use a bit of javascript to help here.   @Andy Keyworth had a post over on https://community.atlassian.com/t5/Jira-Core-questions/Setting-a-character-limit-on-a-text-field-single-line/qaq-p/66709

with some example code, that @SABVARX also helped with.

What you can do is go into the field description in Jira, and then input a script like this:

<script type="text/javascript">
function validate()
    {   
        if(document.getElementById("customfield_19600").value.length >33)
        {
            alert( "Invalid length, must be no more than 33 characters" );
            return false;
        }   
    }
document.getElementById("customfield_19600").addEventListener("keypress", validate);
</script>

In this case, you would have to substitute the 'customfield_19600' with the customfield number this is, and you can also change the specific length you want these values to be.   I would recommend looking into How to find id for custom field(s)?  in order to find your custom field id.   And you also might need to review the documentation on Configuring a custom field in order to understand where you can edit the custom field description.

Sudheer Kumar Vempala March 12, 2019

@Andy Heinzer 

I am also lokking for the similar usecase. I have tried this script , It seems working on create screen but on edit screen this validation is not working. Any suggestions.

Appreciate your help.

Sudheer Kumar Vempala March 12, 2019

FYI -We are using jira server 7.6.6

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 14, 2019

Hi Sudheer,

In hindsight of this answer, I think I actually prefer James Quinton's answer on this post over my own now.  The approach I originally suggested to use a javascript to limit a field length is flawed.  The main reason for this is that it does not cover all the ways that this field could be edited in Jira.  For example, if users access the REST API to create/edit issues, they would never see this field is restricted in any way.

Whereas with James' approach, the use of a workflow validator can prevent users from entering a value you do not want to see here in both input methods.   In his case, I believe he was using the JSU plugin to get this validator to work.  However I'm sure there are other plugins available in Marketplace for Jira that can also provide this same kind of validator functionality.  For example, the Workflow Enhancer for Jira also appears to offer regex validators, and it appears to be a free plugin at this time.

Natalie Al-Delemi May 16, 2019

I get an error when using this script at:

<script type="text/javascript">

 

Is there a specific import package to be used?

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 17, 2019

Hi Natalie,

I believe that changes to Jira Cloud have made my answer irrelevant to that platform.  If you're using Jira Cloud, you can no longer add these custom java scripts to places like field descriptions or the announcement banner in Jira Cloud.

These places could still be utilized for Jira Server platforms as a way to do this.  However the Cloud suite has restricted this ability.

I hope this helps.

Andy

Like # people like this
0 votes
Natalie Al-Delemi May 18, 2019

Thanks Andy for replying.. my bad I didn't notice this is all related to cloud. I'm actually using a server.

TAGS
AUG Leaders

Atlassian Community Events