2

Does anyone know of a guide for setting up a named credential for a Salesforce Org to access it's own API? This is a required step when using Apex from a Lightning Component. I know that the API does not support Basic authentication so does this mean that you have to go through all the steps of setting up a connected app and auth provider? I am sure someone has set this up but I just can't find any official or unofficial guide on the complete flow of accessing the Rest API from a Lightning Component.

Edit to add scenario

I just want to add a simple scenario to better explain the problem. This scenario works from Visualforce today.

Let's say I have a Lightning Component that I want to display search results in. The Salesforce Admin has already configured which fields show in search results using standard functionality. I want to grab that configuration from the search layouts REST resource (I understand this needs to be from my Apex controller). What steps do I need to take to allow the Lightning Component's Apex controller to obtain access to this REST API? If I include this component in a managed package, how much of the setup can I package and how much must be performed by an admin after installation?

4
  • what exactly is it you are trying to do via the API (or in general) ?
    – glls
    Commented Aug 15, 2017 at 1:31
  • Get page layout information.
    – dsharrison
    Commented Aug 15, 2017 at 1:31
  • if i am not mistaken, you can only achieve this using the metadata api, Using Meta data api in apex
    – glls
    Commented Aug 15, 2017 at 1:38
  • Salesforce introduced the User Interface API recently which actually gives the info in a much more usable format (bundled with record info). The rest API also has a standard layouts resource which gives layout info for the running user. I am using this in VF right now but because of the issues with authentication I can't get it running from a Lightning Component.
    – dsharrison
    Commented Aug 15, 2017 at 1:43

1 Answer 1

2

The Known Issues - Generating a Session Id from Lightning Domain Provides Invalid Session Id suggests it should be possible to get a valid session id via UserInfo.getSessionId(). I'm a bit surprised by this as it seems to contradict most other posts that say they don't want you doing this.

However, the particularly interesting part here on the Known Issues is that it is

SCHEDULED - WINTER '18

So if you can wait till the next release, you might find you can just directly grab a valid Session ID in the Apex controller. You could try this now in a pre-release org, but I'm not sure how to tell if the fix has been applied there or not.


Update 24th August 2017:

Tested on GS0.

Current status making API calls from UserInfo.getSessionId() returned via an @AuraEnabled controller method to a Lightning Component...

INVALID_SESSION_ID: This session is not valid for use with the API.

I think this corresponds to an AuthSession with the SessionType Aura.

1
  • Wow, great find. I am now even more confused now since as you mention Salesforce has made it sound like the current behavior is by design and cannot change for security reasons. I will see if it is possible to get in touch with someone from their side to verify that they are actually making a fix for this.
    – dsharrison
    Commented Aug 19, 2017 at 14:33

You must log in to answer this question.

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