You need to sign in to do that
Don't have an account?

Compile error on rollup summary apex trigger
Been amending a piece of code on here to suit my needs but it won't compile. My naieve eyes cannot see the error of my ways:
trigger doRollup on Time_Record__c (after insert, after update, after delete, after undelete) { // List of parent record ids to update Set<Id> parentIds = new Set<Id>(); // In-memory copy of parent records Map<Id,Time_Record__c> parentRecords = new Map<Id,Time_Record__c>(); // Gather the list of ID values to query on for(Daily_Time_Record__c c:Trigger.isDelete?Trigger.old:Trigger.new) parentIds.add(c.Time_Record_Link__c); // Avoid null ID values parentIds.remove(null); // Create in-memory copy of parents for(Id parentId:parentIds) parentRecords.put(parentId,new Time_Record__c(Id=parentId,RollupTarget__c=0)); // Query all children for all parents, update Rollup Field value for(Daily_Time_Record__c c:[select id,FieldToRoll__c,Time_Record_Link__c from Daily_Time_Record__c where id in :parentIds]) parentRecords.get(c.Time_Record_Link__c).RollupTarget__c += c.FieldToRoll__c; // Commit changes to the database Database.update(parentRecords.values()); }
Where:
Time_Record__c is my parent
RollUpTarget__c is the place I am trying to roll to
Daily_Time_Record__c is the child
Time_Record_Link__c is the link to parent that exists on the child
FieldToRoll__c is a test field to roll up on the child
I *think* that is what I had to replace from this generic template:
trigger doRollup on Child__c (after insert, after update, after delete, after undelete) { // List of parent record ids to update Set<Id> parentIds = new Set<Id>(); // In-memory copy of parent records Map<Id,Parent__c> parentRecords = new Map<Id,Parent__c>(); // Gather the list of ID values to query on for(Child__c c:Trigger.isDelete?Trigger.old:Trigger.new) parentIds.add(c.ParentField__c); // Avoid null ID values parentIds.remove(null); // Create in-memory copy of parents for(Id parentId:parentIds) parentRecords.put(parentId,new Parent__c(Id=parentId,RollupField__c=0)); // Query all children for all parents, update Rollup Field value for(Child__c c:[select id,Amount__c,ParentField__c from Child__c where id in :parentIds]) parentRecords.get(c.ParentField__c).RollupField__c += c.Amount__c; // Commit changes to the database Database.update(parentRecords.values()); }
Ideally I want to roll up a sum of 7 fields from the child (hence needing this solution), but starting small.
Ok.
I should have put the trigger on the child. It saves, but it does nothing. I think this is because I have not set parentIDs to anything relevant to my org.
EDIT:
actually I am not sure of that. I think that the SoQL at the end must not be finding any matches. Unsure as to why and a lack of a decent debug makes this a bit difficult to bottom out.
All I can imagine is that ParentIDs is empty of things to match, or is a mismatch.
Ok I sorted this in the end, it was a small error in the SoQL.
Next step is making test code, which I have never done before. The spanner here is the presence of master/detail.