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
OdedHarnivOdedHarniv 

Rich Text field updated without formatting via the REST API

Hello everyone

 

I have a follow up issue for my previous problem (http://boards.developerforce.com/t5/REST-API-Integration/Updating-records-from-REST-API/m-p/291815#M585).

 

I have this code from my JAVA application running on Google app engine:

 

 

HTTPRequest httpRequest = new HTTPRequest(url, HTTPMethod.POST, FetchOptions.Builder.withDeadline(10));

...

JSONObject update = new JSONObject();
update.put("my_txt", mTxt);			
			httpRequest.setPayload(update.toString().getBytes());

log.severe("payload: " + update.toString());		
			
HTTPResponse response = urlFetchService.fetch(httpRequest);

 

 

When I print out the text before sending it I see the spaces, new lines and format are kept and appear OK.

 

But when I look in my Force.com Org in the relevant Rich Text field I see on long line of text (with not spaces, new lines or any formatting).

 

Any advice on why is that happening and what am I doing wrong?

 

Thanks

 

Oded

 

Best Answer chosen by Admin (Salesforce Developers) 
dkadordkador

You might be able to find some library out there, but it seems like you could just do a replaceAll on your HTML string.  Something like:

 

 

String myStr = "...";
myStr = myStr.replaceAll("\n", "<br/>");
myStr = myStr.replaceAll("\t", "&nbsp;");

 Right?

 

All Answers

dkadordkador

RTA fields use HTML for formatting, so you must send corresponding HTML.  For example, if you want to insert a new custom object with a field called "rta__c", you could send JSON that looks like:

 

 

{
  "name": "blah",
  "rta__c" : "a<br/>c"
}

 

Note the <br/> tag.

 

OdedHarnivOdedHarniv

Thanks

 

Is there any way to reformat the text as HTML (I'm using JAVA) or I need to replace the relevant chars with html tags manual?

dkadordkador

You might be able to find some library out there, but it seems like you could just do a replaceAll on your HTML string.  Something like:

 

 

String myStr = "...";
myStr = myStr.replaceAll("\n", "<br/>");
myStr = myStr.replaceAll("\t", "&nbsp;");

 Right?

 

This was selected as the best answer
OdedHarnivOdedHarniv

Thank you very much

 

I was hoping to maybe find something on the APEX side but didn't find any.

 

Are you aware of something?

 

Oded

dkadordkador

If you wanted you could build your own API with Apex REST that does the string substitution.  It's a new pilot feature in the latest release.

 

But there's no built-in API that will do this for you.