function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
GoForceGoGoForceGo 

Use of '\n' for line feed in Error strings

Since visualForce doesn't have good  error handling for custom controllers, I am  writing some custom code show error messages.

My custom controller is editing a table.

In the code below, I am a trying to insert a line feed after every error using '\n'. 

However,  when error messages are output, there are no separate lines - it is one big error message. i have tried '\r' as well - doesn't work.

Any thoughts.

Code:
       } catch (DmlException e) {
for (Integer i = 0; i < e.getNumDml(); i++) { string[] fields = e.getDmlFields(i); //strip __c string field = fields[0].subString(0,fields[0].length()-3); integer row = e.getDmlIndex(i); String rowError = 'Row:' + (row+1) + ' Field:' + field + ':' + e.getDMLMessage(i); if (ErrorString[0] <> null) { ErrorString = ErrorString + '\n' + rowError; } else { ErrorString = 'ERROR:' + rowError + '\n'; } }

 

DevAngelDevAngel
Hi GoForceGo,

Slash n is valid in all strings. I use the slash n all the time.

By the way, your code looks suspect in that there is no declaration for ErrorString and you evaluation of ErrorString[0]


Cheers
GoForceGoGoForceGo
'\n' doesn't seem to work for me. I get one large error string instead of desired line feed between errors.

ErrorString[0] - that issue doesn't exist in my code - I used an array of ErrorString - so my code internally always says ErrroString[0]. I had just cleaned up the code for this posting so as to not confuse the issue.

 I have  noticed the <> as well. Seems like it does the same thing as !=.


J KeenerJ Keener
Assuming you're displaying the string on a page, the /n doesn't process as a new line in html .  I use the following functions in a number of places to convert new line characters (/n) to <BR>.
Code:
public class String_Functions {

  public static String convertCRtoBR(String StringIn) {
    String result = '';

    if ((StringIn != null) && (StringIn != ''))
      {    
      List<String> StringLines = StringIn.split('\n',0);
  
      for (String StringLine : StringLines) 
        {
        if ((result != '') && (StringLine != null)) {result += '<BR>';}
        if (StringLine != null) {result += StringLine;}
        }
      }  
    return result;
  }

  public static String convertBRtoCR(String StringIn) {
    String result = '';
    
    if ((StringIn != null) && (StringIn != ''))
      {    
      List<String> StringLines = StringIn.split('<BR>',0);
  
      for (String StringLine : StringLines) 
        {
        if ((result != '') && (StringLine != null)) {result += '\n';}
        if (StringLine != null) {result += StringLine;}
        }
      }  
    return result;
  }


//******************
//Test Method 
//******************
  static testMethod void TestMe() {

    String result = '';
    
    result = convertCRtoBR('Test1\nTest2\nTest3');
    System.assertEquals('Test1<BR>Test2<BR>Test3',result);

    result = convertBRtoCR('Test1<BR>Test2<BR>Test3');
    System.assertEquals('Test1\nTest2\nTest3',result);
  }

//The End
}

 
 
GoForceGoGoForceGo
Thanks for the post.

I was hoping it would do it...

I get the string <br> in the output instead of line break. When I put \n, the system strips it out and I get no breaks

Perhaps it has something to do with the visualforce processing.

I am using the errorstring in the following fashion in a VisualForce page:

            <apex:outputPanel   rendered="{!errorOccured1}">
                 <apex:outputText value="{!ErrorString1}" />
            </apex:outputPanel>




J KeenerJ Keener

On your OutputText, add an escape="false" attribute, so the <BR> will render as html correctly.

Jon



Message Edited by J Keener on 05-13-2008 12:54 PM

Message Edited by J Keener on 05-13-2008 12:55 PM
GoForceGoGoForceGo
Interestingly, I tried that just a few minutes ago...didn't work either...


awilawil

For some reason, the following code will not properly escape the contents:

 

 

<apex:outputText escape="false">
{!someTextWithBRTags}
</apex:outputText>

 But the following code will escape contents:

 

<apex:outputText escape="false" value="{!someTextWithBRTags}"/>

 

 

 

 

 

VinnySusanthVinnySusanth

Hi,

 

The following worked fine for me.

 

The String was created in the Controller class with a combination of '\n<br/>'.

(Something like String str="This is line 1"+"\n<br/>"+"This is line2";)

 

I tried with this escape sequence as using only \n or only <br/> did not help me.

 

Setting the escape as false in Visualforce page is also needed for this to display correctly.

<apex:outputText value="{!str}" escape="false/>

 

 

Regards,

Vineetha

Joseph Mckenzie 3Joseph Mckenzie 3
"\n" works for me when entering data to a chatter feed as where String.fromCharCode(13) only works in lightning but "\n" works in classic and lightning 😎