0

Below i the code :

Map<String,Object> poVals = 

    (Map<String,Object>)JSON.deserializeUntyped(POResponse); 
     system.debug('poVals' +poVals);
     lstPODetails = (List<object>)poVals.get('purchaseOrders'); 
     system.debug('lstPODetails' +lstPODetails);
     String poFormat = lstPODetails.get(0).FORMAT;

when trying to access a variable from 1st element of list in the last line, it says that variable doesn't exist : FORMAT What do you think is wrong here.

3

The Object type does not contain any attributes or methods by default. You need to cast it to a concrete type to access any properties on a particular instance. For instance, if you had a PurchaseOrderWrapper class defined as below, you would deserialize into a List<PurchaseOrderWrapper> instead of List<Object>.

public with sharing class PurchaseOrderWrapper
{
    final String format;
}

Instead of:

Map<String, Object> response = (Map<String, Object>)JSON.deserializeUntyped(payload);
List<Object> data = (List<Object>)response.get('purchaseOrders');

Use:

List<PurchaseOrderWrapper> data = 
    (List<PurchaseOrderWrapper>)response.get('purchaseOrders');

You can read more about the Object type in the Apex Developer Guide under Primitive Data Types:

Any data type that is supported in Apex. Apex supports primitive data types (such as Integer), user-defined custom classes, the sObject generic type, or an sObject specific type (such as Account). All Apex data types inherit from Object.

You can cast an object that represents a more specific data type to its underlying data type. For example:

Object obj = 10;
// Cast the object to an integer.
Integer i = (Integer)obj;
System.assertEquals(10, i);

The next example shows how to cast an object to a user-defined type—a custom Apex class named MyApexClass that is predefined in your organization.

Object obj = new MyApexClass();
// Cast the object to the MyApexClass custom type.
MyApexClass mc = (MyApexClass)obj;
// Access a method on the user-defined class.
mc.someClassMethod();

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.