0
trigger PaymentTrigger on Money_Received__c (after insert, after update, after delete) {

    Money_Received__c moneyReceived = [SELECT Reservation__c FROM Money_Received__c];

    Reservation__c reservation = [SELECT Id, Name, Total_Recieved__c FROM Reservation__c where Id =: moneyReceived.Reservation__c];
    Decimal totalReceived = reservation.Total_Recieved__c;

    if(Trigger.IsInsert){
        for(Money_Received__c payment: Trigger.new){
            totalReceived += payment.Amount_Received__c;
        }
        update reservation;
    }

    if(Trigger.IsDelete){

        for(Money_Received__c payment: Trigger.old){
            totalReceived -= payment.Amount_Received__c;
        }
        update reservation;
    }
}

I'm trying to write a trigger that sum up payments.

reservation__c and money_received__c are look-up relationship.

sum of Money_Received__c. Amount_Received__c should be equal to Reservation__c. Total_Recieved__c.

I wrote this trigger, but gives me an error message saying

PaymentTrigger: execution of AfterInsert caused by: System.QueryException: List has more than 1 row for assignment to SObject Trigger.PaymentTrigger: line 3, column 1

Any suggestions on how to fix this? Also, how do I write 'trigger.updated'?

your help is appreciated.

marked as duplicate by sfdcfox soql Oct 19 '18 at 0:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Please take a look at the linked questions. If they do not help you solve your problem, please let me know and I'll be glad to reopen your question for you. – sfdcfox Oct 19 '18 at 0:41
1

Your issue is on these lines:

Money_Received__c moneyReceived = [SELECT Reservation__c FROM Money_Received__c];

Reservation__c reservation = [SELECT Id, Name, Total_Recieved__c 
                  FROM Reservation__c where Id =: moneyReceived.Reservation__c];

The reason - the queries here can return more than one row and that you are assigning it to only one single instance, thus it throws the exception as you are getting.

To fix this, you need to bulkify your code, and it should be written as below:

List<Money_Received__c> moneyReceived = [SELECT Reservation__c FROM Money_Received__c];

List<Id> reservationIds = new List<Id>();
for(Money_Received__c m : moneyReceived) {
   reservationIds.add(m.Reservation__c);
}

List<Reservation__c> reservation = [SELECT Id, Name, Total_Recieved__c 
                  FROM Reservation__c where Id IN :reservationIds];

Then you process the items in the list as required. This approach also ensures that if at all you only receive one record, your implementation does not fail.

  • Then I have problem with 'moneyReceived.Reservation__c' and 'reservation.Total_Recieved__c' – Logan H Oct 19 '18 at 0:46
  • You will need to create a list of the Reservation__c that you receive from first query and then utilize it in the other query. I have only provided a guidance there as how you can do that. I added that piece, if that helps. – Jayant Das Oct 19 '18 at 0:52
  • Basically what you need here is to bulkify your code, so that your code is capable of processing more than one record anytime. I added some reference in the answer, hope it helps. – Jayant Das Oct 19 '18 at 1:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.