【问题标题】:Invalid identifier: Amount__c in trigger in Salesforce标识符无效:Salesforce 中触发器中的 Amount__c
【发布时间】:2017-03-07 07:50:01
【问题描述】:

我有一个主从对象上的触发器,其中Doctor 是主对象,Patient 是子对象。 Doctor 有一个名为 TotalAmount 的字段,Patient 有一个名为 Amount 的字段。现在,当患者填写金额字段时,Doctor 中的TotalAmount 字段应该给出所有患者记录中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;
}

【问题讨论】:

    标签: salesforce salesforce-communities


    【解决方案1】:

    试试下面的一个:

    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;
    

    问候 阿杰

    【讨论】:

    • 嗨 Ajay,它在该行显示 '' Illegal assignment from Decimal to Integer" --> amountVAR += p.Amount__c;
    • 使用 integer.ValueOf
    【解决方案2】:

    试试下面:

    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 上创建一个汇总字段?

    Creating Roll Up Summary fields

    【讨论】:

    • 嗨 Arun,是的,我们可以通过 Roll-up 做到这一点,但由于某些限制,我们需要使用触发器!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 2013-01-28
    • 2019-06-27
    • 2011-07-08
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多