28

I know that we can access static resources in a Visualforce page as follows:

{!URLFOR($Resource.UploadTemplate)} 

How can we access the static resource in an Apex class? And if it's in a zip file, how can I specify the specific file from within the zip as is allowed in Visualforce?

Thanks in advance.

1

5 Answers 5

16

If you want to get some file from zipped static resource, you can use getContent():

StaticResource static_resource = [SELECT Id, SystemModStamp FROM StaticResource WHERE Name = 'My Zip Array' LIMIT 1]; String url_file_ref = '/resource/' + String.valueOf(((DateTime)static_resource.get('SystemModStamp')).getTime()) + '/' + static_resource.get('Name') + '/myfile.json'; PageReference file_ref = new PageReference(url_file_ref); String res_json_body = file_ref.getContent().toString(); 

Please, remember, you can not always use this due to limitation on getContent() (it can not be used in triggers) and latest Salesforce updates.

References:

2
  • 2
    I don't know if it is more recent than this comment. But there is an Apex Method to do that instead of building manually the path: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… PageReference fileRef = PageReference.forResource(staticResourceName, path); String srBody = fileRef.getContent().toString(); Commented Mar 29, 2018 at 19:55
  • 1
    Second link in References gives 404. Please update or remove link. Commented Dec 11, 2018 at 18:38
42

You can simply query them.

StaticResource sr = [SELECT Id, Body FROM StaticResource WHERE Name = 'MyJsonFile' LIMIT 1]; String body = sr.Body.toString(); 

Access Static Resource

2
  • That gives just the URL for resource, not content. Commented May 25, 2020 at 4:58
  • 3
    @zaitsman if you are viewing in DC then it will display as URL but in actual you will get the blob data in apex. Commented May 25, 2020 at 8:12
9

You can also use the PageReference.forResource() method, then add the path/filename onto it.

String myUrl = PageReference.forResource('MyStaticResource').getUrl(); myUrl = myUrl.subString(0, myUrl.indexOf('?')); system.debug(myUrl + '/path/myImage.png'); 

PageReference.forResource('MyStaticResource').getUrl() return a string with the base URL and a number of parameters you don't need, which is why I've used the subString method.

3
  • 2
    Interestingly I have had trouble with PageReference.forResource(name). We are trying to use this like "PageReference.forResource(name).getContent().toString()" to actually resolve the content of a static resource and to get it as a string. This works perfectly in anonymous apex, and worked OK when the code was unpackaged, but fails when we put the code in a managed package and install that on a target org. We get an InvalidParameterValueException with message "Static Resource does not exist". Running the apex in anon apex on that same org works fine too. I don't know why. Permissions? Commented Jul 4, 2018 at 9:33
  • 4
    In a managed package you need to prefix everything like so: PageReference.forResource(prefix + '__' + name).getContent().toString(); Commented Nov 16, 2018 at 13:59
  • This is exactly the issue I'm having. But - adding the namespace doesn't work. Looking in Setup, the Resource doesn't have namespace (as it's not part of the package), but it is Package code that's trying to access it. Any ideas? Commented Mar 10 at 16:59
5

Found out a way to do so recently

PageReference.forResource(resourceName).getUrl() or PageReference.forResource(zipName, path).getUrl() provides the URL of a static resource from Apex code.

Append that with your orgs base url to form a fully usable URL

Example:

URL.getSalesforceBaseUrl().toExternalForm()+''+PageReference.forResource(<Static Resource Name>).getUrl(); 
0

Just do the following (omit the zip file extension in the resourceName):

PageReference.forResource('ZipFile', 'Resource.txt').getContent();

Note that this doesn't work in unit tests as this requires a callout.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.