May 15, 2012

Static Resource URL in Apex

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)


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

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Good stuff. "List resourceList" should be "List <StaticResource> resourceList" though..

    ReplyDelete
  3. Hi Ankit,

    Really 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 '';
    }

    ReplyDelete
  4. Excellent Work. This is really an amazing website. I like that
    Destinations worldwide

    ReplyDelete
  5. Great!!!! Thanks a lot for this solution

    ReplyDelete