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
Abhilash Mishra 13Abhilash Mishra 13 

getting error, when querying using ajax toolkit.

Hi i want to use ajax toolkit for my project, however when trying one of the example i m getting this error again and again


{faultcode:'sf:INVALID_SESSION_ID', faultstring:'INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session', detail:{UnexpectedErrorFault:{exceptionCode:'INVALID_SESSION_ID', exceptionMessage:'Invalid Session ID found in SessionHeader: Illegal Session', }, }, }

Here is the VF page.
 
<apex:page >
          <body><button onclick="setupPage();"> dev </button>
                 <div id="output"></div>
          </body>
          
          <script src="../../soap/ajax/35.0/connection.js" type="text/javascript"></script>
          <script>
                    function setupPage() {
                       
                           sforce.connection.query('select id from lead',
                           {onSuccess : layoutResults,
                            onFailure : queryFailed,
                            source : {
                                      output : document.getElementById("output"),
                      startTime : new Date().getTime()
                     }
           });
          
       }
       function queryFailed(error, source) {
       alert('hello 1');
                source.output.innerHTML = error;
}
/**
* This method will be called when the toolkit receives a successful
* response from the server.
* @queryResult - result that server returned
* @source - state passed into the query method call.
*/

function layoutResults(queryResult, source) {
     alert('hello 2');
     if (queryResult.size > 0) {
     var output = "";

     //get the records array
     var records = queryResult.getArray('records');

     //loop through the records and construct html string
     for (var i = 0; i < records.length; i++) {
          var account = records[i];
          output += account.Id + " " + account.Name +
          " [Industry - " + account.Industry + "]<br>";
     }

     //render the generated html string
     source.output.innerHTML = output;
     }
}

          </script>
    
</apex:page>
Need Help !!
Thanks 
Abhilash
Best Answer chosen by Abhilash Mishra 13
thatheraherethatherahere
Hi Abhilash,

You need to set sessionId to sforce.connection.sessionId variable.

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

just before line number 8 in your code.


Thanks,
- thatherahere

All Answers

thatheraherethatherahere
Hi Abhilash,

You need to set sessionId to sforce.connection.sessionId variable.

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

just before line number 8 in your code.


Thanks,
- thatherahere
This was selected as the best answer
Abhilash Mishra 13Abhilash Mishra 13
ohh great,
Thanks thatherahere,

Can I use this for Ajax portal login for my Force.com site. here is what I am asking?
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000D85JIAS
thatheraherethatherahere
Abhilash,

I commented on your question. 

Please mark this question as SOLVED if my previous reply worked for you. This will help other people here.

- thatherahere