0

I have a Wrapper object that I'm using to store values via calculations and callouts. I want to map these values to a custom object's fields. I also have a custom metadata type where I'm storing the mapping of Wrapper object attribute and Custom object field.

The requirement is that I iterate over the key-value pairs of the wrapper object and assign the value to the custom object field that corresponds to that particular wrapper attribute. I do not want to do a hardcoded mapping within my code, and just want to reference the setting to handle the mapping.

I'm so far unable to get my wrapper object converted to Key-value pairs. Any typecasting I do, I'm unable to access the attributes on the wrapper object.

Is there a better way to achieve this?

3
  • 1
    Why are you using a wrapper object? It sounds like what you want is a Map, which natively provides the key-value store you need. Commented May 18, 2020 at 5:17
  • The purpose of the wrapper is to store response from a callout and also some calculations. The wrapper gets its values from response after some manipulation. Commented May 18, 2020 at 5:31
  • Also, I am using the standard sObject.put(FIELDNAME_HERE, VALUE_HERE). VALUE_HERE will be derived from a map of string and Object. String holds the external Id, Object will be wrapper. Each key will give me access to the wrapped Object. The Object's attributes and the custom object's fields are mapped in a setting. I am trying to iterate over the setting and schema.SObjectField, and map values via the .put Commented May 18, 2020 at 5:37

2 Answers 2

1

The Object class doesn't have a way to access anything dynamically. It only has three known methods, toString(), equals() and hashCode(), all of which misbehave most of the time, and wouldn't help you anyways.

What you can do is use JSON.deserializeUntyped, which will give you a close approximation of what you want.

sObject[] records = new sObject[0];
Map<String, Object> extIdMap = (Map<String, Object>)JSON.deserializeUntyped(jsonString);
for(String externalId: extIdMap.keySet()) {
  Map<String, Object> recordValues = (Map<String, Object>) extIdMap.get(externalId);
  ...
}

Note that this has some limitations, like it won't automatically parse date/datetime values (since they appear as string values), but this will work for the normal primitives Boolean, String, and Integer/Decimal.

4
  • Hi @sfdcfox, I can now reduce my first level map. I am now trying to work only with Maps and have removed my wrapper references. The issue is, I have some non-primitive objects as well, and this is not letting me cast to Map<string, List<Object>>. I want this structure as it will house my keys and Objects. Any idea what can be done differently? Commented May 20, 2020 at 5:18
  • For reference, the JSON I want to convert: {"arrayAttribute":[{"attr1":"val1","attr2":"val2","attr3":"val4","attr4":false,"times":[{"endTime":"2020-05-14T10:00:00Z","startTime":"2020-05-14T09:00:00Z"}],"key_attribute_I_want":"key001"},{"attr1":"val1","attr2":"val2","attr3":"val3","attr4":false,"attr5":false,"times":[{"endTime":"2020-05-26T11:00:00Z","startTime":"2020-05-26T10:00:00Z"}],"key_attribute_I_want":"key002"}]} Commented May 20, 2020 at 5:20
  • @ApexSrinivas You can't use complex types with the untyped version; you have to cast to each object, so first to Map<String, Object>, then cast the object to a List<Object>, and finally to a Map<String, Object> again. Commented May 20, 2020 at 5:35
  • The first reply solved my fundamental issue. I'm now doing a series of serilizations and deserializations to get the desired structure. It is now in the form I can work with further. Thank you! Commented May 20, 2020 at 6:27
0

I achieved this with several successive serializations and deserializations into structures I wanted. That seems to be the only thing that worked for me when converting JSON objects into workable Object entities and handling the entire field mapping separately through a custom metadata type. So I can now add or remove fields and attributes that I want to store without having to change code or add logic.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.