Call to get session ID not working

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 = “https://mmfp.na7.visual.force.com/services/Soap/u/26.0”; //** Will address this hardcoded URL shortly ** sforce.connection.init(sid, server); var jsList; skuid.$(’#polysearch’).autocomplete({ 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’); for(i=0;i<records1.length;i++){ 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’ }; jsList.push(obj); } 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’); for(i=0;i<records2.length;i++){ var obj = { label: records2[i].Name + " " + records2[i].MobilePhone + " “+ records2[i].Email, value: records2[i].Id, icon: ‘icon-male icon-2x icon-fixed-width’ }; jsList.push(obj); } 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’); for(i=0;i<records3.length;i++){ var obj = { label: records3[i].Name + " " + records3[i].Title + " “+ records3[i].Email, value: records3[i].Id, icon: ‘icon-user icon-2x icon-fixed-width’ }; jsList.push(obj); } response(jsList); }, select: function( event, ui ) { var uivalue = ui.item.value; window.location.replace(“https://na7.salesforce.com/”+ uivalue); //** Will address this hardcoded URL shortly ** } }).data(“uiAutocomplete”)._renderItem = function (ul, item) { return skuid.$(”

  • ") .data(“item.autocomplete”, item) .append(" " + item.label + “”) .appendTo(ul); }; });

  • 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 ) // 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}"); 
    

    Option 2 works perfectly. Thanks Zach. Awesome.

    So now I’m trying the next step in this and have busted it again. I’m trying to get the server URL using a global variable, but it’s failing: var sid = skuid.utils.mergeAsText(“global”, “{{$Api.Session_Id}}”); //Returns the user’s session ID var server = skuid.utils.mergeAsText(“global”, “{{$Api.Enterprise_Server_URL_290}}”); //Returns the URL of the Salesforce instance sforce.connection.init(sid, server); //Connects to Salesforce using session and server Is that the right way to get the server URL?

    I’ve fixed this. I was overcomplicating it. Don’t need the server URL, just need the user’s session ID, so this works fine: var sid = skuid.utils.mergeAsText(“global”, “{{$Api.Session_Id}}”); //Returns the user’s session ID sforce.connection.sessionId = sid; //Establishes the Salesforce connection based on the user’s session

    The easiest way to get the Enterprise / Partner Server Url for the current release is:

    var enterpriseServerUrl = skuid.utils.mergeAsText("global", "{{$Api.Enterprise_Server_Url}}"); var partnerServerUrl = skuid.utils.mergeAsText("global", "{{$Api.Partner_Server_Url}}");