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 

Trigger, Sum on a Field

Hi Guys,

Can you help me with this, I am just new with salesforce

I need to update the Project Object where the field is OEM_Project_Forecast__c, the Agreement__c is the child of the Project, and the Child of Agreement are list of Product_Forecast__cwherein I need to get the sum of all the product forecast and put it on the Project under the OEM_Project_Forecast__c the sum of product forecast.

thank you
public with sharing class AgreementTriggerHandler{
    
    public static void onAfterInsert(List<Agreement__c> agrList) {
        isUpdateSynapticsPanel(agrList);
    }
    
    public static void onAfterUpdate(List< Agreement__c> agrList) {
        isUpdateSynapticsPanel(agrList);
    }
    
    
    public static void isUpdateSynapticsPanel (List< Agreement__c> agrList){
        Set<Id> agrIdSet = new Set<Id>();
        List<Project__c> PaneltoUpdate = new List<Project__c>();
        Double pfSum = 0;

        for(Agreement__c pfs : agrList){
         // if(pfs.Panel__c != null)
         System.debug('PFA value = ' + pfs);
            agrIdSet.add(pfs.Panel__c);
        }
        
        //System.debug('agrIdSet value = ' + agrIdSet);

       for(Agreement__c  pf: [SELECT Lifetime_Product_Forecast__c, Panel__c  FROM Agreement__c WHERE Panel__c =: agrIdSet]){
            System.debug('pf value = ' + pf);
            pfSum += pf.Lifetime_Product_Forecast__c;
          
        }
  
         
        for(Project__c prj: [Select Id from Project__c where Id =: agrIdSet]){
             System.debug('projsum value = ' + prj);
            prj. OEM_Project_Forecast__c = pfSum;
            System.debug('SUM value = ' + pfSum);
            
            PaneltoUpdate.add(prj);
        }
        
         if(!PaneltoUpdate.isEmpty()){
            update PaneltoUpdate;
        }
    }
}

 
Shiva RajendranShiva Rajendran
Hi Sony,
could you tell me more clearly what issue are you facing? The code looks fine, it would update all the prj.OEM_Project_Forecast__c with the same sum value pfSum you have generated
Thanks and Regards,
Shiva RV
Sony PSPSony PSP
oh I forgot, It doesn't filter the Product Forecast, based on two fields primary and secondary wherein found on the Object  Projects wherein it should not include in computation / sum data is not found on those two fields, I cannot see the quantity_c under agreement_c but can only be found under the product forecast under agreement .
FearNoneFearNone
hi Sony,

in "where Id =:"
for(Project__c prj: [Select Id from Project__c where Id =: agrIdSet]){

shouldn't it be "IN"?
for(Project__c prj: [Select Id from Project__c where Id IN :agrIdSet]){



Good Luck!