1

What I am trying to do is to update a child object field (downloads__c) in each of the child records in the child object (Data__c) record collection, with the value of a field (downloads__c) from its parent object (Parent__c). The parent object also has the field count__c.

Once all the records are updated in the child collection, do a dml insert after the loop. Can't seem to get this to work. Hope I have explained correctly.

jsonBody = '[{"count__c":"45","downloads__c":"30"},{"count__c":"40","downloads__c":"20"}]'; // child records to be updated

// Querry parent 
List<Parent__c> parentList = [SELECT Id, Name FROM Parent__c 
                WHERE count__c IN :countList]; // countList will have unique values of count__c from the child json records

// child object Data__c records coming from json
List<Data__c> dList = (List<Data__c>) System.JSON.deserialize(jsonBody, List<Data__c>.class);

Map<String, List<Data__c>> mapCountByData = new Map<String, List<Data__c>>();
for(Data__c d : dList) {
    if(mapCountByData.containsKey(d.count__c)) {
       List<Data__c> lstData = mapCountByData.get(d.count__c);
       lstData.add(d);
       mapCountByData.put(d.count__c, lstData);
    } else {
        mapCountByData.put(d.count__c, new List<Data__c> { d });
    }
}

2 Answers 2

1

Maps are the way to go here,

List<Data__c> dList = (List<Data__c>) System.JSON.deserialize(jsonBody, List<Data__c>.class);
Map<String, List<Data__c>> mapCountByData = new Map<String, List<Data__c>>();
for(Data__c d : dList) {
    if(mapCountByData.containsKey(d.count__c)) {
       List<Data__c> lstData = mapCountByData.get(d.count__c);
       lstData.add(d);
       mapCountByData.put(d.count__c, lstData);
    } else {
        mapCountByData.put(d.count__c, new List<Data__c> { d });
    }
}

Then you can get all the counts using,

set<integer> setCounts = mapCountByData.keyset();

this will also not have duplicates and you can use the set in the IN clause in SOQL.

Later you can also make use of the mapCountByData to get the list of Data__c records.

5
  • Thats corrct, thanks. But I get these errors when saving with red squiggly lines on put and lstData: Method does not exist or incorrect signature: void put(String, List<Data__c>) from the type Map<String,Data__c> Illegal assignment from Data__c to List<Data__c> Method does not exist or incorrect signature: void put(String, List<Data__c>) from the type Map<String,Data__c>
    – Col
    Sep 3, 2021 at 4:46
  • Just fixed a typo. It’s map with keys as integer Sep 3, 2021 at 12:43
  • I had updated the json. count value is string.
    – Col
    Sep 3, 2021 at 12:47
  • Just updated, check it out Sep 3, 2021 at 12:49
  • Yes, that worked! But I can't seem to update a child field based on it parents field value. I have updated the original post above.
    – Col
    Sep 3, 2021 at 16:55
0

You can use a Set or a List. Sets are preferred.

Set<Integer> values = new Set<Integer>();

for(Data__c d : dList){
  values.add(d.count__c);
}

Note that Count__c appears to be a number, so we use a number data type. If the actual field is a String, conversion is necessary:

Set<String> values = new Set<String>();

for(Data__c d : dList){
  values.add(String.valueOf(d.count__c));
}
1
  • My bad. Count was string. Updated json
    – Col
    Sep 3, 2021 at 12:41

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.