function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RoyGiladRoyGilad 

sforce.one is being called on desktop

We have a VF page that has the code from this articale (https://developer.salesforce.com/docs/atlas.en-us.salesforce1.meta/salesforce1/vf_dev_best_practices_pages_multipurpose.htm) to manage if the code runs on SF1 or desktop.
Since we do create sforce instace for JS work the if as apper in the articale generates an error 
if( (typeof sforce != 'undefined') && (sforce != null) ) {
    // Salesforce1 navigation
}
Does anyone knows why Salesforce whould think that sforce is only found in SF1?
Wouldn't it be more currect to have it :
if( (typeof sforce != 'undefined') && (sforce != null) && (sforce.one) ) {
    // Salesforce1 navigation
}
To make sure there is a sforce.one instance?

Thanks
 
Best Answer chosen by RoyGilad
Alderete_SFDCAlderete_SFDC
Hey folks,

There's a variety of ways to attempt to verify that you're executing in a Salesforce1 context, or not. We'll be updating the doc topic highlighted to use the following if condition for the test:
if( typeof sforce != "undefined" && sforce && sforce.one )
You do need to test that sforce exists before you test for sforce.one. But, depending on your coding style, you might want to write it differently, test a few additional things, etc.

I'd recommend extracting the if condition out into a utility function, so that if the assumptions that underlie it ever change, you can update the function once, instead of doing a lot of search-and-replacing. (hint hint, safe harbor, etc.)

All Answers

Prosenjit Sarkar 7Prosenjit Sarkar 7
I have experineced that from API version 33 sforce is present in both web & salesforce1. Thereby, we should write this like 
if(typeof sforce.one != 'undefined') { // For Salesforce1 
}
else { // For Salesforce web
}

 
RoyGiladRoyGilad
Yes, this is exactly my finding, I wasn't sure if I'm the only one that had experienced it since it is a different behavior than the Salesforce Doc 
Alderete_SFDCAlderete_SFDC
Hey folks,

There's a variety of ways to attempt to verify that you're executing in a Salesforce1 context, or not. We'll be updating the doc topic highlighted to use the following if condition for the test:
if( typeof sforce != "undefined" && sforce && sforce.one )
You do need to test that sforce exists before you test for sforce.one. But, depending on your coding style, you might want to write it differently, test a few additional things, etc.

I'd recommend extracting the if condition out into a utility function, so that if the assumptions that underlie it ever change, you can update the function once, instead of doing a lot of search-and-replacing. (hint hint, safe harbor, etc.)
This was selected as the best answer
Prosenjit Sarkar 7Prosenjit Sarkar 7

Hi Alderere_SFDC ,
From version 33 sforce is always present whether we are opening it from web or salesforce1.

@SF-Roy Thanks. If you ind my solution fruitful please select it a best answer :P 

Alderete_SFDCAlderete_SFDC
@Prosenjit: You might indeed be finding that sforce is always present, in both web and Salesforce1 contexts. Do not count on this in your code! We don't document that this is or will be the case, and that gives us the freedom to change it.

It's a behavior that has changed in the past, and will likely change again. Or we might provide a much better, more reliable way to check. That's why I recommend abstracting out the check for Salesforce1 into a utility function. That way, when it changes again, you'll be able to update your code quickly.
 
Digression that has absolutely no bearing on the above: Yesterday at the Daring Fireball Live event, John Gruber interviewed Phil Schiller (a very senior Apple executive). One question he asked, and it took a long time to ask it, was about the new feature of iOS 9, side-by-side layouts of multiple apps. In the keynote Apple said that "you've already done the work to support this, by adopting Auto Layout".

Gruber observed that this feature got the most mixed reaction from the crowd. You could see that half of the audience had adopted Auto Layout already, because they cheered. You could also see the laggards in the audience, who had not adopted this feature, because they groaned as they realized that they now had no choice, and it was going to be more work because they'd waited.

The question, when it finally came, came in the form of a statement. Roughly, "it seems like when Apple gives you the hint that you want to adopt a particular technology, it's often a very good idea to take the hint". Schiller's response was a deadpan "we have a pretty good track record of that".

Put your Salesforce1 check in a utility function. Check for sforce before you check for sforce.one. Safe harbor.
Prosenjit Sarkar 7Prosenjit Sarkar 7
Thanks for your information @Alderate_SFDC
jasonmuldoonjasonmuldoon
With the news about Lightning Experience for Desktop, I am starting to see sforce.one is available in desktop as well. Is there any additional checks that we can do to prevent our SF1 content load in the desktop version?
Alderete_SFDCAlderete_SFDC
Unfortunately at this time the answer is no.

See "Detecting the User Experience Context" here: https://developer.salesforce.com/trailhead/lex_dev/lex_dev_visualforce/lex_dev_visualforce_multipurpose_pages

If this is going to pose a significant problem, please log a case and explain your use case.