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
Eric Blaxton 11Eric Blaxton 11 

Apex Json return error

Hi and thanks in advance for any help provided.

This is my second HTTP call. The call works, but I am confused on the stucture of the JSON.  I can successfully work with the "childWorkOrders", but I need to access the "workOrder", which is the Parent.  How would I do that?

JSON return:
{
    "status": "New",
    "shortDescription": "xxx",
    "description": "* 11/11/2020 13:12:53 CST  (SVCPOXPO) * xxx",
    "openedOn": "2020-11-11T13:12:53",
    "closeOn": "0002-11-30T00:00:00",
    "workOrder": {
        "parentWorkOrder": null,
        "assignedTo": "xxx",
        "openedOn": "2020-11-11T13:12:53",
        "lastUpdatedOn": "2020-11-11T13:44:20",
        "closeOn": "0002-11-30T00:00:00",
        "shortDescription": "xxx",
        "description": "xxx",
        "status": "Assigned",
        "workOrderNumber": "000007018372",
        "vendorName": null,
        "chargeBack": "0.00 ",
        "invoice": null
    },
    "childWorkOrders": [
        {
            "parentWorkOrder": "000007018372",
            "assignedTo": "xxx",
            "openedOn": "2020-11-11T15:18:59",
            "lastUpdatedOn": "2020-11-11T15:19:01",
            "closeOn": "0002-11-30T00:00:00",
            "shortDescription": "Dispenser",
            "description": null,
            "status": "Assigned",
            "workOrderNumber": "000007018381",
            "vendorName": null,
            "chargeBack": "0.00 ",
            "invoice": null
        }
    ]
}



APEX snippet:
HTTP h2 = new HTTP();
 HTTPRequest req2 = new HTTPRequest(); 
 // This gets the work orders.  The trick is to get the Maintenance Ticket ID into the ().  It would have to be a loop or something

  req2.setEndpoint(qaEndpoint + '(78401)/GetWorkOrders'); 
  req2.setTimeout(120000);       
  req2.setMethod('GET');        
 // QA Authorization
  req2.setHeader('Authorization', 'Bearer ' + tokenkey);  
  HTTPResponse res2 = h2.send(req2);
  String strResponse2 = res2.getBody();  
  if(res2.getstatusCode() == 200 && res2.getbody() != null)
            system.debug('Response: ' + res2.getBody());
        {           
            Map<String,Object> newMap2 = (Map<String, Object>)JSON.deserializeUntyped(strResponse2); //strResponse gets top level and lower level details            
           List<Object > valuelist2 = (List<Object>) newMap2.get('workOrder');            
              
            system.debug('valuelist2: ' + valuelist2);
        
            //Pass values to create/upsert cases (public class createMaintCase)
            //createMaintCase.upsertCases(valuelist);
            
         return null;
        } // end 2nd HTTPRequest
Error:  From line:
List<Object > valuelist2 = (List<Object>) newMap2.get('workOrder'); 

System.TypeException: Invalid conversion from runtime type Map<String,ANY> to List<String>

Regards,
Eric
 

 
AnudeepAnudeep (Salesforce Developers) 
Hi Eric - Can you try using (Map<String, Object>)m.get('workOrder');

This is based on the sample given in the documentation
 
String jsonInput = '{\n' +
    ' "description" :"An appliance",\n' +
    ' "accessories" : [ "powerCord", ' + 
      '{ "right":"door handle1", ' + 
        '"left":"door handle2" } ],\n' +
    ' "dimensions" : ' + 
      '{ "height" : 5.5 , ' + 
        '"width" : 3.0 , ' + 
        '"depth" : 2.2 },\n' +
    ' "type" : null,\n' +
    ' "inventory" : 2000,\n' +
    ' "price" : 1023.45,\n' +
    ' "isShipped" : true,\n' +
    ' "modelNumber" : "123"\n' +
    '}';
    
Map<String, Object> m = 
   (Map<String, Object>)
      JSON.deserializeUntyped(jsonInput);

System.assertEquals(
   'An appliance', m.get('description'));
        
List<Object> a = 
   (List<Object>)m.get('accessories');
System.assertEquals('powerCord', a[0]);        
Map<String, Object> a2 = 
   (Map<String, Object>)a[1];
System.assertEquals(
   'door handle1', a2.get('right'));
System.assertEquals(
   'door handle2', a2.get('left'));

Map<String, Object> dim = 
   (Map<String, Object>)m.get('dimensions');
System.assertEquals(
   5.5, dim.get('height'));
System.assertEquals(
   3.0, dim.get('width'));
System.assertEquals(
   2.2, dim.get('depth'));
        
System.assertEquals(null, m.get('type'));
System.assertEquals(
   2000, m.get('inventory'));
System.assertEquals(
   1023.45, m.get('price'));
System.assertEquals(
   true, m.get('isShipped'));
System.assertEquals(
   '123', m.get('modelNumber'));

Let me know if it helps. If it does, please mark this answer as Best. It may help others in the community. Thank You!