41 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
Tushar Sharma's 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 ...
Praveen's 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 ...
NSjonas's user avatar
  • 10.1k
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 ...
David Reed's user avatar
  • 92.6k
15 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 ...
kurunve's user avatar
  • 4,289
13 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 ...
Keith C's user avatar
  • 136k
13 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/...
sfdcfox's user avatar
  • 487k
12 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 ...
Saroj Bera's user avatar
  • 14.7k
12 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 = ...
sfdcfox's user avatar
  • 487k
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....
sfdcfox's user avatar
  • 487k
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"&...
Mohith Shrivastava's user avatar
10 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(); ...
sfdcfox's user avatar
  • 487k
9 votes
Accepted

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

I want to thank sfdcfox for his answer. Officially, dynamic resource loading is not supported. But I threw together a hack in case others need a similar solution. Setup Let's say you have a specific ...
Salesforce_Noobie's user avatar
9 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 ...
sfdcfox's user avatar
  • 487k
8 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....
Nick C's user avatar
  • 6,553
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 ...
sfdcfox's user avatar
  • 487k
8 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 ...
Phil W's user avatar
  • 35.6k
8 votes
Accepted

LWC loadScript from static resource with export inside

Components are compiled using something like rollup or webpack; the files delivered to the browser aren't the same scripts you upload or see in your IDE, at least not directly. If you're using a ...
sfdcfox's user avatar
  • 487k
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/...
Imran Qureshi's 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/...
Ant12345's user avatar
  • 543
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 ...
sfdcfox's user avatar
  • 487k
6 votes
Accepted

Summer '16 appends orgId query string to certain static resources

So to answer my own question (thanks @Daniel Ballinger), here's what I've found out through digging; 1) How can I turn this off? I've come up with a work-around, as I couldn't find a switch to ...
Todd Halfpenny's user avatar
6 votes

How can I refer a static resource containing js inside javascript on a visualforce page

You have to include it as you would with any other JS file. <apex:includeScript value="{!URLFOR($Resource.LibraryJS, '/base/subdir/file.js')}"/> https://developer.salesforce.com/docs/atlas.en-...
feddus's user avatar
  • 306
6 votes
Accepted

Spring 17 - Lightning fails to load Static Resources with $Resource in Chrome,Firefox and Edge

As Doug Chasman answered on this question there is an issue with static resources in Spring'17. The issue is being tracked in R&D by W-3615844
Nordine Hammache's user avatar
6 votes
Accepted

static resource image not showing on lightning app(component)

Here's the documentation about the use of static resources. As you can see, you can call your resource like this: <aura:component > <img src="{!$Resource.Icon_one}" height="30" width="30"/&...
Martin Lezer's user avatar
  • 7,295
6 votes
Accepted

Referencing static resource outside Salesforce

Yes, if you configure a Site. Go to Setup > Develop > Sites, create a new Site, and note the URL. Afterwards, you need to go to Setup > Develop > Static Resources, and upload your resource. Make sure ...
sfdcfox's user avatar
  • 487k
6 votes

Accessing Static Resource Contents from Lightning JS Controller

I would like to share an alternative approach by using Fetch API (MDN web docs) The Fetch API provides an interface for fetching resources (including across the network). It will seem familiar ...
Oleh Berehovskyi's user avatar
6 votes
Accepted

ltng require scripts not working for me!

Your scripts from <ltng:require> are not guaranteed to be available in your component's init handler. They are loaded asynchronously. You need to add the attribute afterScriptsLoaded="{!c....
David Reed's user avatar
  • 92.6k
6 votes
Accepted

CSS/Javascript static resources not loading

Your tag seems correct, its your file path that seems to be off, mind sharing your zip file tree structure? Did you zip the files directly, or you zipped a filder that contains these 3 files? if the ...
glls's user avatar
  • 20.1k
6 votes
Accepted

LWC1513: @salesforce/resourceUrl modules only support default imports

Default imports dont use curly braces, therefore, your import would be as follows: import Library from '@salesforce/resourceUrl/CustomLibrary'; this is standard across library imports from the ...
glls's user avatar
  • 20.1k

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