-1

Trying to convert some JSON to an Account List, I suspect my error is because the query next stuff coming into the response. How can I convert these results to an account list?

HttpRequest feedRequest = new HttpRequest();
feedRequest.setEndpoint('callout:sandwich/services/data/v46.0/query/?q=SELECT+Id+FROM+Account');
feedRequest.setMethod('GET');
Http http = new Http();
HTTPResponse feedResponse = http.send(feedRequest);

String s = feedresponse.getBody();
List<Account> accounts = (List<Account>) JSON.deserialize(s, List<Account>.class);
3

The response body from the sObject Query endpoint is not a record list. Here's the example response body from the linked documentation:

{
    "done" : true,
    "totalSize" : 14,
    "records" : 
    [ 
        {  
            "attributes" : 
            {    
                "type" : "Account",    
                "url" : "/services/data/v20.0/sobjects/Account/001D000000IRFmaIAH"  
            },  
            "Name" : "Test 1"
        }, 
        {  
            "attributes" : 
            {    
                "type" : "Account",    
                "url" : "/services/data/v20.0/sobjects/Account/001D000000IomazIAB"  
            },  
            "Name" : "Test 2"
        }, 

        ...

    ]
}

It's actually a dictionary, with a nested sObject list. If you want to use strongly-typed deserialization, you'll have to define an outer wrapper object to represent that top-level dictionary, something like

class Response {
    public Boolean done;
    public Integer totalSize;
    public List<Account> records;
}
  • Thank you, I still am having trouble with how to get my results into the Account List, can you show an example strongly-typed deserialization line if you can – All about the W Sep 20 '19 at 23:56
  • 1
    @AllabouttheW You already have most of the code you need. You just need to deserialize into a class like Response above, rather than a List<Account>. – David Reed Sep 20 '19 at 23:57
  • I think I am close, sorry to be needed. Where I am confused is response class is not a list, but a object with a list inside of it. So I need to make it work like this? List<Account> accounts = (Response) JSON.deserialize(s, Response.List<Accounts>.class); – All about the W Sep 21 '19 at 0:01
  • 1
    @AllabouttheW You need to declare a variable of type Response. Then, you need to call JSON.deserialize() with the second parameter Response.class. Finally, you can access your Accounts via the records property of the returned Response. – David Reed Sep 21 '19 at 0:07
3

You can see what the return looks like in workbench. It's not returning a list of Accounts which is why you're not able to deserialize the way you're attempting. You can create a custom class (AccountQueryWrapper) to deserialize the response

public class AccountQueryWrapper { public Integer totalSize; public boolean done; public List records; } The records will contain your account Ids.

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.