Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
1  
You want to access zip static resource in VF page ? If it is a zip then use like {!URLFOR($Resource.StyleZip, 'basic.css')} developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/… – Ratan Feb 2 at 12:41
up vote 3 down vote accepted

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:

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.