Forums

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

Create a custom field based on part of the string of another

Derek_Fage March 20, 2018

Hi there,

I've spent a while searching for an answer on how I can create a scripted custom field based on another or copy one or add two together and am happy with all of that using scriptrunner

What I need to do is to is to have a scripted custom field where I change the last character for use in another custom field that does a database lookup.

I have a custom field that contains (for example) "12345A" or "123A". 

What I want to do is to create another custom field that will replace the last letter in the string with "%" so that I end up with "12345%" or "123%"

Can anybody help me with a script fragment that that might allow me to say the equivalent of def newfield = left(oldfield,len(oldfield)-1 or similar

Thanks

Derek...

1 answer

1 accepted

1 vote
Answer accepted
Knut Arne Ristebråten
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 20, 2018

Hi, I got the following code working.

It's based on regex match so you can adapt it to suit your needs for the replacing. Example here takes the last ($) character ([A-Z]) and replace it with a %.

Hope this works for you.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def cfField = customFieldManager.getCustomFieldObject("customfield_10306")
def cfValue = issue.getCustomFieldValue(cfField)
def replacedValue = cfValue.toString().replaceAll(/[A-Z]$/, '%')

return replacedValue
Derek_Fage March 21, 2018

That's excellent - should have thought of regex

Have included in my script and it does all I wanted!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events