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
Mohammed AzarudeenMohammed Azarudeen 

Finding Total Amount from Child to Parent

Hi. I am getting more and more strong by reading forums and practising. Here I have a problem. I have two objects one is Department and another one is Medical_Equipment. Department object is Master for Medical_Equipment. Medical_Equipment has Amount__c field. Am rendering page by pdf format. In controller, I want to calculate total amount for items(medical equipment)

Here is my controller.
public class Medical_Equipment_ControllerCls {

    public decimal totalAmount {get;set;}
    public decimal temp = 0;
    set<id> getid = new set<id>();
    public Medical_Equipment_ControllerCls(ApexPages.StandardController controller)
    {       
        Medical_Equipment__c med = new Medical_Equipment__c();
        for(Medical_Equipment__c medical : [select name, Amount__c,Equipment_for_Department__c from Medical_Equipment__c])
        {
            getid.add(medical.Equipment_for_Department__c);  
            
        }
        List<Medical_Equipment__c> lstmedical = [select Amount__c from Medical_Equipment__c where id in : getid and Amount__c !=null];
        for(Medical_Equipment__c medicalequp : lstmedical)
           {
                temp = temp + medicalequp.Amount__c;  
                totalAmount = temp;
           }
        
    }
}

It is showing nothing for Total Amount
User-added image

I think the problem on getting Departmennt id for particular medical equipment(parent id -> child)
Any discussion and ans is welcome !!!
Shweta_AgarwalShweta_Agarwal
Hi,

If your standared standardController is "Department" parent object then in code do below changes

public class Medical_Equipment_ControllerCls {

    public decimal totalAmount {get;set;}
    public decimal temp = 0;
    string parentId = null;
    public Medical_Equipment_ControllerCls(ApexPages.StandardController controller)
    {       
        parentId = Apexpages.currentPage().getParameters().get('id');
        
        List<Medical_Equipment__c> lstmedical = [select Amount__c from Medical_Equipment__c where <ParentIdField> =: parentId and              Amount__c !=null];
        for(Medical_Equipment__c medicalequp : lstmedical)
           {
                temp = temp + medicalequp.Amount__c;  
                totalAmount = temp;
           }
        
    }
}

Hope it will solve your problem.

Thanks,
Shweta

 
Mohammed AzarudeenMohammed Azarudeen
@shweta vfp standard controller is child object which is Medical_Equipment__c
JeffreyStevensJeffreyStevens
If the object are master-detail - then you could use roll-up summary fields for your total.  Probably wouldn't need to code anything in that case. 
Shweta_AgarwalShweta_Agarwal
Hi Mohammad,

If your stanadred controller is child object and you are showing sum of amount for the all the the item under the parent of particular child object.
Then may be this can help you

public class Medical_Equipment_ControllerCls {

    public decimal totalAmount {get;set;}
    public decimal temp = 0;
    set<id> getid = new set<id>();
    string parentId = null;
    Medical_Equipment__c medEqRec = new Medical_Equipment__c();
    public Medical_Equipment_ControllerCls(ApexPages.StandardController controller)
    {       
        medEqRec = (Medical_Equipment__c)controller.getRecord();
        parentId = medEqRec.<parentIdField>;
        
        List<Medical_Equipment__c> lstmedical = [select Amount__c from Medical_Equipment__c where <ParentIdField> =: parentId and Amount__c !=null];
        for(Medical_Equipment__c medicalequp : lstmedical)
           {
                temp = temp + medicalequp.Amount__c;  
                totalAmount = temp;
           }
        
    }
}