function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chamil MadusankaChamil Madusanka 

access static resource in APEX class

Hi All,

 

We can access static resources in visualforce page as follows;

{!URLFOR($Resource.UploadTemplate)}

 

How can we access the static resource in APEX class?

 

Thanks in Advance!

kiranmutturukiranmutturu

you can't access $resource in apex script.. if you want that file to be used from apex you need to place as a document and use it in apex...

Chamil MadusankaChamil Madusanka

Hi,

 

How to place that file as a document in salesforce?

kiranmutturukiranmutturu

go to all tabs section in that you find the document tab .. click on that and create a new document .....

Chamil MadusankaChamil Madusanka

Hi,

 

How can I access the file which uploaded as a document, in APEX class?

kiranmutturukiranmutturu

you can query the document object

 

 

 lstDoc = [select id,name from Document where name = 'test'];    

CuriousJohnCuriousJohn

The standard technique for accessing Document Tab files in Apex is not working for me.

 

Here's the code I'm using:

 

List<Document> docs;
docs = [select id, name, body from Document 
       where name = 'Donations_Test_Data']; 
 

 

Donations_Test_Data is the "Document Unique Name" (i.e. API name) of a CSV file stored in the Documents Tab. This SOQL query produces zero (0) records of output.

 

The log looks like this:

 

07:41:45.283 (283369000)|SOQL_EXECUTE_BEGIN|[372]|Aggregations:0|select id, name from Document where name = 'Donations_Test_Data_XXX'
07:41:45.292 (292463000)|SOQL_EXECUTE_END|[372]|Rows:0

 

I need to be able to access this test data programmatically in order to get test coverage.

 

 

 

 

Tsunami SailorTsunami Sailor

Static Resources aren't stored in the Documents object, they are in the StaticResource object.  Just change your code to this:

 

List<StaticResource> docs;
docs = [select id, name, body from StaticResource 
       where name = 'Donations_Test_Data']; 
narendra wadkarnarendra wadkar

@Tsunami Sailor 

How can find data inside zip (Content type application/x-zip-compressed )  as static resource?

Emmanuel BRUNO 1Emmanuel BRUNO 1
Sure you can :
PageReference.forResource('UploadTemplate').getUrl();

 
JaanuJaanu
1. I have created a Json file and uploaded into static resources in salesfoce. The file name is "xyz"
2. Converted the json file to apex using - http://json2apex.herokuapp.com/.
3. Copied the class from step 2 into the main class "SandboxPostCopy". 
4. public static JSON2Apex parse(String $staticresource.xyz) { return (JSON2Apex) System.JSON.deserialize($staticresource.xyz, JSON2Apex.class);

I am not sure step 4 coding is correct or not (I am trying to access the xyz static resource json file) .. once I read this file .. I want to update the custom settings based on the json file values.. please guide me with the coding... how to read and how to access the file xyz. Thanks.
 
Arindam Laik 16Arindam Laik 16
StaticResource sr = [SELECT Id, Body FROM StaticResource WHERE Name = 'Test_Resource' LIMIT 1];
String strBody = sr.Body.toString();