Save newline in Text field

I’m having trouble saving newline delimited String into Text field via Apex on Salesforce.

String.format('{0} - {1} - {2} - {3} : {4}n', new List<String> {...};

String looks something like this

This – is – a – test : linenThis – is – a – test : linenThis – is – a – test : linen

I need this saved into a Text field and then later I should be able to retrieve and split by ‘n’ to get the lines backs.

I also tried using ‘rn’ but that doesn’t work either.

Please see I want to use newline character for now, will consider possibility of using some other character once I know this is not possible.

Thanks!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The issue you are having is with your target sObject field, declared as Text, length 255. These fields don’t support new line characters. If you think about it for a moment, text fields are used for Account.name, Opportunity.name and there’s no way to enter a new line in such fields on the standard UI.

Fields declared as textArea or textArea (long) do support new line characters

I created a custom object Foo__c in my DE with both Text (len 255) and TextArea custom fields

Using this code:

insert new Foo__c(name='00foo',
              text__c = String.format('{0}n{1}',new List<String>{'Hello','world'}),
              textarea__c = String.format('{0}n{1}',new List<String>{'Hello','world'})
             );

The results are clear – the new line is present in the textArea field but not the Text field:enter image description here

Now, when you use n at the end of the string pattern for format; they are stripped off before being saved in the database:

insert new Foo__c(name='03foo',
              text__c = String.format('{0}n{1}n',new List<String>{'Hello','world'}),
              textarea__c = String.format('{0}n{1}nnnn',new List<String>{'Hello','world'})
             );

system.debug(loggingLevel.INFO,[select text__c, textarea__c from Foo__c where name = '03foo']);


12:49:51.262 (262880325)|USER_DEBUG|[6]|INFO|(Foo__c:{Text__c=Hello world, Id=a094000000P3BW4AAN, TextArea__c=Hello
world})

Method 2

Where you have new list<string>, is where your list of formatting arguments should be located. I suspect that would be where you want to locate your newline character.

However, since you appear to be assembling a string from several substrings, you may want to do it in two steps by assembling the substrings first, then doing the format with the newline character using the something along the lines of the pattern from the Docs as shown in the example below.

From the Apex Code Developer’s Guide – String.format:

public static String format(String stringToFormat, List<String>
formattingArguments)

Parameters:

stringToFormat

Type: String

formattingArguments

Type: List

Return Value Type: String

Example

String placeholder = 'Hello {0}, {1} is cool!';
List<String> fillers = new String[]{'Jason','Apex'};
String formatted = String.format(placeholder, fillers);
System.assertEquals('Hello Jason, Apex is cool!', formatted);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x