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
Avinash AAvinash A 

how to pass Account object from rest callout to rest webservice, Please Help me..

In the given code below i am passing json string 'name' in to http post method.In the rest callout mthod i serialized Account object in to JSON String.When i try to deserislize it in HttpPost method Im getting JSON parser error, Can anyone Please Help Me??

//This is the rest webservice
@Httppost
    Global static Account doPost(String name){
        //System.debug(name);
     Account tn=(Account)JSON.deserialize(name, Account.class);
    
        insert tn;Error message
        
        return tn;
        
    }
Best Answer chosen by Avinash A
Ashish KumarAshish Kumar
Could we connect on any other channel if it is okay for you to share the code? We can connect on zoom meetings.
Or you can do one thing, since in post method debug will not get captured.. you can use  below trick to see what's coming in json.
@Httppost
    Global static Account doPost(String name){
        //System.debug(name);
Account ac = new Account();
ac.Name = name ;
insert ac;
//check newly created account object to verify what is coming in parameters. 
//comment below code.
    // Account tn=(Account)JSON.deserialize(name, Account.class);
    
      //  insert tn;        
      //  return tn;
return null;
        
    }

I guess the problem is that you are setting everything in request body so you may need to something like below 
@Httppost
    Global static Account doPost(String name){
        //System.debug(name);
RestRequest req = RestContext.request;
String requestJson = req.requestBody.tostring();
     Account tn=(Account)JSON.deserialize(requestJson , Account.class);
      insert tn;        
        return tn;
        
    }

I guess the above will work properly. Let me know if it helps.

Regards,
Ashish Kr.
 

All Answers

Ashish KumarAshish Kumar

Hi Avinash,

Could you please post the json you are passing? 
The json you are getting in the post method must follow the below structure.
 

{"attributes":{"type":"Account","url":"/services/data/v41.0/sobjects/Account/001f4000002rsgKAAQ"},"Name":"GenePoint","Id":"001f4000002rsgKAAQ"}

If your json is not in the above form then you can use Json.deserializeUntyped method to create a map and then you can retireive the required parameters to create the account records.
Let me know if you need further help.

Regards,
Ashish Kr.

Avinash AAvinash A
// This is how im sending data from apex callout in JSON FORMAT
Account a=new Account (name='test account name');
        String ac=JSON.serialize(a);
        request1.setBody(ac);
        system.debug(ac);

can you please help me using  Json.deserializeUntyped method to create a map and retrive the required parameters.
Ashish KumarAshish Kumar
Hi Avinash, Could you please put a debug point to capture the json coming in Post Method? Does it look like below one? {"attributes":{"type":"Account"},"Name":"test account name"} Regards, Ashish Kr. Ashish
Avinash AAvinash A
DEBUG|{"attributes":{"type":"Account"},"Name":"test account name"}

I am getting this when i put DEBUG in the callout,
but if i put DEBUG in HttpPost ,its not getting executed..
Can you please help me out??
Ashish KumarAshish Kumar
Hi Avinash, Could you please let me know if you are making the callout from the same org or from different org. If from same org then i guess you can do it by just calling a function and assuming if you are calling it from different org then are you providing proper authentication system by passing the token in the request? I guess you may need to check out this post: https://developer.salesforce.com/forums/?id=906F000000099zbIAA Regards, Ashish Kr. Ashish
Avinash AAvinash A
Hi Ashish ,

Im making callout from a different org. My Rest Web Service does exist in a different org.And I have followed proper authentication process .
Still getting error.Could u please assist.
Thank you
Ashish KumarAshish Kumar
Could we connect on any other channel if it is okay for you to share the code? We can connect on zoom meetings.
Or you can do one thing, since in post method debug will not get captured.. you can use  below trick to see what's coming in json.
@Httppost
    Global static Account doPost(String name){
        //System.debug(name);
Account ac = new Account();
ac.Name = name ;
insert ac;
//check newly created account object to verify what is coming in parameters. 
//comment below code.
    // Account tn=(Account)JSON.deserialize(name, Account.class);
    
      //  insert tn;        
      //  return tn;
return null;
        
    }

I guess the problem is that you are setting everything in request body so you may need to something like below 
@Httppost
    Global static Account doPost(String name){
        //System.debug(name);
RestRequest req = RestContext.request;
String requestJson = req.requestBody.tostring();
     Account tn=(Account)JSON.deserialize(requestJson , Account.class);
      insert tn;        
        return tn;
        
    }

I guess the above will work properly. Let me know if it helps.

Regards,
Ashish Kr.
 
This was selected as the best answer
Avinash AAvinash A
Now its working fine.Thanks a lot Ashish,