You need to sign in to do that
Don't have an account?
Chethan SN
Stuck in implementing Rest HTTP callout.
Hi All,
I am making a Rest API call which results in select query (filter) of a list view. This works in rest client as i am making a seperate calls for token and then actual data(with access token as header).
But i am stuck in implementing the same in apex code. Please suggest where i am going wrong ?
public class TestApex {
@AuraEnabled
public static void testMethod1(String str){
try{
//testMethod1 tesM = new testMethod1();
Object obj = JSON.deserializeUntyped(str);
system.debug('Object :'+obj);
/*************************************************************************************************************/
HttpRequest req = new HttpRequest();
String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
String filterId = '00B6A000002Fc2BUAS';
//String filterId = '00B6A000002Fc0LUAS';
String endPoinURL = baseUrl+'/services/data/v41.0/sobjects/Account/listviews/'+filterId+'/describe';
req.setEndpoint(endPoinURL);
req.setHeader('Authorization', 'Bearer' + UserInfo.getSessionId());
req.setMethod('GET');
system.debug('UserInfo.getSessionId()' + UserInfo.getSessionId());
//ID tempAuth= '**************************************************************************bGUyNIK.obuwcoY'; System.debug('UserInfo.getSessionId()'+UserInfo.getSessionId());
//req.setHeader('Authorization', 'Bearer ' + tempAuth);
Http http = new Http();
HTTPResponse response = http.send(req);
// Map<String, Object> tokenResponse = (Map<String, Object>)
// JSON.deserializeUntyped(response.getBody());
system.debug('Response'+JSON.deserializeUntyped(response.getBody()));
/* String query = '';//(String) tokenResponse.get('query');
Map<String,String> columnData = new Map<String,String>();
for(String key : tokenResponse.keySet()){
if(key.contains('columns')){
List<Object> colList = (List<Object>)tokenResponse.get(key);
for(Object obj : colList){
Map<String,Object> colMap = (Map<String,Object>)obj;
if(colMap.containsKey('hidden') && !(Boolean)colMap.get('hidden')){
columnData.put(String.valueOf(colMap.get('label')),String.valueOf(colMap.get('fieldNameOrPath')));
}
}
}
}
for(String key : columnData.keySet()){
System.debug('Lable : '+key+' : Itag : '+columnData.get(key));
}
List<Account> accList = Database.query(query);
for(Account acc : accList){
}
*/
}catch(Exception ex){
System.debug('Line Number '+ex.getLineNumber());
System.debug('Exception '+ex.getMessage());
}
}
@AuraEnabled
public static String testMethod2(){
try{
system.debug('Inside testMethod2');
HttpRequest req = new HttpRequest();
String clientId = '3MVG982oBBDdw******************************************CO4g';
String username = '*************';
String password = '**********';
String clientSecret = '353*********************9';
// String loginUri = URL.getSalesforceBaseUrl().toExternalForm();
// String loginUri='https://test.salesforce.com/';
req.setMethod('POST');
// req.setEndpoint(loginUri+'/services/oauth2/token');
req.setEndpoint('https://test.salesforce.com/services/oauth2/token');
req.setBody('grant_type=password' +
'&client_id=' + clientId +
'&client_secret=' + clientSecret +
'&username=' + EncodingUtil.urlEncode(username, 'UTF-8') +
'&password=' + EncodingUtil.urlEncode(password, 'UTF-8'));
Http http = new Http();
HTTPResponse res = http.send(req);
// testMethod1(res);
System.debug('BODY: '+res.toString());
System.debug('STATUS:'+res.getStatus());
System.debug('STATUS_CODE:'+res.getStatusCode());
String JsonString = JSON.serialize(res);
testMethod1(JsonString);
return JsonString;
}catch(Exception ex){
System.debug('Line Number '+ex.getLineNumber());
System.debug('Exception '+ex.getMessage());
return null;
}
}
}
I am making a Rest API call which results in select query (filter) of a list view. This works in rest client as i am making a seperate calls for token and then actual data(with access token as header).
But i am stuck in implementing the same in apex code. Please suggest where i am going wrong ?
public class TestApex {
@AuraEnabled
public static void testMethod1(String str){
try{
//testMethod1 tesM = new testMethod1();
Object obj = JSON.deserializeUntyped(str);
system.debug('Object :'+obj);
/*************************************************************************************************************/
HttpRequest req = new HttpRequest();
String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
String filterId = '00B6A000002Fc2BUAS';
//String filterId = '00B6A000002Fc0LUAS';
String endPoinURL = baseUrl+'/services/data/v41.0/sobjects/Account/listviews/'+filterId+'/describe';
req.setEndpoint(endPoinURL);
req.setHeader('Authorization', 'Bearer' + UserInfo.getSessionId());
req.setMethod('GET');
system.debug('UserInfo.getSessionId()' + UserInfo.getSessionId());
//ID tempAuth= '**************************************************************************bGUyNIK.obuwcoY'; System.debug('UserInfo.getSessionId()'+UserInfo.getSessionId());
//req.setHeader('Authorization', 'Bearer ' + tempAuth);
Http http = new Http();
HTTPResponse response = http.send(req);
// Map<String, Object> tokenResponse = (Map<String, Object>)
// JSON.deserializeUntyped(response.getBody());
system.debug('Response'+JSON.deserializeUntyped(response.getBody()));
/* String query = '';//(String) tokenResponse.get('query');
Map<String,String> columnData = new Map<String,String>();
for(String key : tokenResponse.keySet()){
if(key.contains('columns')){
List<Object> colList = (List<Object>)tokenResponse.get(key);
for(Object obj : colList){
Map<String,Object> colMap = (Map<String,Object>)obj;
if(colMap.containsKey('hidden') && !(Boolean)colMap.get('hidden')){
columnData.put(String.valueOf(colMap.get('label')),String.valueOf(colMap.get('fieldNameOrPath')));
}
}
}
}
for(String key : columnData.keySet()){
System.debug('Lable : '+key+' : Itag : '+columnData.get(key));
}
List<Account> accList = Database.query(query);
for(Account acc : accList){
}
*/
}catch(Exception ex){
System.debug('Line Number '+ex.getLineNumber());
System.debug('Exception '+ex.getMessage());
}
}
@AuraEnabled
public static String testMethod2(){
try{
system.debug('Inside testMethod2');
HttpRequest req = new HttpRequest();
String clientId = '3MVG982oBBDdw******************************************CO4g';
String username = '*************';
String password = '**********';
String clientSecret = '353*********************9';
// String loginUri = URL.getSalesforceBaseUrl().toExternalForm();
// String loginUri='https://test.salesforce.com/';
req.setMethod('POST');
// req.setEndpoint(loginUri+'/services/oauth2/token');
req.setEndpoint('https://test.salesforce.com/services/oauth2/token');
req.setBody('grant_type=password' +
'&client_id=' + clientId +
'&client_secret=' + clientSecret +
'&username=' + EncodingUtil.urlEncode(username, 'UTF-8') +
'&password=' + EncodingUtil.urlEncode(password, 'UTF-8'));
Http http = new Http();
HTTPResponse res = http.send(req);
// testMethod1(res);
System.debug('BODY: '+res.toString());
System.debug('STATUS:'+res.getStatus());
System.debug('STATUS_CODE:'+res.getStatusCode());
String JsonString = JSON.serialize(res);
testMethod1(JsonString);
return JsonString;
}catch(Exception ex){
System.debug('Line Number '+ex.getLineNumber());
System.debug('Exception '+ex.getMessage());
return null;
}
}
}