0

Been amending a piece of code to suit my needs but it won't compile. My naive 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.

4

0 回答 0