Currently my code uses salesforce partner wsdl of another salesforce org and obtain the session id. Below is code
// apex callout
partnerSoapSforceCom.Soap partner = new partnerSoapSforceCom.Soap();
AcustomSetting__c configs = AcustomSetting__c.getValues('AcustomSettingRecord');
String endpoint = configs.End_Point__c;
String userId = configs.userID__c;
String password = configs.password__c;
partner.endpoint_x = endpoint ;
partnerSoapSforceCom.LoginResult lr = partner.login(userId,password);
System.debug('Session Id obtained===========' + lr.sessionId);
And in subsequent request I add the JsessionId in the header for authroization:
Http http = new Http();
HttpRequest req = new HttpRequest();
// Endpoint of the API
req.setEndpoint('Endpoint');
req.setHeader('Content-Type','application/json');
//req.setHeader('Authorization', 'Bearer ' + lr.sessionId);
WSDLTOApex Class function login():
public partnerSoapSforceCom.LoginResult login() {
partnerSoapSforceCom.login_element request_x = new partnerSoapSforceCom.login_element();
//request_x.username = username;
//request_x.password = password;
partnerSoapSforceCom.loginResponse_element response_x;
Map<String, partnerSoapSforceCom.loginResponse_element> response_map_x = new Map<String, partnerSoapSforceCom.loginResponse_element>();
response_map_x.put('response_x', response_x);
if(!Test.isRunningTest()) {
WebServiceCallout.invoke(
this,
request_x,
response_map_x,
new String[]{'callout:Sample_NC',
'',
'urn:partner.soap.sforce.com',
'login',
'urn:partner.soap.sforce.com',
'loginResponse',
'partnerSoapSforceCom.loginResponse_element'}
);
}
response_x = response_map_x.get('response_x');
return response_x.result;
}
I do not want to use this approach as storing the userid and password in Custom setting is something I do not want(password is not encrypted and is visible to everyone in the org). I would like to use named credentials for this purpose(Simple authentication in HTTP header, do not want to use OAuth though)
I read through this instruction - https://help.salesforce.com/articleView?id=named_credentials_about.htm&type=0
However, I am trying to figure out how I can fix my above to code to use named credential instead of the place where I call the partner wsdl Login - partner.login(userId,password);
method to obtain session id. Any suggestions here how I can modify the code? Say the named credential is called - Sample_NC
.