Call to get session ID not working

  • 1
  • Question
  • Updated 6 years ago
  • Answered
I've read the community posts and the developer docs about getting the running user's session ID using XXX. But I can't get it working in my JQuery function below.

The function runs SOQL database queries based on input text using JQueryUI autocomplete. It needs to make a connection to Salesforce using the session ID, but it fails, giving an error in the console:
Uncaught {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', }, }, }

I've tested the function by hard-coding the session ID and it works fine, so I just need to nail this issue. Here's the code, appreciate any help:
skuid.$(function() {
//Establish a connection to Salesforce using the
//sforce.connection.init(sessionID, ServerURL) function.
var template = "{{$Api.Session_Id}}"; //This isn't working!
var sid = skuid.utils.merge('global', template);
var server = ""; //** Will address this hardcoded URL shortly **
sforce.connection.init(sid, server);
var jsList;
minLength: 2, //Minimum number of characters user must type before search commences
delay: 350, //Delay in milliseconds before the database call is made
source: function(request, response) {
var st = request.term;
jsList =[];
var query1 = "select Id, Name, ShippingCity, ShippingState, ShippingPostalCode from Account where Name Like '%"+st+"%' limit 20";
queryResult1 = sforce.connection.query (query1,null);
var records1 = queryResult1.getArray('records');
var obj = {
label: records1[i].Name + " " + records1[i].ShippingCity + " " + records1[i].ShippingState + " " + records1[i].ShippingPostalCode ,
value: records1[i].Id,
icon: 'icon-heart icon-2x icon-fixed-width'
var query2 = "select Id, Name, MobilePhone, Email from Contact where Name Like '%"+st+"%' limit 20";
queryResult2 = sforce.connection.query (query2,null);
var records2 = queryResult2.getArray('records');
var obj = {
label: records2[i].Name + " " + records2[i].MobilePhone + " "+ records2[i].Email,
value: records2[i].Id,
icon: 'icon-male icon-2x icon-fixed-width'
var query3 = "select Id, Name, Title, Email from User where Name Like '%"+st+"%' limit 20";
queryResult3 = sforce.connection.query (query3,null);
var records3 = queryResult3.getArray('records');
var obj = {
label: records3[i].Name + " " + records3[i].Title + " "+ records3[i].Email,
value: records3[i].Id,
icon: 'icon-user icon-2x icon-fixed-width'
select: function( event, ui ) {
var uivalue = ui.item.value;
window.location.replace(""+ uivalue); //** Will address this hardcoded URL shortly **
}).data("uiAutocomplete")._renderItem = function (ul, item) {
return skuid.$("<li />")
.data("item.autocomplete", item)
.append("<a><i class='" + item.icon + "'></i>&nbsp;" + item.label + "</a>")
Photo of Glenn Elliott

Glenn Elliott, Champion

  • 7,748 Points 5k badge 2x thumb
  • getting a tad frustrated

Posted 6 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 52,296 Points 50k badge 2x thumb
Official Response
Hi Glenn,

My guess is that the problem is that the skuid.utils.merge method always returns HTML, and when you make the API call, you need to send a text string. There are two ways you can get around this. Either way, I'd do a console.log(sid) as a sanity check:

// Option 1 --- use .text() to convert the DOM element
// that the merge method generates

// (a <div class="nx-template">)

// into a text string
var sid = skuid.utils.merge("global","{{$Api.Session_Id}").text();

// Option 2 --- use .mergeAsText() instead, which does this for you.
var sid = skuid.utils.mergeAsText("global","{{$Api.Session_Id}");