Can we use JsForce in Lightning component?

I need to Describe SObject i.e., retrieve Field data types for an object in lightning component helper. Would like to check if I can use JsForce instead of Apex method call which can pull information using Schema.DescribeSobjectResult.

  • 2
    It should be possible by wondering why you would want to use JSforce instead of apex(@AuraEnabled method) as it consumes API calls. – Raul Feb 8 '17 at 18:15
  • Thanks Rahul, Thats perfect. I don't actually want to use JsForce if it consumes API calls, but would like to know if we can use JsForce in lightning... (just to test). Small problem, I have included JsForce script, but was unable to use jsforce.Connection(). Could you provide any example ? – Sriram Feb 8 '17 at 18:32
  • Could you share what you have tried so far? (Update your question and add it) – Raul Feb 8 '17 at 19:38

Lightning Component: Add required scripts to component. scripts should be in static resources.

<ltng:require scripts="{!$Resource.ABC + '/lib/jsforce.min.js'}"/> 

Controller: Authorize connection using Session_Id obtained from @AuraEnabled Apex Method or Attribute passed all the way from Visual force Page.

var getSessionIdAction = component.get("c.getSessionID"); //getSessionID - Apex method

    getSessionIdAction.setCallback(this, function(response) {
        var state = response.getState();
        if(state === 'SUCCESS'){
            var conn = new jsforce.Connection({ accessToken: response.getReturnValue() });
            conn.sobject("Account").describe(function(err, meta) {
              if (err) { 
                 return console.error(err); 
              console.log('Label : ' + meta.label);
              console.log('Num of Fields : ' + meta.fields.length);
              meta.fields.forEach(function(field) {
                console.log(field.name + ' - '  + field.label + ' - ' + field.type);

JsForce works pretty well in lightning component but Its not recommended for the case here due to costly API Calls. Hence I am going to use a simple @AuraEnabled Apex method which returns DescribeFieldResult for an object.

| improve this answer | |
  • Tried your code example with api version 37.0 to 40.0 - I receive a 401 or Access-Control-Allow-Origin error upon conn.sobject("Account") I am trying to use jsforce for listening to platform events – Christian Szandor Knapp Jul 23 '17 at 14:51
  • 2
    I'm getting error Refused to connect because it violates the following Content Security Policy directive: "connect-src 'self' Seems this is known limitation with jsforce: github.com/jsforce/jsforce/issues/269 – Doug Ayers Aug 6 '17 at 6:43
  • Yarp, same behaviour as I experienced. Wonder what bayeux does differently when using oauth to connect to streaming apis. it works for bayeux but doesn't for jsforce. – Christian Szandor Knapp Aug 6 '17 at 15:07
  • I believe Streaming API is specifically whitelisted for Lightning because there is no alternative way to use its features effectively than from the front end. If what you're trying to do is a SObject describe, this is something you should do in the component's Apex controller. – Charles T Jan 4 '18 at 20:25

JSforce not supported Lightning "Unfortunately lightning component does not allow to invoke API endpoint from component JavaScript. " https://github.com/jsforce/jsforce/issues/269

| improve this answer | |

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.