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 });
}
}