0

In Summer 17 Release (v.40) Salesforce provided a way to retrieve URL for item inside Static Resource within Apex which was very required feature and which was asked many times (like in this question).

I have a question, is there any viable way to get URL in Apex for standard salesforce SLDS asset?

In Visualforce it is possible to use something like {!URLFOR($Asset.SLDS, 'assets/images/profile_avatar_96.png')} but I haven't found any way to do this in Apex.

It is not possible to query that object since this is not Asset or ContentAsset object instance.

If only I knew the URL I could use it to fetch the content like new PageReference(url_file_ref).getContent().toString();

I could achieve this if I download SLDS as resource and upload it as static resource but since it is provided like some kind of assets I would like to avoid this.

4
  • Have you tried /sfsites/c/file-asset/{name}? In Communities I used {communityName}/s/sfsites/c/file-asset/{name} Commented Dec 18, 2019 at 14:14
  • @ytiq I am using PageReference sprite = new PageReference('/_slds/icons/standard-sprite/svg/symbols.svg'); - however, this might be fragile if Salesforce moves those files Commented Dec 18, 2019 at 14:15
  • then try uploading it asset and then using PageReference sprite = new PageReference('/sfsites/c/file-asset/symbols'); Commented Dec 18, 2019 at 14:23
  • I don't want to upload anything since it is already available in Visualforce Commented Dec 18, 2019 at 14:59

1 Answer 1

2

I have found the following way to achieve this. I have created a Visualforce page Urlfor

<apex:page controller="UrlforController" showHeader="false" sidebar="false" standardStylesheets="false"> <apex:form > <apex:dynamicComponent componentValue="{!c}"/> </apex:form> </apex:page> 

and a class UrlforController

public inherited sharing class UrlforController { private static final String START = 'UrlforStart('; private static final String FINISH = ')UrlforFinish'; public UrlforController() { text = ApexPages.currentPage().getParameters().get('text'); System.debug(LoggingLevel.ERROR, 'text ' + text); } public String text { get; set; } public Component.Apex.OutputText getC() { Component.Apex.OutputText outputText = new Component.Apex.OutputText(); System.debug(LoggingLevel.ERROR, 'text ' + text); outputText.expressions.value = START + text + FINISH; System.debug(LoggingLevel.ERROR, 'outputText.value ' + outputText.value); return outputText; } public static String get(String expression) { PageReference r = Page.Urlfor; r.getParameters().put('text', expression); System.debug(LoggingLevel.ERROR, 'r.getContent().toString() ' + r.getContent().toString().substringBetween(START, FINISH)); } } 

Then I use UrlforController.get('{!URLFOR($Asset.SLDS,\'/assets/icons/utility-sprite/svg/symbols.svg#announcement\')}') to get the actual URL /_slds/icons/utility-sprite/svg/symbols.svg#announcement

2
  • An interesting "hack" :) Commented Dec 18, 2019 at 15:58
  • 1
    @PhilW this is reuse of old hack used to get translated text of label in Apex code Commented Dec 19, 2019 at 9:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.