5

I'm running into a problem on a specific customer's org when attempting to get the HTML from a custom object's standard 'new' page layout.

String objPrefix = My_Object__c.SObjectType.getDescribe().getKeyPrefix();
PageReference page = new PageReference('/' + objPrefix + '/e?nooverride=1');
Blob b = page.getContent();
String html = b.toString();

Using the above code results inBLOB is not a valid UTF-8 string when attempting to use the toString() method.

This code is running in a managed package and has worked successfully in many other orgs.

I know that Blob.toString() method is only supported for UTF-8 encoded strings, but shouldn't all VisualForce pages be encoded as UTF-8?

  • What context is the Apex running in? Is it in a VF controller? – Mike Chale Jan 3 '14 at 20:14
  • Yes, I'm using a custom VF controller. – csnullptr Jan 3 '14 at 20:16
  • Is their 'new' page layout a VF override? If so, have they tried with a standard page layout? – Mike Chale Jan 3 '14 at 20:20
  • @csnullptr What server is this running on? I think AP and/or EMEA servers do not render pages in UTF-8. – sfdcfox Jan 3 '14 at 20:28
  • @MikeChale We are doing urlhacking from withing a managed package (e.g. force201.wordpress.com/2012/01/22/…). We have overridden the 'new' layout in our package to point to a different page that redirects to the standard new page with query params. – csnullptr Jan 3 '14 at 20:50
3

For shits and giggles you can try demoting it to ASCII before it gets to the page:

b = EncodingUtil.urlEncode(b, 'ASCII');
b = EncodingUtil.urlDecode(b, 'ASCII');

Or something elsewhere in your code is trying to stuff binary data into a string. Can happen in a few cases when trying to output:

  • Document.Body
  • Attachment.Body
  • StaticResource.Body
  • etc

Where's the rest of your class? Nothing trying to dump unescaped outputText into a page with funny contentType? :-)

Worth noting NA0 (ssl.salesforce.com) is a special pod that does ISO-8559-1 only. Worth raising a case to Salesforce if this is inconsistent with behaviour in other pods.

You can determine the character set for your organization by doing a global describe and inspecting the encoding value returned.

|improve this answer|||||
  • While the first part of your answer isn't relevant to my specific question it is a confirmed issue with NA0. – csnullptr Jan 6 '14 at 18:54
  • 1
    Wow, so Salesforce has code that doesn't run on a whole pod? Was there a workaround? – Mike Chale Jan 6 '14 at 20:04
  • According to SFDC the workaround was to have the customer migrate to a non na0 pod. What we ended up doing was disabling this auto-fill feature when a generic exception is thrown by Blob.toString(). – csnullptr Jan 27 '14 at 15:35
  • All Salesforce instances are planned to be migrated off of na0 by the end of Q2 2014. – csnullptr Jan 27 '14 at 15:37
0

I believe this exception would occur only if the page was rendered as PDF. PDF files are binary formatted, and not compatible with UTF-8 strings. Any Visualforce page that renders in HTML or any normal plain text format (text/plain, text/csv, etc) should work with Blob.toString().

|improve this answer|||||
  • But the Apex is pulling the results of the 'new' record page layout; surely that's not rendering as a PDF? – Mike Chale Jan 3 '14 at 20:19
  • As far as I know, all pages in salesforce.com are rendered in UTF-8, so it certainly shouldn't be possible. Either there's a glitch in Blob.toString() or a bug in salesforce.com that's allowing the page to render in some non-UTF-8 compatible manner. This might need the attention of someone at salesforce.com to confirm. What language is the organization running in? Is it on an AP or EMEA server? They might use a different character set. – sfdcfox Jan 3 '14 at 20:23
  • No, it is definitely not rendering as a PDF. – csnullptr Jan 3 '14 at 20:23

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.