2017-03-07 2 views
0

J'ai un déclencheur sur un objet principal-détail où Doctor est maître et Patient est enfant. Doctor a un champ appelé TotalAmount et Patient a un champ appelé Amount. Maintenant, quand un patient remplit le champ montant, le champ TotalAmount dans Doctor doit donner la somme de Amount dans tous les dossiers du patient.Identificateur non valide: Amount__c dans le déclencheur dans Salesforce

J'ai écrit le code ci-dessous, mais il montre une erreur:

Invalid identifier: Amount__c

Comment puis-je résoudre ce problème?

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; 
} 

Répondre

0

Essayez le ci-dessous un:

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; 

concernant Ajay

+0

Salut Ajay, Il montre '' assignation illégale de décimale en entier " à la ligne -> amountVAR + = p.Amount__c; –

+0

utilisation integer.ValueOf – Ajay

0

Essayez ci-dessous:

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; 
} 

Mais pourquoi voulez-vous utiliser un déclencheur? Pourquoi ne pas créer un champ récapitulatif de cumul sur Doctor?

Creating Roll Up Summary fields

+0

Salut Arun, Oui, nous pouvons faites cela en Roll-up mais à cause d'une contrainte nous devons utiliser trigger! –

+0

Ok. Est-ce que ma réponse a fonctionné? –