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
PBS_IC786PBS_IC786 

Calling a javascript function from a visualforce page

Folks,

Anyone know why I would be getting an Invalid Session error when I try to call a javascript function from an onclick event in the my Visualforce page.  The javascript function has a data call using the sforce.connection.query syntax.  Everytime I test fire my onclick event, I get a popup error on my VF page stating that the Invalid session...Illegal session and some other text....

Any help is greatly appreciated...

Thanks
dchasmandchasman
Visualforce does not automatically login into the ajax toolkit for a number of reasons (see the ajax toolkit doc on how to establish the connection in your javascript) - one of which is that there are very few if any reasons to use the ajax toolkit now that we have visualforce. If you describe what you are trying to accomplish and we should be able to point in the right visualforce-centric direction.
canonwcanonw
After do a project to convert s-control to VisualForce, I have to keep this in mind about SOQL.

Bind action attribute to a controller method.  Let the controller method call the SOQL. 
WesGrayWesGray
>there are very few if any reasons to use the ajax toolkit now that we have visualforce

One reason is to do a dynamic SOQL query.  VF controllers don't appear to support this yet.
dchasmandchasman
I am happy to be able to say that we have removed that reason to use the ajax tookit :-) Take a look at Dynamic SOQL (currently in pilot status but will be generally available in the near future).
Chris DChris D
In an scontrol, the following is automatically embedded within the code somewhere before connection.js is included:

NOTE from salesforce: Please do not use this suggested approach to establishing the ajax toolkit session - see reply from dchasman for the supported mechanism
<script type="text/javascript">__sfdcSessionId = '{!$Api.Session_ID}'</script>

The Ajax toolkit references this. Drop this into your VF page near the top to have the same functionality. For some reason connection.js seems to already be included in VF pages.

Keep in mind that this could break in later versions of the toolkit, especially since connection.js is automatically included.

There are a few very specific use cases where a hybrid VF/Ajax toolkit app is a more optimal design pattern.


Message Edited by dchasman on 07-08-2008 08:04 AM
dchasmandchasman
You do not want to use the exact auto injected code from scontrols because that code is an internal detail that is very much subject to change at any time. instead, use the following supported approach (place this javascript inside of a script block somewhere before you try to use the ajax toolkit):

Code:
sforce.connection.sessionId = '{!$Api.Session_ID}';

I researched the inclusion of connection.js (that was a surprise to me) and tracked it back to the System Log functionality - going to see about getting that dependency removed asap (could definitely result in some undesirable behavior).

Chris - can you elaborate on your statement "There are a few very specific use cases where a hybrid VF/Ajax toolkit app is a more optimal design pattern."? From a Visualforce perspective we are working to remove all need for the ajax toolkit and I would very much like to hear about any specific use cases where the hybrid is still the opti


Message Edited by dchasman on 07-08-2008 08:01 AM
Chris DChris D
Partial page updates where you would have VF/Apex re-render an entire page block server side is an inefficient use of bandwidth in some situations.
mtbclimbermtbclimber
You can control how much of the page is posted back (actionRegion) and how much is re-rendered based on how granularly you define the rerender targetS . Perhaps you could share one of these very specific examples with us if you still feel strongly that this does not offer the bandwidth control you are seeking.

Thanks,
Chris DChris D
Upon careful consideration, it is important to make a distinction between JavaScript and the AJAX toolkit for SOAP messages. In theory one can use VF/Apex to return back JSON for a REST style app. SOAP is actually sub optimal for this purpose when it comes to conserving bandwidth. Keep up the good work.
dchasmandchasman
Agreed - and this REST w/JSON wire format combination is actually something we've been doing with VF in a few places. Consuming SOAP/XML in javascript is also not the fastest thing on the planet. We have plans to make this much easier in future releases. For example adding support for things like native JSON serialization for apex classes is something we're batting around. Making controller actions URL addressable is another concept we're considering - the same thing can be accomplished using a page with a page level action binding but for some situations that seems unnatural.
asgirisoftasgirisoft
Doug,

I am developing a Visualforce page that would need to access the EMAILSTATUS object and related Contact objects and display them in a report format. This is currently available as the "HTML Email Status report". I could not modify that. Neither does SF expose this object for reporting purposes.
It is available only via the Ajax API.
How would I get a VF controller to access this object and print all the join records between EMAILSTATUS and Contact objects?

Andi Giri