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
rams123rams123 

how to assign agregate result value in to filed using trigger?


please suggest me if any one knows, find below my code.

i am facing error :"Error:Apex trigger cloudRevenueForcastTrigger caused an unexpected exception, contact your administrator: cloudRevenueForcastTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a1Q4B0000008PPGUA2; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, cloudRevenueForcastTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a1Q4B0000008P7qUAE; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, cloudRevenueForcastTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 1 with id a1Q4B0000008P5pUAE; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = a1Q4B0000008P5p) is currently being saved. Saving object (id = a1Q4B0000008P5p) would cause it to be recursively saved, which is not allowed.: [] Class.cloudRevenueForcastTriggerHandler.updateSegmentValue: line 85, column 1 Trigger.cloudRevenueForcastTrigger: line 14, column 1: [] Class.cloudRevenueForcastTriggerHandler.updateSegmentValue: line 85, column 1 Trigger.cloudRevenueForcastTrigger: line 14, column 1: []: Class.cloudRevenueForcastTriggerHandler.segmentValueUpdate: line 62, column 1"

Public void updateSegmentValue(List<Cloud_Revenue_Forecast__c> newList){
        
        set<string> crfIds = new set<string>();
        set<string> segmenttype = new set<string>();
        List<Cloud_Revenue_Forecast__c> updateRecords = new List<Cloud_Revenue_Forecast__c>();
        
        for(Cloud_Revenue_Forecast__c c : newList){            
            if(c.Segment_Value__c != null){
                crfIds.add(c.Id);
                segmenttype.add(c.Segment_Type__c);
            }            
        }
        Map<Id, Cloud_Revenue_Forecast__c> arSegmentValueMap = new Map<Id, Cloud_Revenue_Forecast__c>();        
        aggregateResult[] sumSegmentValue = [select sum(Segment_Value__c)cnt from Cloud_Revenue_Forecast__c where segment_type__c IN :segmenttype];        
        List<Cloud_Revenue_Forecast__c> uniquesegmentrecords = new List<Cloud_Revenue_Forecast__c>([select id, Segment_Value__c from Cloud_Revenue_Forecast__c where segment_type__c IN :segmenttype ]);
                
        for(Cloud_Revenue_Forecast__c c : uniquesegmentrecords){
            c.Segment_Value__c = (Decimal)sumSegmentValue[0].get('cnt');
            updateRecords.add(c);
        }
        update updateRecords;        
    }  
Tim BarsottiTim Barsotti

Hi Sriram - the issue is that the record that you are trying to update is actually one of the records being updated by the trigger already. In the After Context of the trigger, the record is read only as it has already been 'saved' to the database (but not yet committed). Reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm 

Additional considerations: 
Depending on the number of records you are querying - using aggregateResult will cap out at 50k records. 

Have you considered using a roll-up summary field?