0

I'm trying to access a JSON file as a String uploaded as a Zipped Static Resource. But when I try to convert the Blob to string it fails with this exception -

BLOB is not a valid UTF-8 string

Here is my code -

String EmailEditorZipURL = PageReference.forResource('EmailEditor').getUrl();
System.debug('EmailEditorZipURL---'+EmailEditorZipURL);
String baseTemplateJSON = new Pagereference(EmailEditorZipURL+'/Templates/Event_Base_Template.json').getContent().toString();
System.debug('baseTemplateJSON---'+baseTemplateJSON);

Surprisingly, it works fine when executed through anonymous Apex. Any suggestions?

  • This error implies that there is some content in the static resource that is not in UTF-8 format. If its in some other format, for example, base64 encoded, then you might be able to decode it before trying to convert it into String. Is this static resource a open source library that you can point out? – arut Aug 24 '20 at 2:50
  • no it's just a JSON file and uses UTF-8 encoding. I don't ge it though, why does it need to be base64 encoded? I feel like the problem is because it is inside the zip folder but I need it to be bundled together in one resource. – Jitender Padda Aug 24 '20 at 4:28
  • base64 encoded is just one possible example where this kind of error could occur. I didn't specifically mean that the file is base64 encoded. And, I doubt the problem is because of having the file inside a ZIP folder because that is how all static resources are to be placed in SF. So, its just a simple JSON file placed along with other files as a static resource? If yes, then could you provide the JSON file? [I could try this at my end here] – arut Aug 24 '20 at 5:10
  • I'm not sure how reliable the PageReference method is anymore, you may have more consistent results using SOQL instead. Ie [SELECT Id, Body FROM StaticResource WHERE Name = 'file.json'] – Mattisdada Aug 24 '20 at 6:28
  • @Mattisdada It works when I run it through Execute Anonymous and I see the String in the Dev Console so url is fine. And it also works fine if it is just the single file uploaded as a resource but I need it be bundled in a zip folder because I will have multiple files like this. – Jitender Padda Aug 24 '20 at 6:56
2

PageReference.forResource method used in your code gets the reference to the entire static resource (zip folder). In your case, you need to use the overloaded method documented here. Note that the input param path should include the static resource name as well as shown in the code snippet below. If you don't include the static resource name, you'll get resource not found (404) error.

EmailEditorZipURL used in your code has query parameters in it and concatenating it with '/Templates/Event_Base_Template.json' as-is leads to incorrect URL.

You mentioned in your question that the code executes fine anonymously, but I probably wouldn't rely on anonymous execution for specific scenarios (one of them being this case). Reason being I was getting the BLOB is not a valid UTF-8 string error in anonymous execution which is contradicting your observation. Further, I tried to refer to an incorrect nested file name and I kept getting the same error, which leads me to believe that page reference object created had not even found the correct file and was giving misleading errors. You may encounter this in anonymous execution if you tried a different file name.

Following code snippet should be the right way to access the static resource JSON content as string.

PageReference pageRef = PageReference.forResource('EmailEditor', 'EmailEditor/Templates/Event_Base_Template.json');
PageReference contentRef = new PageReference(pageRef.getUrl().split('\\?')[0]);
System.debug('Content---' + contentRef.getContent().toString());

FYI: When I opened the file (you have posted in google drive) using notepad++, it showed up as UTF-8 encoded format. So, I believe there is nothing wrong with the file format, but just the apex code.

  • 1
    Ah okay okay, was missing the use of the overloaded method. I actually did not have to change much in my code except use the overloaded method and not concatenate the url manually. As for the reason you had to use the resource name in your path is probably because inside the zip you have a folder called EmailEditor, worked fine for me without it. But I still cannot explain to you why I was able to get it working in execute anonymous. Thanks for all your help though, cheers! – Jitender Padda Aug 24 '20 at 21:26
  • You are probably right about the folder path. And, don't bother about the anonymous execution > I've seen some weird behaviors using it. – arut Aug 25 '20 at 3:25

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.