Question is how to get the Static Resource's URL in Apex, so below is the function which will return the relative URL (also supports namespace-resources)
Use this in debug logs to verify :
*Please change the "StaticResourceName" with your Static resource name in above line
public class StaticResourceURL { //Pass the resource name public static String GetResourceURL(String resourceName) { //Fetching the resource List resourceList = [SELECT Name, NamespacePrefix, SystemModStamp FROM StaticResource WHERE Name = :resourceName]; //Checking if the result is returned or not if(resourceList.size() == 1) { //Getting namespace String namespace = resourceList[0].NamespacePrefix; //Resource URL return '/resource/' + resourceList[0].SystemModStamp.getTime() + '/' + (namespace != null && namespace != '' ? namespace + '__' : '') + resourceName; } else return ''; } }
Use this in debug logs to verify :
System.debug(':::: ' + StaticResourceURL.GetResourceURL('StaticResourceName')) ;
*Please change the "StaticResourceName" with your Static resource name in above line
Good post Ankit!!!
ReplyDeleteReally helpful.
Cool, thanks Ankit.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteGood stuff. "List resourceList" should be "List <StaticResource> resourceList" though..
ReplyDeleteHi Ankit,
ReplyDeleteReally a great post! Kinda exactly what I was looking for. However, I am stuck with one strange thing and was hoping you or someone else had some solutions.
Here is my code which is invoking the GetResourceURL(), where $1 represents the resource_name in the following code.
{!URLFOR($Resource.resource_name,'images/image_name.jpg')}
CODE
-----
String body1 = body.replaceAll('\\{!URLFOR\\(\\$Resource\\.(.+?),\\\'(.+?)\\\'\\)\\}',GetResourceURL('$1') + '/$2');
If I print the value for $1 within GetResourceURL it prints it, but the SOQL query doesn't return any records :( whereas if instead of $1 if I pass the same hard coded string value it returns the correct result set ...
Any idea on what I should try?
Complete Code
-------------
public String getBody() {
String body = xec.Content__c;
// THIS DOESN'T WORK
String body1 = body.replaceAll('\\{!URLFOR\\(\\$Resource\\.(.+?),\\\'(.+?)\\\'\\)\\}',GetResourceURL('$1') + '/$2');
// THIS WORKS
String body1 = body.replaceAll('\\{!URLFOR\\(\\$Resource\\.(.+?),\\\'(.+?)\\\'\\)\\}',GetResourceURL('resource_name') + '/$2');
return body1;
}
public String GetResourceURL(String rn)
{
//Fetching the resource
List resourceList = [SELECT Name, NamespacePrefix, SystemModStamp FROM StaticResource WHERE Name = :rn];
//Checking if the result is returned or not
if(resourceList.size() == 1)
{
//Getting namespace
String namespace = resourceList[0].NamespacePrefix;
//Resource URL
return '/resource/' + resourceList[0].SystemModStamp.getTime() + '/' + (namespace != null && namespace != '' ? namespace + '__' : '') + rn;
}
else return '';
}
Excellent Work. This is really an amazing website. I like that
ReplyDeleteDestinations worldwide
Great!!!! Thanks a lot for this solution
ReplyDelete