25

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

  {!URLFOR($Resource.UploadTemplate)}

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

1

5 Answers 5

15

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

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.
    – zaitsman
    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. May 25, 2020 at 8:12
8

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.

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

Not the answer you're looking for? Browse other questions tagged .