I know that we can access static resources in visualforce page as follows;


How can we access the static resource in APEX class? And if it is in a zip file, how can I specify the specific file from within the zip as it is allowed in visualforce ?

Thanks in advance


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.


  • 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(); – Etienne Rocheleau Mar 29 '18 at 19:55
  • Second link in References gives 404. Please update or remove link. – devilpreet Dec 11 '18 at 18:38

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

  • That gives just the URL for resource, not content. – zaitsman May 25 '20 at 4:58
  • 1
    @zaitsman if you are viewing in DC then it will display as URL but in actual you will get the blob data in apex. – Tushar Sharma May 25 '20 at 8:12

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.

  • 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? – Phil W Jul 4 '18 at 9:33
  • 1
    In a managed package you need to prefix everything like so: PageReference.forResource(prefix + '__' + name).getContent().toString(); – Semmel Nov 16 '18 at 13:59

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


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

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.