This question already has an answer here:

I am trying to use Line breaks (\n) in return statement. But it doesn't work. \n gets printed in the return statement. I am not using any VF Page. I am trying this in my webservice class.

Below is the method structure.

global static string createNewCase()
    return 'FIELDS :' + '' + response.fields + '/\n' +
        'MESSAGE :' + '' + response.message +
        'STATUS CODE :' + '' + response.statuscode;

I tried by adding '/\n' this also did not work. Please help in how to do this. I tried to search for this. But all I get is the example of VF page and controller where escape=False is specified in controller.

I have tried the code mentioned in the I don't know why '\n' new line is not working link for reference and made changes. Still it is not working.

marked as duplicate by Dave Humm, battery.cord, Gortonington, glls, Martin Lezer Nov 13 '17 at 16:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Hi Basti, Thanks for the reply. But this is not the same question that you mentioned. I am not using any VF page. I am trying to return string from a method. – sfdcdev Nov 13 '17 at 13:10
  • 2
    FYI you can remove your concatenations of the empty string ('') as they add nothing. – Adrian Larson Nov 13 '17 at 13:29
  • @AdrianLarson The concatenations were used for adding whitespace. I will remove them. Yes you are right they add nothing. – sfdcdev Nov 13 '17 at 13:40

You may use like following:

String str = 'Welcome home'; 
str += '\n';
str += 'salesforce stackExchange';
return str;

Welcome home 
salesforce stackExchange
  • Hi @Prince. I already tried what you have given. But that is not working. – sfdcdev Nov 14 '17 at 6:03

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