+ Start a Discussion

Page navigation in Salesforce1 app

I'm writing a set of VF pages, designed to run in Salesforce1, to allow the user to Find and then Add Products to a given Opportunity.

The Find Products page lets the user search for the desired Products, select them, then navigate to the Add Products page. To get to the Add Products page, the Find Products page has JavaScript that sets window.location.href to /apex/AddProducts, passing in query string params with the Opportunity Id and the selected Price Book Entry Ids. This is all working fine.

The Add Products page then displays a form that lets the user enter the quantities for the selected Price Book Entries. When the user clicks the page's Save button, the page correctly saves the new Opportunity Products (via a Remote Actoin call to the controller), and then calls the following JavaScript funciton, passing in the Opportunity Id as an argument:
function navigateToSObject(id) {
    if ((typeof sforce == 'undefined') || (sforce == null) ) {
        // Not running in Salesforce1 - go to the regular Opportunity list view
        alert('setting window.location.href');
        window.location.href = '{!URLFOR($Action.Opportunity.View, Opportunity.Id)}';
    else {
        // Running in Salesforce1
        alert('doing sforce.one.navigateToSObject');
        sforce.one.navigateToSObject(id, 'detail');
I've tried launching my Find Products page as a Publisher Action on the Opportunity object and as a custom button on the Opportunity page layout, and so far, it's doing everything right until the Add Products page tries to get back to the Opportunity View page - that's where I'm having problems.

On my iPad, when I launch the Find Products page as a Publisher Action, when the Find Products page tries to display the Opportunity View page, it's not recognizing that the sforce object exists, so it's setting window.location.href to the URL, and so what gets displayed in Salesforce1 is the standard browser-based Opportunity view page, complete with header and sidebar, and not the SF1 app's view of the page. Furthermore, the page is Opportunity view page displayed in the publisher action's pop-up window, not in the main SF1 app's window.

On my iPad, when I launch the Find Products page as a custom button, again, it doesn't recognize that the sforce oject exists, so it's redirecting to the standard browser-based Opportunity view page, complete with header and sidebar.

My questions:
  • For pages like the ones I'm trying to develop, what's the recommended way to launch them? Publisher Action or custom button? (In both cases, I'd really rather not have the action/button visible to non-SF1-app users, but I don't think there's a good option for accomplishing that.)
  • When a VF page is running in the SF1 app, what's the recommended way of navigating to a standard Salesforce page? I've seen several pieces of documentation saying to check whether sforce is defined, and then do sforce.one.navigateToSObject(recordid, 'detail'), but that's definitely not working for me. In fact, I've seen postings saying that sforce.one is buggy.
  • When a VF page is running in the SF1 app, what'st eh recommended way of navigating to a VF page? I haven't seen any documentation on that.



I've been using Salesforce1 since the outset and I haven't hit any problems with the sforce object being undefined unless I am working in a child browser (created using window.open) or an iframe - in both of those cases the page doesn't know its in Salesforce1 because there is an intermediate layer in the way.  Do you have any links to the posts detailing the sforce.one availability?

If you are using a publisher action, the way to handle navigation is to utilise the publisher JavaScript library to close the action popup and refresh the main window.  You can see an example of this in my SF1Utils project on github in the following VF component :


The key piece of JavaScript is :

Sfdc.canvas.publisher.publish({name: "publisher.close", 
			   payload:{ refresh:"true"}});