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
ChrisYazChrisYaz 

Getting field values in Apex from an Object (not an Sobject)?

I am integrating with a managed package that returns an Object, not an Sobject, but a plain Object in Apex. Using System.Debug the following is shown:

    {nbrRows=1, rowIds=()}

I want to parse this returned data so that I can extract specific values from the object such as "nbrRows". I have already tried "JSON.deserializeUntyped" but due to the format not being typical JSON this did not work. I also tried "getPopulatedFieldsAsMap" but learn that this only works with SObjects, not an object.

Is there a simple way to parse the data above? Thanks in advance!
EllEll
You can make a custom class wrapper to handle this object if you know what data types it always has!

From the looks of it, looks like nbrRows is always an Integer and rowIds is probably a list of ID strings?
In that case you can make a wrapper class like:
public class ExampleWrapper {
  public Integer nbrRows;
  public List<String> rowIds;
}

Then you can cast the value as this type and get the values directly, i.e.
ExampleWrapper result = (ExampleWrapper) ManagedPackage.method();
This will try and 'force' the method to return as that type and if the data types match it should work.

If it returns a JSON string you'd be able to deserliaze it using your wrapper class too, like:
String jsonResult = ManagedPackage.method();
ExampleWrapper result = (ExampleWrapper) JSON.deserialize(jsonResult, ExampleWrapper.class);




 
Chris YaskoskiChris Yaskoski
Hi @Ell
I just tried this however when I attempt to cast I get this error:
Invalid conversion from runtime type Map<String,ANY> to MyClass.ExampleWrapper

This seems like I am already getting a Map back however when I try object.get('key') I see the compile error
Method does not exist or incorrect signature: void get(String) from the type Object