Skip to content

Instantly share code, notes, and snippets.

@amitastreait
Last active July 17, 2018 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitastreait/427a431bc1b3e45f09ee1acdee17a34d to your computer and use it in GitHub Desktop.
Save amitastreait/427a431bc1b3e45f09ee1acdee17a34d to your computer and use it in GitHub Desktop.
Login_User__c loginUserSetting = Login_User__c.getOrgDefaults();
String Session_Id;
If(loginUserSetting !=null){
Organization orgInfo = [Select Id, Name, isSandbox From Organization LIMIT 1];
String endPoint = '';
If(orgInfo.isSandbox)
endPoint = 'https://test.salesforce.com/services/oauth2/token';
else
endPoint = 'https://login.salesforce.com/services/oauth2/token';
System.debug('#### endPoint '+endPoint);
String requestBody = 'grant_type=password&client_id='+loginUserSetting.Client_Id__c+'&client_secret='+loginUserSetting.Client_Secret__c
+'&username='+loginUserSetting.userName__c+'&password='+loginUserSetting.Password__c+loginUserSetting.Token__c;
System.debug('#### requestBody '+requestBody);
HttpRequest httpReq = new HttpRequest();
HttpResponse httpRes = new HttpResponse();
httpReq.SetEndPoint('https://test.salesforce.com/services/oauth2/token');
httpReq.SetHeader('Content-Type','application/x-www-form-urlencoded');
//httpReq.setTimeout(12000);
httpReq.setMethod('POST');
httpReq.setBody(requestBody);
httpRes = new http().send(httpReq);
System.debug('#### responseBody '+httpRes.getBody());
IF(httpRes.getStatusCode() == 200){
Map<String, Object> responseMap = (Map<String, Object>)JSON.deserializeUntyped(httpRes.getBody());
String sessionId = (String)responseMap.get('access_token');
Session_Id = sessionId;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment