+ Start a Discussion
Kenn K.Kenn K. 

Update values from Aggregate Result Query

I am trying to update a field on the Quote (Operational_Start_Date__c) from the results of an aggregate result query.
The trigger runs on quote, queries QuoteLine for unique values and update the Quote with the unique values. This is what I have so far but it doesn't seem to be working;
 
trigger SB_QuoteAdditionalUpdates on SBQQ__Quote__c (before insert, before update) {
	
    	
    	Set<Id> quoteIds = new Set<Id>();
    
   		 for (SBQQ__Quote__c quotes : Trigger.new) {
   		 	
   		 		quoteIds.add(quotes.id);
   		 
   		 }
    	
    		List<AggregateResult> uniqueCountries = ([SELECT  Country_Operational_Date__c FROM SBQQ__QuoteLine__c WHERE SBQQ__Quote__c IN:quoteIds 
	    												AND country_code__c <> null AND Employee_CountFormula__c <> null GROUP BY Country_Operational_Date__c]);
    		
    		
    		if(uniqueCountries.Size()>0 ){
    			
    			
    			for (AggregateResult uniqueCountry : uniqueCountries){
    				
    				quotes.Operational_Start_Date__c = String.valueOf(uniqueCountry.get('Country_Operational_Date__c'));
    				system.debug('======>'+String.valueOf(uniqueCountry.get('Country_Operational_Date__c')));
    				
    			}      			
    		}
   		 
}

When I make the for loop run through the aggregate result (not optimized/bulkified), it updates the quote but it's only updating with one of the unique values and the quote might have had 3 unique values.