0

I have a JSON response I receive from an endpoint, it is then deserialized, and all of the objects are stored in a custom object list (stored in the list variable "scope").

I am trying to place this list of custom objects into a map, however the objects have not been inserted it, and may not be inserted, how do I accomplish this since they do not have IDs? I am thinking I could iterate through the scope objects and assign incremental values?

The error I receive (which to my understanding is because the objects haven't been inserted and they do not have IDs):

Row with null Id at index: 0

Map<ID, Custom_Object__c> scopeAccountMap = new Map<ID, Custom_Object__c>(scope);

  system.debug(scopeAccountMap.keyset());

    for (ID idKey : scopeAccountMap.keyset()) {
        Customer_Account__c a = scopeAccountMap.get(idKey);
        Map<String, Object> fieldsToValue = a.getPopulatedFieldsAsMap();
    }
2
  • 2
    Why do you need to use a Map at all? The code you include here could just as well iterate over a List, and the fact that your data seems to lack a unique identifier militates against using a Map. – David Reed Jun 6 '18 at 20:00
  • Good point. I wan running with somebody else's example with getting all of the fields for particular objects. Essentially ditch this first map, and iterate through the List with a < List.size() and create maps only for my fields piece sounds like the way to go. – S.B. Jun 6 '18 at 20:12
0

Remove the map and replace with a list as David Reed mentioned in his comment.

In your previous question(getSObject - Dynamically Referencing Object Fields from Map) you are already iterating through this list of Custom Objects.

You can reference these objects with the dynamic field names with scope[s].get(fieldName) where fieldName is every key in your fieldToValue Map. Ex: for (String fieldName : fieldsToValue.keySet()) {...

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.