function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
prasanth kumarprasanth kumar 

rest api Json format throwing error

Hi, 

   I am just passing the name of the newly inserting account name to method and this method will create a new record in destination salesforce org(another salesforce org). But it is throwing error message.  Please help.

Error is:  Malformed JSON: Expected '{' at the beginning of object

 
**source ORG code:-**

   

   

     trigger SendAccount on Account(after insert)
    {
    for(Account a : Trigger.new){
       SendAccountFromSource.createAccount(a.Name, a.Id);
    }
    }
    
    
    
    Apex class:- 
    
    
    public class SendAccountFromSource {
     private final String clientId = '3MVG9ZL0ppGP5UrCXwgNBMST50bhCIRR_E_t7gUGb2TLvmA5wQD3YWiAOBnjOr7x5x1L2MB8G4frfx.oZRkOh';
     private final String clientSecret = '8220657863400185678';
     private final String username = 'prasanthkumar@gmail.com';
     private final String password = 'dolly1234';
     
     
     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();
      system.debug('The request body is: ' + reqbody);
      req.setBody(reqbody);
      req.setMethod('GET');
      req.setEndpoint('https://ap2.salesforce.com/services/oauth2/token');
      HttpResponse res = h.send(req);
      deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
      
      system.debug('The resp1.access_token is : ' +resp1.access_token+' The status code is: '+res.getstatuscode() +'....' +res.getstatus());
      
      
      return resp1.access_token;
      
      
     }
     @future(callout=true)
     public static void createAccount(String accName, String accId) 
     {
      SendAccountFromSource acount = new SendAccountFromSource();
      String accessToken = acount.ReturnAccessToken (acount);
     
      if(accessToken != null)
      {
      system.debug('inside the if statement :' );
       String endPoint = 'https://ap2.salesforce.com/services/apexrest/displayAccount/';
       System.debug('Account anme of ='+accname);
                   
                    
            
       Http h2 = new Http();
       HttpRequest req1 = new HttpRequest();
       String rename=accName;
      string rajas = '"'+rename+'"';
       req1.setHeader('Authorization','Bearer ' + accessToken);
       req1.setHeader('Content-Type','application/json');
       req1.setHeader('accept','application/json');
       
      
       req1.setBody('{"name" : rajas }');
       req1.setMethod('POST');
       req1.setEndpoint(endPoint);
       HttpResponse res1 = h2.send(req1);
       
       system.debug('The value of the res1 is :' +res1);
       deserializeResponse resp2 = (deserializeResponse)JSON.deserialize(res1.getbody(),deserializeResponse.class);
      system.debug('The value of resp2 :' +resp2);
       Account a = [SELECT Id FROM Account WHERE Id = :accId];
       a.externalId__c = resp2.id;
       update a;
      }
     }
     }