Trim string with ScriptRunner

Jose Jimenez September 13, 2016

Hi guys, hope you are doing great. I hope someone can help me with the following:

I have a text custom field (single line) that stores values like 'xyz@something.com'.

I need to trim the string before the '@' and copy that value in another text field (vía post-function).

So, if the original value is 'xyz@something.com', I need to store the value 'xyz' in another text field.

Thank you in advance for your help.

Best.

2 answers

1 accepted

2 votes
Answer accepted
JamieA
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.
September 13, 2016

You want something like this... completely untested. Put this function first in the list of post-functions.

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def emailCf = customFieldManager.getCustomFieldObjectByName("Email") 
def mailboxCf = customFieldManager.getCustomFieldObjectByName("Mailbox") 

def mailBox = (issue.getCustomFieldValue(emailCf) as String)?.replaceAll(/@.*/, '')
issue.setCustomFieldValue(mailboxCf, mailBox)
Jose Jimenez September 14, 2016

Jamie you are genious. It worked perfectly. smile

Thank you so much!

Best.

Tomas Arguinzones Yahoo February 16, 2017

Hi @Jamie Echlin [Adaptavist], do you know how to accomplish something similar but lets assume that instead of  "@" I have "["....I am trying as /[.*  but it is throwing an error.

 

Thanks

JamieA
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.
February 21, 2017
assert "bar" == "foo[bar".replaceAll(/.*\[/, '')
Tomas Arguinzones Yahoo March 3, 2017

Thank you @Jamie Echlin [Adaptavist]...it worked.

Tamil June 25, 2020

@JamieA 

I want to copy issue key(e.g DUP-35) to a custom field without character(-)e.g DUP35.

I have created a scripted field and got value of issue key copied to custom field, but when i try to use .replaceAll(-) function, it is returning value without trimming character(-) (e.g displaying as DUP-35 itself on custom field).

import com.atlassian.jira.issue.Issue
Issue issue=issue
def key=issue.getKey()

return key.replaceAll(/.-*\[/, '')

 

Any thoughts on how to return value without special character(-)

0 votes
Vasiliy Zverev
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.
September 13, 2016

I would reccomend to use split method for String like this:

CustomFieldManager cstFldMng = ComponentAccessor.getCustomFieldManager();
issue.setCustomFieldValue(
    cstFldMng.getCustomFieldObjectByName("where to set")
    ,( (String) issue.getCustomFieldValue(cstFldMng.getCustomFieldObjectByName("from field"))).split("@")[0]())
Jose Jimenez September 14, 2016

Thank you for your time Vasiliy. Unfortunetly it didn't work.

Best.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events