I've got a Chrome extension running while I'm signed in to Salesforce. I want to use my active SessionID from my session to query Opportunities using Javascript. I've tried passing the sessionID via Ajax but that is not working (getting 401 error).

Here's my code so far:

function processSessionID(sessionID) {
    // Make the REST call here

  "domain": "lightning.force.com", "name": "sid"
}, function(cookies) {
  for(i = 0; i < cookies.length; i++) {
    var sessionID = cookies[i].value;

UPDATE: The answer to my question is on the right track. ForceTK does access the API via Javascript - which is what I'm loooking to do. However, it's failing to resolve names on a chrome extension.

How can I make a REST call with with the sessionID to return a list of opportunities?


Calling the REST API in javascript using forcetk is pretty straightforward in a content script:

function processSessionID(sessionID) {

    var client = new forcetk.Client();

    client.query("SELECT Id, Name FROM Opportunity", function(response){
        // do something with response, response is a list of Opportunities in this case

Using it from a popup seems a little more involved. Firstly, I think you would need to use a content script to setup the forcetk client and interact with Salesforce. Then, from your popup, you'd have to use messaging to communicate back and forth. For example, in your popup.js:

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  chrome.tabs.sendMessage(tabs[0].id, {method: "getOpportunities"}, function(response) {

Then, in your content script, add a listener:

var client = new forcetk.Client();

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    if(request.method === 'getOpportunities') {
      client.query('SELECT Id, Name FROM Opportunity',function(response) {
        sendResponse({opps: response});

PS. I haven't tried this but seems like it should work.

  • I do think OP needs a bit of direction on how to get the sessionId as well? Unless you usually look through cookies to figure that out?
    – Adrian Larson
    Sep 19 '16 at 19:48
  • It depends on what type of extension he's developing, but it can be as simple as var sessionID = document.cookie.match("sid=([^;]*)")[1]; Sep 19 '16 at 20:02
  • Yes, chrome has a cookie api. Thx! Sep 21 '16 at 2:07
  • When I run this with the current sessionID I'm getting this; Failed to load resource: net::ERR_FILE_NOT_FOUND chrome-extension://okgglkjjoapejcmbmegeclmnfmdamool/services/data/v29.0/query?q=SELECT%20Id%2C%20Name%20FROM%20Opportunity&_=1474424357089. Looks like forcetk is not being passed the instance id. How do I do that? Sep 21 '16 at 2:21
  • What does this mean? ForceTK requires that you add the correct REST endpoint hostname for your instance (i.e. na1.salesforce.com or similar) as a remote site in Your Name > Administration Setup > Security Controls > Remote Site Settings. Sep 21 '16 at 2:28

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.