1

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 Answer 1

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.

11
  • 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
    Commented Sep 19, 2016 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]; Commented Sep 19, 2016 at 20:02
  • Yes, chrome has a cookie api. Thx! Commented Sep 21, 2016 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? Commented Sep 21, 2016 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. Commented Sep 21, 2016 at 2:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .