35 votes

accessing static resource in apex

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
user avatar
21 votes
Accepted

How do I call functions loaded from a Static Resource JavaScript file?

you could attach your utility methods to the global window object(SecureWindow) and use it in your controller. Static Resource: TestScript (function(w){ "use strict"; //Optional because LC on ...
user avatar
  • 10.1k
19 votes
Accepted

Custom Metadata vs. Custom Objects in Managed Package

Here are some "advantages" vs using a Custom Object: Can be Deployed Can use Field Definition or Entity Particle to create references to Custom Object Metadata Retrieve doesn't cost against ...
user avatar
  • 9,537
16 votes

Custom Metadata vs. Custom Objects in Managed Package

A reason I haven't seen articulated yet applies specifically to the managed package context: Packaged, protected Custom Metadata Types are an ideal solution for secret storage in a managed package ...
user avatar
  • 87k
15 votes
Accepted

When to use Static Resource vs Documents

Typically, static resources is used for files that will need to be transferred between orgs. Often, images are put here that are used within code, Javascript, CSS, or documents that code might ...
user avatar
  • 6,029
13 votes
Accepted

Salesforce1 referencing a static resource from another static resource

The paths can't be concatenated because Salesforce1 adds ?isdtp=p1 to all generated URLs. To solve this problem, the URLFOR function must used properly instead of concatenating the different parts. I ...
user avatar
13 votes
Accepted

Access Custom Setting global variable from Static Resources

Static resources are... static. There is no pre or post processing of their contents. Instead of putting globals from Visualforce code in your javascript, try putting some JavaScript in your ...
user avatar
12 votes
Accepted

accessing static resource in apex

If you want to get some file from zipped static resource, you can use getContent(): StaticResource static_resource = [SELECT Id, SystemModStamp FROM StaticResource ...
user avatar
  • 4,169
12 votes
Accepted

What is recommended way to unit test JSON Parser based on JSON.deserializeUntyped calls?

If the JSON is reasonably small then I would build it in the unit tests. That allows you to generate various permutations and always have the values to hand to assert against. The best way to do that ...
user avatar
  • 130k
12 votes
Accepted

Is the 5 MB static resource size limit 5,242,880 bytes or 5,000,000 bytes?

It's 5,000,000 bytes. In Salesforce, all storage units are per SI units, where megabyte means 1,000,000, and mebibyte means 1,048,576. Nowhere in Salesforce are the SI units for kibibyte/mebibyte/...
user avatar
  • 434k
11 votes
Accepted

which zip archive format (.rar or .zip) works with static resources without any issues?

The only accepted formats are .jar and .zip You can package a collection of related files into a directory hierarchy and upload that hierarchy as a .zip or .jar archive. https://www.salesforce.com/...
user avatar
11 votes

How can I dynamically load a static resource image in LWC?

You would normally upload a ZIP file, and use it in the manner described in the docs: import TRAILHEAD_CHARACTERS from '@salesforce/resourceUrl/trailhead_characters'; // ... einsteinUrl = ...
user avatar
  • 434k
10 votes

Is it possible to access static resource of a managed package in a local VF page

Yes it is possible; the name needs qualifying using the managed package namespace prefix. So if that namespace prefix is "abc", this will work: <apex:image value="{!$Resource.abc__LogoImage}"/> ...
user avatar
  • 130k
10 votes

Wrong use of !URLFOR inside a controller of aura component

See $Resource. To use $Resource in JavaScript, you have to actually call a method. Here's an example from the docs: ({ profileUrl: function(component) { var profUrl = $A.get('$Resource....
user avatar
  • 434k
10 votes
Accepted

Call Javascript function on visualforce page from static resource?

Its definitely doable .You will need to use window global object to resolve all merge fields and then use the object in your static resource . Here is a simple hello world example <html lang="en"&...
user avatar
10 votes
Accepted

Static resource image in formula field

This example displays the image regardless of the value in any field. IMAGE('/resource/low', "low", 64, 64) This example displays an image of a green square if the amount of an opportunity is ...
user avatar
9 votes
Accepted

Update StaticResource via APEX ToolingAPI

One option is to use the Rest version of the ToolingAPI to create the StaticResource: string name = 'testjs'; string contentType = 'text/javascript'; string body = 'alert("Hello World");'; // ...
user avatar
9 votes

Accessing Non-Static Variables in Static Methods of a Visualforce Controller

The short answer is quite simply, you cannot get to the instance of the Apex class that was invoked on page load. It is gone. You are bumping up against one of the architectural peculiarities of ...
user avatar
  • 19.1k
9 votes
Accepted

Accessing Static Resource Contents from Lightning JS Controller

You can get the data with a simple HTTP request: ({ init: function(component, event, helper) { var path = $A.get("$Resource.StaticResourceName"); var req = new XMLHttpRequest(); ...
user avatar
  • 434k
8 votes
Accepted

Read text file as a static resource and display in a VF page?

The StaticResource object is readable via a normal SOQL query. Add a property to your controller that queries the static resource and returns its body assuming the body is text: public String ...
user avatar
  • 130k
8 votes
Accepted

Create static resource with apex code

You will have to go with MetaData API. Something as below - MetadataService.MetadataPort service = createService(); MetadataService.StaticResource staticResource = new MetadataService....
user avatar
  • 3,840
8 votes
Accepted

Hard-coded resource path vs $Resource global variable?

$Resource has two benefits: cache busting and automatic path resolution. By cache busting, we're referring to the fact that the /resource/name has a very long expiry time, and so you'll find it harder ...
user avatar
  • 434k
7 votes
Accepted

AJAX load JSON from static resource

I've actually kind of figured it out. There is some issue with my use of $.getJSON, however $.ajax works fine. I'll explore why .getJSON wasn't working.. Either way, I've moved my code back into the ...
user avatar
7 votes

accessing static resource in apex

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....
user avatar
  • 5,489
7 votes
Accepted

Font-Awesome fonts are not loading after Spring'17 release

I found the solution and changed the URL for the font files from this: @font-face { font-family: 'FontAwesome'; src: url('../fonts/fontawesome-webfont.eot?v=4.2.0'); src: url('../fonts/...
user avatar
7 votes
Accepted

Include custom font in lightning components

Uploading a .ttf file to my static resources and including it as follows worked for me: .THIS .boldFont { font-family: GothamBold; } @font-face { font-family: GothamBold; src: url(/resource/...
user avatar
  • 523
7 votes

Refer static resource in custom HTML email template

To refer to a static resource in a template dynamically, you'll need to use Visualforce templates and use a controller of some sort. Here's an example that uses Document: public class ...
user avatar
  • 434k
7 votes

JSON files in Static resource lightning web component

This is actually surprisingly easy to achieve using standard JavaScript functionality that is supported by the Salesforce Locker Service; the XMLHttpRequest. You can check what is supported by opening ...
user avatar
  • 27.6k
7 votes

How to Import lodash into org?

What exactly should I download? https://raw.githubusercontent.com/lodash/lodash/4.17.15-npm/core.min.js Or https://raw.githubusercontent.com/lodash/lodash/4.17.15-npm/lodash.min.js Depending on what ...
user avatar
  • 434k
6 votes
Accepted

Reference Static Resource in Zip File

Check this documentation https://www.salesforce.com/us/developer/docs/pages/Content/pages_resources_reference.htm To reference a file in an archive, use the URLFOR function. Specify the static ...
user avatar
  • 16.5k

Only top scored, non community-wiki answers of a minimum length are eligible