0

我有一个主从对象上的触发器,该对象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;
}
4

2 回答 2

0

试试下面的一个:

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 amountVAR = 0; 
        for (Patient__c p : d.Patient__r){
             amountVAR += p.Amount__c;
        }
        d.Total_Amount__c = amountVAR ;
        lstDoctor.add(d);
    }
    update lstDoctor;

问候阿杰

于 2017-03-07T10:26:17.080 回答
0

试试下面:

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]) {
        d.Total_Amount__c = 0;
        for (Patient__c p: d.Patient__r) {
            d.Total_Amount__c += p.Amount__c;
        }
        lstDoctor.add(d);
    }
    update lstDoctor;
}

但是为什么要使用触发器呢?为什么不在 Doctor 上创建一个汇总字段?

创建汇总汇总字段

于 2017-03-08T03:54:31.537 回答