function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ShaunCShaunC 

Dynamic reference to Static Resource in Managed Package

I am developing an application which includes a configuration object that allows a user to specify an image they would like displayed on a VF page. The user must type in the filename of the image and include the image in an existing Static Resource zip file. The visual VF page then retrieves the correct image as below:

<apex:image url="{!URLFOR($Resource.Logos, Logo)}"/>

The Controller Extension has a method getLogo() which returns the appropriate filename.

 

This is all very nice, however it won't work in a Managed Package because the end user won't be able to update the SR zip file. It will be locked.

 

Does anybody have a workaround for this. Or an idea of how I might be able to achieve the same result differently?

 

Many thanks.

ShaunCShaunC

I found moving to Documents rather than Static Resources solved the problem. Now the user will be able to upload their own images as documents and the VF page will display the appropriate image. See code snippet from VisualForce page and Controller

 

 

    public String getLogo() {
    	if (theFormat != null && theFormat.Logo__c != null && theFormat.Logo__c.length() > 0) {
	    	List<Document> docs = [ SELECT Name, Id From Document WHERE Name = :theFormat.Logo__c LIMIT 1 ];
	    	if (docs.size() > 0) {
	    		Document doc = docs[0];
		    	string imageid = doc.id;
		    	imageid = imageid.substring(0,15);
		    	return '<img src="/servlet/servlet.FileDownload?file=' + imageid + '"/>';
	    	}
    	}
    	
    	return '';
    }

 

        <apex:outputText value="{!Logo}" escape="false" />

 

 

 

 

 

A_SmithA_Smith

I don't have the URL handy, but you can refer to a static resource via URL instead of using the $Resource syntax.  Just inspect the URL to your resource and you'll see the structure.  Strip out the random number in the middle.  They can then enter the resource into your config and then you can construct a URL using it.  

 

Keep in mind if the customer replaces their static resource content, it will take a while for it to be refreshed.  The random number is updated each time they replace the static resource content.  That new number forces the browser to update their images/css stored in that static resource.  Since you'll be removing the number (you won't know the number), then it might take a bit more time for browsers to update the content.  

 

Hope that helps!

 

d3developerd3developer

Is there an easy way for a not particularly proficient Salesforce user to get the url for their recently updated static resource?

ClaiborneClaiborne

The "random number" is actually a timestamp. So you cannot safely hardcode the /resource/<timestamp>/<resourcename> into your code. The timestamp changes every time the resource is updated.

MohaMoha
Hello, i have this problem : i want the customer to upload his own image just like you did, i was reading your snippet of code and i would like to know what theformat is ? what kind of object i need your help if you want to,
thanks