4

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 : line\nThis - is - a - test : line\nThis - is - a - test : line\n

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 '\r\n' 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!

  • 1
    what datatype field that you use?is to store the string?Is it Textarea(Long) ? – unidha May 5 '15 at 8:02
  • It's Text field with length 255. – jagmohan May 5 '15 at 8:15
8

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}\n\n\n\n',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})
  • "Now, when you use \n at the end of the string pattern for format; they are stripped off before being saved in the database" - That's what I've been overlooking! Important point. – Jason Clark Apr 24 '18 at 13:57
1

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);
  • n.b. you might want to revise your statement regarding new List as an argument; it works fine. – cropredy May 5 '15 at 19:54
  • Thanks. I didn't have time to test so didn't make an absolute statement. My "gut" was also telling me something along the lines of your answer, but couldn't quickly find a reference to support it, although I thought I'd seen it written somewhere in earlier docs. – crmprogdev May 6 '15 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.