+ Start a Discussion
Manjunath reddy 25Manjunath reddy 25 

Trying to call out using REST services, not able to get response.

Hi I am trying to make a call out using rest services.the scenario is when I create an account in source org I should create an account in the target org.I have created a trigger SendAccount which calls the class SendAccountFromSource. Iam able to get access token as a response, but I am not able to get response when I use end point URL my debuglogs are given below, please anyone help me on this
trigger SendAccount on Account(after insert){
    for(Account a : Trigger.new){
            SendAccountFromSource.createAccount(a.Name, a.Id);        
     }
}
 
public class SendAccountFromSource {
    private final String clientId = '3MVG9Y6d_Btp4xp5pS0Mwb345aVwqNmCpRF9rEs1QMG_WkvGPZtIKsyUgujSZPhIcCzzxFQqugJCA7fs7SkZB';
    private final String clientSecret = '3594199692394096988';
    private final String username = 'manjunath@ceptes.com';
    private final String password = 'Surya@123SKIjxR3KtHFY6wv3uhLfbga0E';
public class deserializeResponse{
    public String id;
    public String access_token;
}
public String ReturnAccessToken (SendAccountFromSource acount){
    String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
system.debug('====================reqbody============'+reqbody);
req.setMethod('POST');
req.setEndpoint('https://ap1.salesforce.com/services/oauth2/token');
system.debug('====================req============'+req);
HttpResponse res = h.send(req);
system.debug('====================res============'+res);
    deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
system.debug('====================resp1============'+resp1);
system.debug('====================resp1.access_token============'+resp1.access_token);
String accessToken = resp1.access_token;
String accName = 'Shiva8';
return accessToken;
}
@future(callout=true)
public static void createAccount(String accName, String accId){
    system.debug('====================accId============'+accId);
    SendAccountFromSource acount = new SendAccountFromSource();
    system.debug('===acount == '+acount );
    String accessToken = acount.ReturnAccessToken (acount);
    if(accessToken != null){
    system.debug('====================accId============'+accId);
    String endPoint = 'https://ap1.salesforce.com/services/apexrest/createAccount/';
                       //https://ap1.salesforce.com/services/apexrest/v1/getaccountids/
    //String endPoint = 'https://ap1.salesforce.com/services/apexrest/v1/getaccountids/';
    //String jsonstr = '{"AccName" : "' + accName + '"}';
    String jsonstr = '{"Name" : "' + accName + '"}';
    
    Http h2 = new Http();
    HttpRequest req1 = new HttpRequest();
    req1.setHeader('Authorization','Bearer ' + accessToken); 
    req1.setHeader('Content-Type','application/json;charset=UTF-8');
    req1.setHeader('accept','application/json');
    req1.setBody('jsonstr');
    req1.setMethod('POST');
    req1.setEndpoint(endPoint);
    HttpResponse res1 = h2.send(req1);
       system.debug('====================res1============'+res1.getBody());
    String trimmedResponse = res1.getBody().unescapeCsv().remove('\\');
    system.debug('====================trimmedResponse ============'+trimmedResponse );
    deserializeResponse resp2 = (deserializeResponse)JSON.deserialize(trimmedResponse, deserializeResponse.class);
    system.debug('====================resp2 ============'+resp2);

    
    
    Account a = [SELECT Id FROM Account WHERE Id = :accId];
    //a.externalId__c= resp2.id;
    update a; 

       
  } 
}
}
16:55:38.17 (17241361)|CODE_UNIT_STARTED|[EXTERNAL]|01p280000054SO5|SendAccountFromSource.createAccount 16:55:38.17 (18294404)|USER_DEBUG|[30]|DEBUG|====================accId============0012800000peYgoAAE 16:55:38.17 (18506002)|USER_DEBUG|[32]|DEBUG|===acount == SendAccountFromSource:[clientId=3MVG9Y6d_Btp4xp5pS0Mwb345aVwqNmCpRF9rEs1QMG_WkvGPZtIKsyUgujSZPhIcCzzxFQqugJCA7fs7SkZB, clientSecret=3594199692394096988, password=Surya@123SKIjxR3KtHFY6wv3uhLfbga0E, username=manjunath@ceptes.com] 16:55:38.17 (18681163)|USER_DEBUG|[15]|DEBUG|====================reqbody============grant_type=password&client_id=3MVG9Y6d_Btp4xp5pS0Mwb345aVwqNmCpRF9rEs1QMG_WkvGPZtIKsyUgujSZPhIcCzzxFQqugJCA7fs7SkZB&client_secret=3594199692394096988&username=manjunath@ceptes.com&password=Surya@123SKIjxR3KtHFY6wv3uhLfbga0E 16:55:38.17 (18780827)|USER_DEBUG|[18]|DEBUG|====================req============System.HttpRequest[Endpoint=https://ap1.salesforce.com/services/oauth2/token, Method=POST] 16:55:38.17 (18859108)|CALLOUT_REQUEST|[19]|System.HttpRequest[Endpoint=https://ap1.salesforce.com/services/oauth2/token, Method=POST] 16:55:38.17 (488682227)|CALLOUT_RESPONSE|[19]|System.HttpResponse[Status=OK, StatusCode=200] 16:55:38.17 (488841703)|USER_DEBUG|[20]|DEBUG|====================res============System.HttpResponse[Status=OK, StatusCode=200] 16:55:38.17 (489959480)|USER_DEBUG|[22]|DEBUG|====================resp1============deserializeResponse:[access_token=00D90000000gZ83!AQEAQKB7bYRQZObCuq7fvGfN8p9xc9UjvF2hvRmsc5xFc778E6IPDYfqeFCJxIAjet8vdhk8rzk2Rgnw5VngAWBcv.JypYsv, id=https://login.salesforce.com/id/00D90000000gZ83EAE/005900000014zatAAA] 16:55:38.17 (490013080)|USER_DEBUG|[23]|DEBUG|====================resp1.access_token============00D90000000gZ83!AQEAQKB7bYRQZObCuq7fvGfN8p9xc9UjvF2hvRmsc5xFc778E6IPDYfqeFCJxIAjet8vdhk8rzk2Rgnw5VngAWBcv.JypYsv 16:55:38.17 (490065182)|USER_DEBUG|[35]|DEBUG|====================accId============0012800000peYgoAAE 16:55:38.17 (492632717)|CALLOUT_REQUEST|[50]|System.HttpRequest[Endpoint=https://ap1.salesforce.com/services/apexrest/createAccount/, Method=POST] 16:55:38.17 (688726311)|CALLOUT_RESPONSE|[50]|System.HttpResponse[Status=Bad Request, StatusCode=400] 16:55:38.17 (688871235)|USER_DEBUG|[51]|DEBUG|====================res1============[{"message":"Unexpected character ('j' (code 106)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [line:1, column:2]","errorCode":"JSON_PARSER_ERROR"}] 16:55:38.17 (689407452)|USER_DEBUG|[53]|DEBUG|====================trimmedResponse ============[{"message":"Unexpected character ('j' (code 106)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [line:1, column:2]","errorCode":"JSON_PARSER_ERROR"}] 16:55:38.17 (689816529)|FATAL_ERROR|System.JSONException: Malformed JSON: Expected '{' at the beginning of object Class.System.JSON.deserialize: line 15, column 1 Class.SendAccountFromSource.createAccount: line 54, column 1 16:55:38.17 (689839357)|FATAL_ERROR|System.JSONException: Malformed JSON: Expected '{' at the beginning of object Class.System.JSON.deserialize: line 15, column 1 Class.SendAccountFromSource.createAccount: line 54, column 1
SandhyaSandhya (Salesforce Developers) 
Hi,

I see fro your debug that you have the problem in JSON. And also when I used below link to validate your JSON it is throwing error.

http://jsonlint.com/
 
I suggest you focus on that and try to give proper JSON format.

Hope this helps you!

Thanks and Regards
Sandhya
Manjunath reddy 6Manjunath reddy 6
Hi Sandhya, 

Thanks for your response,I am getting debug logs as below, can you please check and let me know, If I can change something in my code.

35.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO 14:55:27.0 (124926)|USER_INFO|[EXTERNAL]|00528000000JHso|manjunath@riverdale.com|India Standard Time|GMT+05:30 14:55:27.0 (172265)|EXECUTION_STARTED 14:55:27.0 (179071)|CODE_UNIT_STARTED|[EXTERNAL]|FutureHandler - state load 14:55:27.0 (3536604)|CODE_UNIT_FINISHED|FutureHandler - state load 14:55:27.0 (5225203)|EXECUTION_FINISHED 14:55:27.18 (18997120)|USER_INFO|[EXTERNAL]|00528000000JHso|manjunath@riverdale.com|India Standard Time|GMT+05:30 14:55:27.18 (19040537)|EXECUTION_STARTED 14:55:27.18 (19048391)|CODE_UNIT_STARTED|[EXTERNAL]|01p280000054SO5|SendAccountFromSource.createAccount 14:55:27.18 (19641940)|USER_DEBUG|[30]|DEBUG|====================accId============0012800000poFWBAA2 14:55:27.18 (19891297)|USER_DEBUG|[32]|DEBUG|===acount == SendAccountFromSource:[clientId=3MVG9Y6d_Btp4xp5pS0Mwb345aVwqNmCpRF9rEs1QMG_WkvGPZtIKsyUgujSZPhIcCzzxFQqugJCA7fs7SkZB, clientSecret=3594199692394096988, password=Sangeetha@123KVrdjQ3fe0lRYus68saUXiMm, username=manjunath@ceptes.com] 14:55:27.18 (20055491)|USER_DEBUG|[15]|DEBUG|====================reqbody============grant_type=password&client_id=3MVG9Y6d_Btp4xp5pS0Mwb345aVwqNmCpRF9rEs1QMG_WkvGPZtIKsyUgujSZPhIcCzzxFQqugJCA7fs7SkZB&client_secret=3594199692394096988&username=manjunath@ceptes.com&password=Sangeetha@123KVrdjQ3fe0lRYus68saUXiMm 14:55:27.18 (20181511)|USER_DEBUG|[18]|DEBUG|====================req============System.HttpRequest[Endpoint=https://ap1.salesforce.com/services/oauth2/token, Method=POST] 14:55:27.18 (20275504)|CALLOUT_REQUEST|[19]|System.HttpRequest[Endpoint=https://ap1.salesforce.com/services/oauth2/token, Method=POST] 14:55:27.18 (398535789)|CALLOUT_RESPONSE|[19]|System.HttpResponse[Status=OK, StatusCode=200] 14:55:27.18 (398671245)|USER_DEBUG|[20]|DEBUG|====================res============System.HttpResponse[Status=OK, StatusCode=200] 14:55:27.18 (399625165)|USER_DEBUG|[22]|DEBUG|====================resp1============deserializeResponse:[access_token=00D90000000gZ83!AQEAQKLShcbkNlnhVQ05ZdOpP8Yoe_n.24EMvUBRs7TxXgyPKJ1cvND_w9HpNnl0DCjBIgvUaF0vlhveQQNUHQMdKSMh5H_9, id=https://login.salesforce.com/id/00D90000000gZ83EAE/005900000014zatAAA] 14:55:27.18 (399657425)|USER_DEBUG|[23]|DEBUG|====================resp1.access_token============00D90000000gZ83!AQEAQKLShcbkNlnhVQ05ZdOpP8Yoe_n.24EMvUBRs7TxXgyPKJ1cvND_w9HpNnl0DCjBIgvUaF0vlhveQQNUHQMdKSMh5H_9 14:55:27.18 (399683572)|USER_DEBUG|[35]|DEBUG|====================accId============0012800000poFWBAA2 14:55:27.18 (402690848)|CALLOUT_REQUEST|[50]|System.HttpRequest[Endpoint=https://ap1.salesforce.com/services/apexrest/createAccount/, Method=POST] 14:55:27.18 (477431867)|CALLOUT_RESPONSE|[50]|System.HttpResponse[Status=Not Found, StatusCode=404] 14:55:27.18 (477582872)|USER_DEBUG|[51]|DEBUG|====================res1============[{"errorCode":"NOT_FOUND","message":"Could not find a match for URL /createAccount/"}] 14:55:27.18 (477755560)|USER_DEBUG|[55]|DEBUG|====================trimmedResponse ============[{"errorCode":"NOT_FOUND","message":"Could not find a match for URL /createAccount/"}] 14:55:27.18 (478102605)|FATAL_ERROR|System.JSONException: Malformed JSON: Expected '{' at the beginning of object Class.System.JSON.deserialize: line 15, column 1 Class.SendAccountFromSource.createAccount: line 57, column 1 14:55:27.18 (478116572)|FATAL_ERROR|System.JSONException: Malformed JSON: Expected '{' at the beginning of object