Trim string with ScriptRunner

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

1 vote

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)

Jamie you are genious. It worked perfectly. smile

Thank you so much!

Best.

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

assert "bar" == "foo[bar".replaceAll(/.*\[/, '')

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

0 vote
Vasiliy Zverev Community Champion Sep 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]())

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

Best.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 30, 2018 in Marketplace Apps

Three tips for boosting your board's efficiency with Story Maps

Trello is one of the most effective tools for driving your sprints. It's customizable for every Agile team and product owners and Scrum masters (SM) love it. However, Agile teams often struggle with:...

824 views 2 9
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you