1

I just followed a trailhead on callouts and I am trying to get some values from an endpoint. My debug shows me my status is ok but I get an error on line 17:

Callout error Invalid conversion from runtime type List to Map

Anonymouse window

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('http://api.sandbox.test.com/v1/agencies');
request.setMethod('GET');

request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
request.setHeader('Content-Type', 'application/json');
request.setHeader('X-test-APIKEY', 'xxxxxxxxxxxxxx');

HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    System.debug('OK');
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'agencies' key as a list
    List<Object> agencies = (List<Object>) results.get('agencies');
    System.debug('Received the following:');
    for (Object agency: agencies) {
        System.debug(agency);
    }
}

My result should be similar to below

[
  {
    "uid": "72352a55-5be4-496e-804a-fc3kl261f0a4",
    "name": "Agency 1"
  },
  {
    "uid": "62352a55-5487-496e-804a-fc3klf48f0a4",
    "name": "Agency 2"
  }
]

2 Answers 2

4

Because you are getting List with Json, So you need to parse it into List

List<Object> results = (List<Object>) JSON.deserializeUntyped(response.getBody());
for (Object agency: results) {
    System.debug((Map<String, Object>)agency); //Then you can parse it into key value pair
}
0
2
WebService static boolean checkIfTLDExists(id contractID, String authProvider)
{
    //Id remoteTLDId;
    List<Contract> contractsToSend = [Select id, Status, StartDate,A_Label__c,String__c from Contract WHERE id = :contractID];
    String tldName ;
    if (contractsToSend.size() != 1) {
        return false;
    }
    Contract contractToSend = contractsToSend[0];
    if(contractToSend.A_Label__c != null && contractToSend.A_Label__c.length() > 0)
    {
        tldName = contractToSend.A_Label__c.toLowerCase();
    }
    else
    {
        tldName = contractToSend.String__c.toLowerCase();
    }
    Http http = new Http();
    HttpRequest getTLDRequest = new HttpRequest();
    //Reference OAuth2 NamedCredential for endpoint and generated authorization header
    getTLDRequest.setEndpoint('callout:'+authProvider+'/services/data/v34.0/query/?q=SELECT+id+from+TLD__c+where+Name+=+\''+tldName+'\'');
    getTLDRequest.setMethod('GET');
    // increasing timeout to 30 secs for httprequest.
    getTLDRequest.setTimeout(30000);
    getTLDRequest.setHeader('Content-Type', 'application/json');
    HTTPResponse restld = http.send(getTLDRequest);
    list<String> tldIds=new List<String>();
    System.debug('@@@@restld'+restld.getBody());

    //List<Object> tldresults = (List<Object>) JSON.deserializeUntyped(restld.getBody());
    //for (Object tldresult: tldresults) {
    //  System.debug((Map<String, Object>)tldresult); //Then you can parse it into key value pair
    //}

    Map<String, Object> tldresults = (Map<String, Object>)JSON.deserializeUntyped(restld.getBody());
    if((Double)tldresults.get('totalSize')>0)
    {
        List<Object> lstTLDs = (List<Object>)tldresults.get('records'); 
        // List<Object> lstTLDs = (List<Object>)JSON.deserializeUntyped(restld.getBody());
        for (Object onetld: lstTLDs) 
        {   
            Map<String, Object> tldAttributes= (Map<String, Object>)onetld;
            tldIds.add((String)tldAttributes.get('Id'));
        }
        if(tldIds.size()>0)
        {
            return true;
        }
    }
    return false;
} 

I am getting error :- Callout error Invalid conversion from runtime type List to Map

please help

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .