答案 0 :(得分:0)
这是Apex的一个非常直接的用例。您需要在子对象上创建一个Trigger,它将执行数学运算并更新父对象。 最佳实践规定Trigger实际上应该在类中调用方法,但这是解决方案的缩写版本:
trigger myTrigger on Collateral__c (after insert, after update){
Map<Id, Deal__c> dealMap = new Map<Id, Deal__c>();
for ( Collateral__c c : Trigger.new ){
dealMap.put( c.Deal__c, new Deal__c( Id=c.Deal__c, Total_Lendable_Collateral__c=0) );
}
for ( AggregateResult ar : [
SELECT Deal__c parentId, SUM(Lendable_Value__c) s
FROM Collateral__c
WHERE Deal__c IN :dealMap.keySet()
]){
Id i = (Id)ar.get('parentId');
dealMap.put( i, new Deal__c(Id=i,Total_Lendable_Collateral__c=(Decimal)ar.get('s')));
}
update dealMap.values();
}