+ Start a Discussion

Using variables in URLFOR function


Simple question here.

I have a related list- onclick javascript custom button and I want to use the urlfor function to view the record selected in the related list (after it performs some operations).

Problem is, I can't seem to pass the Id of the record I want to view, because it seems like the urlfor function will only accept merge fields.  Is there any way to get around this?

I have a variable containing the Id I want to open, how can I get it into the urlfor function?

All of the functions supported in sControls and Buttons and Links -- such as URLFOR() and IF() -- are resolved on the server.  That means that they are already resolved when your javascript on the client starts to run.  That's why they don't recognize your javascript variables.  There is no way to change that behavior.  In your case, you could use the URLFOR() to build the base of the URL and then manipulate it in javascript to add in the ID query parm.

That makes total sense, but unfortunately I have no idea how to go about executing that.  Is there some resource someone could reccomend or could someone just point me in right direction of how to start?

How are the urls of records constructed?  What should be the building block using the Urlfor command?  If you want to view a record, the command won't work w/o a id mergefield.

Anything helps! :)



I may have done something similar to what you are trying. Just to make sure I understand, your Javascript contains a URLFOR mergefield and you want to add the record id as an argument at run time. I did this:

href = "{!URLFOR($SControl.My_SControl)}" + "&eid=" + recordID

This is the run time equivalent of "{!URLFOR($SControl.My_SControl, recordID)}"

This approach runs the risk of Salesforce changing or eliminating "eid" as the entity identifier. I'm betting that they'll have better alternatives to solve problems like this by then.

Neha JainNeha Jain



Can you please explain how you have used URLFOR on click of a custom button, i mean directly on JS.




My example above comes from an S-Control that gets the ID of a related custom object, then passes that ID to another S-Control. The first S-Control uses URLFOR to generate the URL for the next S-Control. Note that Salesforce is deprecating S-Controls in favor of VisualForce, which also supports URLFOR.


When you put the URLFOR function in any Javascript served by Salesforce, such as a custom button with "OnClick JavaScript" content source, the Salesforce server replaces the merge field containing URLFOR with the actual URL requested in the URLFOR function arguments. When the browser gets the page, it sees the actual URL, not the URLFOR function that generated it.


Please reply if that didn't answer your question.