[SalesForce] Pass value from OnClick JavaScript Detail Page Button to a Controller

Good morning.
Is there a way to pass a value from an OnClick Javascript Detail Page Button to a Controller other than the controller that the button is on?

This is my JavaScript:

{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/26.0/apex.js")}

var engID=window.sfdcPage.entityId
var postType="statement of work";   

window.location="https://rsidemo1-dev-ed--c.na24.visual.force.com/apex/VfFilePost?scontrolCaching=1&id="+engID;

I want to assign postType to a variable in the controller of the VfFilePost Page. I am new to Salesforce and programming. Any help is appreciated.

Best Answer

You could pass it as an encoded URL parameter:

{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/26.0/apex.js")}

var engID=window.sfdcPage.entityId
var postType="statement of work";   

window.location="https://rsidemo1-dev-ed--c.na24.visual.force.com/apex/VfFilePost?scontrolCaching=1&id="+engID+"&postType="+EncodingUtil.urlEncode(postType, 'UTF-8');

And then retrieve and decode it in your recipient controller:

String rawPostType = ApexPages.currentPage().getParameters().get('postType');
String postType = EncodingUtil.urlDecode(rawPostType, 'UTF-8');
Related Topic