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
Praetorian65Praetorian65 

Using Javascript to run apex from a VF page

I have a Visualforce page that contains a javascript function:
 
Code:
       function reportSuccessOrFail()
        {
          try {
                 var success = sforce.apex.execute("ReadResult","GetResult",{});
                
                if(success ==true)
                    alert('Salesforce data successfully updated');
                else
                    alert('There was an error with the update');
                
            } catch (e) {
                alert(e.name + ' ' + e.message);
            }
        }

This uses the following apex class:

Code:
global class ReadResult{
    webservice static boolean GetResult()
    {
        boolean returnresult = false;
    
        DateTime timefrom = DateTime.now();
        DatabaseRequest__c[] dbr = [select Result__c from DatabaseRequest__c order by Time__c desc];
        
        if(dbr.size()>0)
        {
            returnresult  = dbr[0].Result__c;
        }
        
        return returnresult;
    }
} 

 
When I run it I get the error: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session
 
How do I fix this?


Message Edited by Praetorian65 on 01-07-2009 06:05 AM
dchasmandchasman
While invoking an apex code based webservice via the ajax toolkit is supported I have yet to see a situation where using action binding and a controller action method does not provide a superior/simpler/no javascript required/etc solution that is much more in line with the VF philosophy. Why are you not just binding to an action method from an apex:commandButton/Link or apex:actionSupport/apex:actionFunction?
Praetorian65Praetorian65
Because the command button that I am using is running an apex script that gets data from an external server via a webservice. This can take up to 5 seconds to complete. What I want to do is to give the user feedback as to whether or not it succeeded.
dchasmandchasman
That does not require switching away from using an action. Take a look at the rich support for async requests with feedback during the request already baked into VF. By default a commandButton will invoke its action as a full page refresh but if you leverage rerender /or oncomplete attributes on the component that owns the action binding combined with apex:actionStatus you can get very powerful async behaviors.
Praetorian65Praetorian65
Do you have a link for a page that will help get me started? I have tried using an actionstatus but i can't see how to get it to do what I want. I basically need to press the button, wait a few seconds and then check the database for the existence of a certain object. I then have to alert the user based on certain fields on that object. I have read the VF developer's guide for actionstatus and related things but it isn't too helpful.

I should also point out that the action completes before the apex script has finished processing. I have to wait for the script to finish before checking the database object.

Message Edited by Praetorian65 on 01-07-2009 08:02 AM
dchasmandchasman
Understood - I'll try to post an example soon - (as a hint apex:actionPoller will also be needed for this situation).
a_Ba_B
Set session id to connection before calling its execute function

sforce.connection.sessionId = "{!$Api.Session_ID}";


I think you already have included the following script.
<script src="/soap/ajax/13.0/connection.js"/>