Setting up a named credential for Salesforce REST Web Service

calloutintegrationlightning-web-components

I'm learning integrations and found out about named credentials. I want to set a named credential for my Apex method which performs a callout to my scratch org:

  @AuraEnabled(cacheable=true)
  public static List<Object> restCallout(){
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://ability-data-6444-dev-ed.cs102.my.salesforce.com/services/apexrest/Johnny');
    req.setHeader('Authorization', 'Bearer abcdef123456}');
    req.setMethod('GET');
    HttpResponse res = h.send(req);
    System.debug(res.getBody());
    List<Object> objs = (List<Object>)JSON.deserializeUntyped(res.getBody());
    System.debug(objs);
    return objs;
}

Scratch org web service:

@RestResource(urlMapping='/Johnny')
global class WebServiceFactory {
    @HttpGet
    global static void getRecord() {
        List<JSONFactory> JSONs = new List<JSONFactory>();
        for(integer i = 0; i<15; i++){
            JSONs.add(new JSONFactory(i, 'Jan' + i, 'Test' + i));
        
            }
            RestContext.response.responseBody = Blob.valueOf(JSON.serialize(JSONs));
    }
}

I am slightly confused about which Identity Type and Protocol to choose, so that I can avoid typing this line:

req.setHeader('Authorization', 'Bearer abcdef123456}');

Identity Type options:

  • Anonymous
  • Per user
  • Named principal

Protocol options:

  • OAuth 2.0
  • Password Authentification
  • AWS Signature Version 4
  • JWT
  • JWT Token Exchange

Could anyone give me a hint?

Best Answer

For a Salesforce to Salesforce connection, use Per user or Named principal with OAuth 2.0 or JWT. The Identity Type "Per user" allows each user in your org to authenticate with a user in the Scratch Org, while "Named principal" uses a single login (what you'd call an "integration user"). Using the Protocol "OAuth 2.0" uses a simple web-based flow to obtain a token, while JWT uses a command-line tool and security certificates, as outlined in the documentation. The most typical configuration is probably Named principal with OAuth 2.0 in most cases.

Related Topic