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
Sony PSPSony PSP 

Calculate the sum of quantity from product forecast under agreement under project

Hi guys,

hope you can help me.

I have this two fields under Project__C namely as Primary_Solution_Series__cand Secondary_Solution_Series__c and another field wherein results/sum are being put namely as Allocated_OEM_Project_Forecast__c.

the scenario is I have a product forecast under agreement which is under project meaning the product forecast is the grand child of project.

under the productforecast there are fields namely Quantity and Product(Primary and Secondary Series)

example is below

User-added image 

what i want is that if S332U if chosen as Primary_Solution_Series__c  it will auto recalculate having the sum 13000 in the  Allocated_OEM_Project_Forecast__c under the Project__c and if Primary_Solution_Series__c is has chosen S3320 it will recalculate having the sum 8500.

another what is the sum of the Allocated_OEM_Project_Forecast__c will also be written on Agreement__c under the field OPA_Lifetime_Product_Forecast__c. wherein the Agreement__c is the child or Project__c

I already have the delete/add function my problem is that when Primary_Solution_Series__c  and Secondary_Solution_Series__c  has changed it doesn't recalculate.
Set<ID> poemId = new Set<ID>();
        Set<ID> poemssId = new Set<ID>();
        Set<ID> pmmfId = new Set<ID>();
        List<Apttus__APTS_Agreement__c> AgreementToUpdate = new List<Apttus__APTS_Agreement__c>();
        List<Project__c> PaneltoUpdate = new List<Project__c>();
        Double pfSum = 0;
        
        for(Project__c pfc: newProject){
            poemId.add(pfc.id);
            poemssId.add(pfc.Primary_Solution_Series__c);
            pmmfId.add(pfc.Secondary_Solution_Series__c);}
        
        for(Apttus__APTS_Agreement__c agr : [select id FROM Apttus__APTS_Agreement__c where  Panel__c =: poemId]){
            poemId.add(agr.id);
        
        for(APTS_Product_Forecast__c pf : [SELECT APTS_Quantity__c , APTS_Price_Agreement__c, Panel__c FROM APTS_Product_Forecast__c WHERE (product_is_primary__c = TRUE OR product_is_secondary__c = TRUE ) AND APTS_Price_Agreement__c != NULL AND  APTS_Price_Agreement__c =: poemId]){
                                                  
            pfSum +=pf.APTS_Quantity__c;
        
        
        for(Project__c prj: [Select Id, Primary_Solution_Series__c, Secondary_Solution_Series__c from Project__c where Id =: poemId]){
            if(prj.Primary_Solution_Series__c != oldMap.get(prj.id).Primary_Solution_Series__c || prj.Secondary_Solution_Series__c != oldMap.get(prj.id).Secondary_Solution_Series__c)
                prj.Allocated_OEM_Project_Forecast__c = pfSum;
            PaneltoUpdate.add(prj);
        }
        
        for(Apttus__APTS_Agreement__c agmt: [Select Id from Apttus__APTS_Agreement__c where ID =: poemId]){
            agmt.OPA_Lifetime_Product_Forecast__c = pfSum;
            AgreementToUpdate.add(agmt);}
    }
prabhakaran Chockalingam 5prabhakaran Chockalingam 5
How are you grouping the quantity by Project? Line 18, does sum up the quantity for all the projects.
Have u added debug statement to see the sum of quantity. 

I am also new to Apex and the below code may not be the correct solution, but I hope it can be of some help in terms of rewritting the logic,
trigger calculateForcecastSum on Project__C (before update) {

    //store the values in a Map to use it for update
    Map<Id, Project__c> newprojectMap = new Map(Id, Project__c)(Trigger.new);
    Map<Id, Project__C> oldProjectMap = new Map(Id, Project__c)(Trigger.old);

    //Consider those records whose value changes
    for(Project__c project : newprojectMap.keySet()){
    if(newprojectMap.get(project.Id).Primary_Solution_Series__c == oldProjectMap.get(project.ID).Primary_Solution_Series__c ||
            newprojectMap.get(project.Id).Secondary_Solution_Series__c == oldProjectMap.get(project.ID).Secondary_Solution_Series__c ){
                newprojectMap.remove(project.ID);
    }
    }

    System.debug('Number of Changed Values: ' + newprojectMap.size());

    if(!newprojectMap.isEmpty()) {
        //calcuate the sum for those records whose value changes.
        Map<Id, Apttus__APTS_Agreement__c> agrToUpdateMap = [SELECT Panel__c, Sum(APTS_Product_Forecast__c.APTS_Quantity__c) forcast_sum
        from Apttus__APTS_Agreement__c
        where Panel__c in :newprojectMap.keySet()
        group by Panel__c];


    for(Apttus__APTS_Agreement__c agrUpadte : agrToUpdateMap.values()){
        agrUpadte.OPA_Lifetime_Product_Forecast__c = agrUpadte.forcast_sum;
    }

        for(Project__c project : newprojectMap.keySet()){
            newprojectMap.get(agrUpadte.Panel__c).Allocated_OEM_Project_Forecast__c = agrToUpdateMap.get(project.Id).forcast_sum;
        }
    }

}



 
Sony PSPSony PSP
Hi 

thanks for this, I was also able to figure it out before this post and its a bit same :) thanks men :)