Decimal totalReceived = 0.00;

List<Money_Received__c> moneyReceived = [Select Amount_Received__c,Reservation__r.Total_Recieved__c from Money_Received__c where Id =: reservationId];

        for(Money_Received__c receivedMoney : moneyReceived ){
            if (totalReceived == 0.00){
                totalReceived += Money_Received__c.Amount_Received__c;

Amount_Received__c is a custom field(type currency). When user input multiple 'Amount_Received__c' into a table, it should sum up to 'totalReceived'

how can I get the numeric values from Amount_Received__c ??

thank you in advanced.


Two problems.

First Only

Your if statement means that you're only going to get the first non-zero value in the list. That's probably not what you want if you're trying to sum multiple values.

Field Reference vs Object Reference

You need to refer to the field on a record, not the static field reference that represents the field's metadata.

for(Money_Received__c receivedMoney : moneyReceived ){
    totalReceived += receivedMoney.Amount_Received__c;
| improve this answer | |
  • I got an error when I tried receivedMoney.Amount_Received__c, but not is works..Thank you!! – Logan H Oct 11 '18 at 20:48
  • What do I need to change if I want to sum values whenever Amount_Received__c is updated? – Logan H Oct 11 '18 at 21:13
  • @LoganH you need to call the method when the value changes. This is usually done with an "actionSupport" element in your Visualforce page. – sfdcfox Oct 11 '18 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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