0

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) {
    console.log(sessionID);
    // Make the REST call here
}


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

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?

1

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

function processSessionID(sessionID) {

    var client = new forcetk.Client();
    client.setSessionToken(sessionID);

    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) {
    console.log(response.opps);
  });
});

Then, in your content script, add a listener:

var client = new forcetk.Client();
client.setSessionToken(sessionID);

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]; – Phil Hawthorn Sep 19 '16 at 20:02
  • Yes, chrome has a cookie api. Thx! – user3063045 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? – user3063045 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. – user3063045 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.