function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Sum of fields from two custom objects



I would like to sum up fields from two different custom objects, no idea how to do it without ugly hacks of converting to decimal. I want to keep the sObject so that I can use it in an apex:outputField as this is a currency field and I need the currency sign.


Here's how I get the two sums today. Any idea how to sum both fields into one sObject?




  	    list <AggregateResult> resRooms = [SELECT SUM(Price__c) FROM ReservationRoomAssociation__c WHERE Rooms__c = :getReservationId()];
   		list <AggregateResult> resBeds = [SELECT SUM(Price__c) FROM BedReservationAssociation__c WHERE Reservation__c = :getReservationId()];



You Can Follow the concept of junction object for this

Obj1 having field Var1

Obj2 having field Var2

Obj3 having lookup with Obj1, Obj2 and "Sum" Field which is of type formula which will add var1 and var2.

Does it help???


No because it's not symetrical, I could have 1 obj1 and 100 obj2 


Ok i assumed in wrong way.

I thought how to add two different custom fields on different objects.