Forums

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

Need to use regex validation on Scriptrunner for a label custom field validation

Vincent Leduby August 4, 2021

Hello dear community,

I would like to create Script Validator [ScriptRunner]' Validator for "Create" transition that will check the value filled in a custom filed "Other Devices" which is LABEL custom field,  when creating a new ticket.

I have used a regular expression but it seems not working:
I have tested using this code:
cfValues["Other Devices"] == "D1|E2|V2H|H2|M2|M2-N|D3|E3|H3|H3-N|M3-W|M3-N|V3M|V3H|V3U|V4M|S4|V4H|Other|P926x"


It didn't work then I tried : cfValues["Other Devices"] == 'D1' and didn't work too.

 

image.png

I would like that my custom field accepts only this values: D1|E2|V2H|H2|M2|M2-N|D3|E3|H3|H3-N|M3-W|M3-N|V3M|V3H|V3U|V4M|S4|V4H|Other|P926x

Please, could you help me on this issue?

Best regards,

1 answer

1 accepted

0 votes
Answer accepted
Vikrant Yadav
Community Champion
August 4, 2021

Hi @Vincent Leduby  Try below Code, it should work :- 

 

Apply this is Simple Scripted Validator

 

cfValues['Other Devices'] ==~ /^(D1|E2|V2H|H2|M2|M2-N|D3|E3|H3|H3-N|M3-W|M3-N|V3M|V3H|V3U|V4M|S4|V4H|Other|P926x)*$/

Let me know the result

Thanks

Vikrant Yadav

Vincent Leduby August 4, 2021

Hello @Vikrant Yadav 

Thank you for your help.

After adding your script, I did a test by adding D1 value to the custom field "Other devices" but the error message is shown.

 

I have added some screenshots to my answer.

Thank you again for help.

Custom field.PNGerror.PNGscript validator.PNG 

Vikrant Yadav
Community Champion
August 4, 2021

Hi @Vincent Leduby  Little change in script,

Tested on my side, it's working fine. Give it a try.

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.label.Label

def customFieldManager = ComponentAccessor.getCustomFieldManager()

int otherdevicesId = 35851 //replace with custom field id
def other_devicesField = customFieldManager.getCustomFieldObject(otherdevicesId)
def devicesFieldValue = issue.getCustomFieldValue(other_devicesField)

devicesFieldValue.each { label ->
if (!label.toString().matches("(D1|E2|V2H|H2|M2|M2-N|D3|E3|H3|H3-N|M3-W|M3-N|V3M|V3H|V3U|V4M|S4|V4H|Other|P926x)")){
throw new InvalidInputException( "Kindly use correct value")
}
}

 Let me know the result. Hope it works for you!

Label Regex Validator.PNG 

Thanks

Vikrant Yadav

Vincent Leduby August 4, 2021

Hello @Vikrant Yadav ,

Thank you very very much for your help.

It works like a charm.

I really appreciate your help!

Best regards,

Vikrant Yadav
Community Champion
August 4, 2021

Great :)

Glad to hear it works for you!

Kindly accept the answer if solution works for you so that it helps others users having same query.

Stay Safe!

Vincent Leduby August 4, 2021

Done, thank you very much for your help!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events