Named Credentials #inSalesforce

Named Credentials #inSalesforce


This specifies the URL of a callout endpoint and its required authentication parameters in one definition.

We can make call out to the external system without supplying username or Password.


Steps:

Setup | Named credentials | New | Give a good name (like SampleNC), URL - of sfdc instance you want to connect (https://samplepath.com), Identity Type - Named Principal, Authentication Protocol - Password Authentication (depends on requirement), username and password to be provided, Generate Authorization Header - checked | Save | on success, you will receive Success Message


Without Named Credential:
req.setMethod('POST');
req.setEndpoint('https://samplepath.com');
String username = 'username';
String password = 'password';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authHeader);
Http h = new Http();
HttpResponse response = h.send(req);
System.debug('response-' + response);
HttpRequest req = new HttpRequest();


With Named Credential: (The details provided above in code is taken care by Named Credential we created - No hard code)
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint('callout:SampleNC');
Http http = new Http();
HTTPResponse response = http.send(req);
System.debug('response-' + response);


Visual: https://www.youtube.com/watch?v=uRu96FdV9go

Comments