Tell Me More: About the Code

There are two code objects behind our Visualforce custom action: the Apex class QuickOrderController and the Visualforce page QuickOrderPage.

The Apex class is the controller for the Visualforce page, and uses the @RemoteAction annotation on the methods. When you use this annotation, the Visualforce page wraps the logic in a JavaScript-friendly way. This is known as Visualforce remoting.

Visualforce remoting allows for quick and tight integration between Apex and JavaScript. This communication model works asynchronously as opposed to the synchronous model in the traditional Visualforce/Apex MVC paradigm. So after passing parameters into your controller, you can get the result from a response handler function, and write any additional client-side logic before doing any DOM manipulation or building your page with mobile templates or frameworks.

Visualforce remoting is ideal for mobile developers on the Salesforce1 Platform because it simplifies the direct server-side access to Salesforce objects and allows you to use Apex tools such as SOQL, Apex methods, and so on for rapid platform development. And, you don’t have to deal with view state, which makes pages perform better.

Apex Class QuickOrderController

This class uses Visualforce remoting and contains the logic called by the Visualforce page to find warehouses and create orders and line items.

The Apex controller also has an insertQuickOrder method that creates a feed item about the new order in the account feed as shown in this code snippet. The feed item is a link post that links to the invoice.

Visualforce Page QuickOrderPage

This page calls the controller with the user input and then displays the merchandise and warehouse information to the user. If the user wants to create an order, this page also calls the controller to create the order associated with the customer account and add a line item. At the beginning of the page, the code also does some styling of the page using the Salesforce mobile design templates.

The QuickOrderPage also calls the Force.com Canvas SDK to enable the publisher Submit button and close the publisher window.

First, it includes a reference to the SDK:
Then it calls the setValidForSubmit method to enable the publisher Submit button:
After the setValidForSubmit is called and the user clicks Submit, this subscribe method fires. This method invokes the final JavaScript function which uses JavaScript remoting to insert the line item (thus completing the quick order) and then post a feed item to the account:
Finally, after the callback from the remoting method returns successfully, this method closes the publisher window: