Forums

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

Is it possible to use regex inside scriptrunner behaviours in Jira Cloud?

Rodrigo Méndez March 3, 2024
I want to add a regex to validate that a field input has the correct number of digits and if so to change the description to a custom message. The field in question is a Paragraph field.
So far I haven't been able to make it work. Using a validator is the very last option.
const changedField = getChangeField();

const regex = /\\d{7,}\\s*\\d{7,}/

if (changedField.getType() == "com.atlassian.jira.plugin.system.customfieldtypes:textfield" &&

    changedField.getName() == "My Customfield" &&

    !regex.test(changedField.getValue().toString())) {

    getFieldById("customfield_10050").setDescription("Correct");

} else {

    getFieldById("customfield_10050").setDescription("Wrong");

}

3 answers

1 accepted

1 vote
Answer accepted
Rodrigo Méndez March 4, 2024

I actually solved it with this code. Instead of textfield I changed it to textarea and it worked. I also changed the regex to the below format.

 

const changedField = getChangeField();

const regex = /^(?:(?:\d{7,}\s+){0,2}\d{7,})$/; // Simplified regex

const logs = !regex.test(changedField.getValue().toString())

logger.info(logs.toString())

if (changedField.getType() == "com.atlassian.jira.plugin.system.customfieldtypes:textarea" &&

    changedField.getName() == "My Customfield" &&

    !regex.test(changedField.getValue().toString())) {

    getFieldById("customfield_10050").setDescription("Correct");

} else {

    getFieldById("customfield_10050").setDescription("Wrong");

}
2 votes
Sean Chua _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 4, 2024

Hey Rodrigo,

I think it is something to do with your statement for regex, maybe the \\ in 

const regex = /\\d{7,}\\s*\\d{7,}/

or something with the script call to

"com.atlassian.jira.plugin.system.customfieldtypes:textfield"

Nevertheless, I do have another use-case which is similar to yours but our scripts are different, you can check out my sample here : ScriptRunner Jira Cloud - Behaviours (Regex description change) 

0 votes
Kristian Walker _Adaptavist_
Community Champion
March 4, 2024

Hi Rodrigo,

I can confirm that Behaviours in Jira Cloud use Javascript so you can use Javascript Regex in the script.

However, if you are getting the value of a paragraph field, please note this field is returned in Atlassian Document format, so you will need to handle this Syntax in your script.

If you need further assistance, then I would advise you to raise a support ticket here.

I hope this information helps.

Regards,

Kristian

Rodrigo Méndez March 4, 2024

Hi @Kristian Walker _Adaptavist_ ,

I actually solved it with this code. Instead of textfield I changed it to textarea and it worked. I also changed the regex to the below format.

 

const changedField = getChangeField();

const regex = /^(?:(?:\d{7,}\s+){0,2}\d{7,})$/; // Simplified regex

const logs = !regex.test(changedField.getValue().toString())

logger.info(logs.toString())

if (changedField.getType() == "com.atlassian.jira.plugin.system.customfieldtypes:textarea" &&

    changedField.getName() == "My Customfield" &&

    !regex.test(changedField.getValue().toString())) {

    getFieldById("customfield_10050").setDescription("Correct");

} else {

    getFieldById("customfield_10050").setDescription("Wrong");

}
Kristian Walker _Adaptavist_
Community Champion
March 4, 2024

Hi Rodrigo,

I am glad you managed to solve your issue.

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer