我有一个主从对象上的触发器,该对象Doctor
是主对象和Patient
子对象。Doctor
有一个字段叫做TotalAmount
并且Patient
有一个字段叫做Amount
。现在,当患者填写金额字段时,该TotalAmount
字段Doctor
应给出Amount
所有患者记录的总和。
我已经编写了下面的代码,但它显示了一个错误:
无效标识符:Amount__c
我怎样才能解决这个问题?
trigger tgPatient on Patient__c (after insert,after update) {
Set<Id>SetDoctor = new Set<Id>();
for (Patient__c p : trigger.new) {
if( p.Amount__c != Null ) {
SetDoctor.add(p.Doctor__c);
}
}
List<Doctor> lstDoctor = new List<Doctor>();
for( Doctor__c d : [SELECT Id, (SELECT Id,Amount__c FROM Patients__r) FROM Doctor__c WHERE Id IN:SetDoctor]){
Integer Amount__c = 0;
for (Patient__c p : d.Patient__r) {
int Amount__c += p.Amount__c;
}
d.Total_Amount__c = int Amount__c;
lstDoctor.add(d);
}
update lstDoctor;
}