+ Start a Discussion
Scott.MScott.M 

Using URLFOR in apex with dynamic components

Does anyone know of an equivalent to URLFOR in Apex. We now have dynamic components that can be initialized in apex. For example if you wanted to initialize the stylesheet component you could do this:

 

Apex.Component.Stylesheet customstyle = new Apex.Component.Stylesheet();

customstyle.value = '/mystyle.css';

 

This is great but I want to initalize the component to use a static resource stored in a zip file. In visualforce that's easy using the URLFOR() function. Is there anyway to acheive the same functionality in Apex?

 

Thanks!

Scott

Best Answer chosen by Admin (Salesforce Developers) 
Laxman RaoLaxman Rao

you can try this:

 

Component.Apex.Stylesheet customstyle = new Component.Apex.Stylesheet();
customstyle.value = URL.getSalesforceBaseUrl().toExternalForm() + '/resource/MobileCSS';
system.debug('customstyle.value is ' + customstyle.value);

 

MobileCSS is the resource name.

If it is inside the folder then use like this

customstyle.value = URL.getSalesforceBaseUrl().toExternalForm() + '/resource/MobileCSS/my.css';

All Answers

Laxman RaoLaxman Rao

you can try this:

 

Component.Apex.Stylesheet customstyle = new Component.Apex.Stylesheet();
customstyle.value = URL.getSalesforceBaseUrl().toExternalForm() + '/resource/MobileCSS';
system.debug('customstyle.value is ' + customstyle.value);

 

MobileCSS is the resource name.

If it is inside the folder then use like this

customstyle.value = URL.getSalesforceBaseUrl().toExternalForm() + '/resource/MobileCSS/my.css';

This was selected as the best answer
Scott.MScott.M

thanks, it actually works with just the relative url /resource/name/path :) 

Laxman RaoLaxman Rao

yes I agree :)

Scott.MScott.M

One thing I am a little worried about is that URLFOR returns a url that has a timestamp in it /resource/1349463045000/name/path. If you don't include the timestamp will it always point to the latest uploaded version? 

 

Thanks! 

Scott

Scott.MScott.M

A quick test makes it look like it points to the latest version, so it should be a decent solution