You need to sign in to do that
Don't have an account?
Update a child record field with a value of a field of its corresponding parent
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.
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 List<Data__c> dList = (List<Data__c>) System.JSON.deserialize(jsonBody, List<Data__c>.class); countList has unique count__c values, say 45,40 // to use in the IN clause. // Querry parent for those plan ids in daily data json List<Parent__c> parentList = [SELECT Id, Name FROM Parent__c WHERE count__c IN :countList]; // Loop through the list of returned parent records - outer loop for(Parent__c parentRecords : parentList) { // Loop through dList - inner loop for(Data__c dRecords : dList) { // look for matching count__c in parent // update corresponding downloads__c in the child record with its parents downloads__c value } } Should I use maps, if so how? // dml insert of child collection
Use the below code.
All Answers
Use the below code.