0

I'm following the Getting Started section on the JSforce documentation and I'm getting the following error:

resource: the server responded with a status of 400 (Bad Request) INVALID_SESSION_ID Session expired or invalid

Code:

<apex:page >
   <script src="//cdnjs.cloudflare.com/ajax/libs/jsforce/1.9.1/jsforce.min.js"></script>
   <script>
      var conn = new jsforce.Connection({ accessToken: '{!$API.Session_Id}' });
      conn.query('SELECT Id,Name FROM User WHERE Id=:Userinfo.getUserId()', function(err, res) {
        if (err) { return handleError(err); }
          console.log('works!');
          console.log(res);
      });        
   </script>
</apex:page>

I assume that the issue is related to 'Userinfo.getUserId()' because if I change the query to SELECT Id,Name FROM User then it works.

Please advise.

0

You can't use UserInfo.getUserId() because you are running the code from Visualforce and not from apex. Instead, try with {!$User.Id}:

conn.query("SELECT Id,Name FROM User WHERE Id='{!$User.Id}'", function(err, res) {
    if (err) { return handleError(err); }
      console.log('works!');
      console.log(res);
  });
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.