Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Replace a String with an line break in multi line field

Tobias December 13, 2021

Hello together, 

we are currently facing a problem in our internal Jira environment. We receive data from an external system and they are not possible to send it with line breaks via the API. The problem here is that we need every new data set in a new line because we have some automation scripts reading this line break. 

So now my question is: How can i replace a String with a line break? Maybe this is something I could implement via ScriptRunner. 

The current format is this: 

Hello\nthis\nis\na\test

And the expected format would be:

Hello
this
is
a
test 

 Are there any possibilities to change the data in Jira internally. 

Thanks for now, have a nice weekend. 

Best regards!

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 20, 2021

Hi @Tobias 

For your requirement, you could try something like this on the ScriptRunner Console:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issue = issueManager.getIssueByCurrentKey('MOCK-1') as MutableIssue
def description = issue.description.replace("\\n", "\n")
issue.setDescription(description)
issueManager.updateIssue(loggedInUser,issue, EventDispatchOption.DO_NOT_DISPATCH,false)

Please note, the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

The main point is on this line:-

def description = issue.description.replace("\\n", "\n")

You will need to use the replace method to convert the \n parameter in your String which will be read as \\n to \n to enable the line break.

Below is a print screen of the ScriptRunner Console configuration:-

script_console.png

Below are a few test screens:-

1) This is how the text in the Description field is displayed initially:-

image1.png

2) This is how the text in the Description field is after running the code above:-

image2.png

I hope this helps to answer your question. :)

 

Thank you and Kind Regards,

Ram 

Tobias January 11, 2022

Hi @Ram Kumar Aravindakshan _Adaptavist_
thanks for providing this solution it works really well. How to implement this in Jira? I think best would be to trigger it in a transition or what do you think? The best way would be after creation, but I think this could cause some problems. 

Best Regards,

Tobias

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 11, 2022

Hi @Tobias

You can either do it during a Transition, i.e. using the Post-Function or even using the Listener to answer your question.

If you want to do it using the Post-Function, you could even use the Create transition, i.e. once the issue is created, the text will be re-formatted. This shouldn't cause any issues.

Thank you and Kind Regards,

Ram

Like Tobias likes this
TAGS
AUG Leaders

Atlassian Community Events