You need to sign in to do that
Don't have an account?

AggregateResult with Multiple Values
I'm an admin trying to be a devleloper and am struggling. I'm trying to bulkify my trigger but am not sure how to assign multiple values from an aggregate query to a list. I've tried using a set, map and list but every example I see just has a Key and one value. I have two keys and multiple values.
My end goal is to update the case with the totalRevenue, TotalHours, and OverageHours from the billing. I have a related list of billing for each case. Each billing has a product that can have a different incident charge, per incident hours and possibly overage charge and each case can have multiple billings for the same product BUT only one case is counted as an incident. So if there are 4 billings with two different products, the incident charge would only be there once for each product, not four times for each billing. I wrote an aggregate query to get the sum of hours worked for each case and product. What I need to happen is for my trigger to loop through the aggregate results and for each case the for each product:
for case
loop through each product
if the totalHoursWorked > Per_Incident_hours__c then
total_hours = (totalHoursWorked - incidentHours * overageCharge) + incidentCharge
else total_hours = totalHoursWorked * incidentCharge
get next product
then update case TotalRevenue, TotalHours, TotalOverageHours
get next case
Here's what I have. However I don't know what to do with the AggregateResult set.
My end goal is to update the case with the totalRevenue, TotalHours, and OverageHours from the billing. I have a related list of billing for each case. Each billing has a product that can have a different incident charge, per incident hours and possibly overage charge and each case can have multiple billings for the same product BUT only one case is counted as an incident. So if there are 4 billings with two different products, the incident charge would only be there once for each product, not four times for each billing. I wrote an aggregate query to get the sum of hours worked for each case and product. What I need to happen is for my trigger to loop through the aggregate results and for each case the for each product:
for case
loop through each product
if the totalHoursWorked > Per_Incident_hours__c then
total_hours = (totalHoursWorked - incidentHours * overageCharge) + incidentCharge
else total_hours = totalHoursWorked * incidentCharge
get next product
then update case TotalRevenue, TotalHours, TotalOverageHours
get next case
Here's what I have. However I don't know what to do with the AggregateResult set.
trigger CaseClosed on Case (after insert, after update) { // this code will get the list of closed cases for Cloud IT records. It will loop through the billing for each case // and calculate the total case revenue along with the Total Hours Worked and Case Overage Hours. // list to hold updated case values List<Case> updateCase = new List<Case>(); // get list of closed cases and add the case to the case list set<id> caseIDs = new set<id>(); for (Case c : trigger.new){ if (c.IsClosed && c.RecordTypeId == '012600000001COh') { caseIDs.add(c.Id); system.debug('in CaseClosed trigger'); } } // loop through the caseIDs and get billing for each product // create an aggregate Map of all billing for each case by product LIST<aggregateResult> results = ([SELECT Case__c, Product__c, MAX(Per_Incident_Hours__c) incidentHours, MAX(Incident_Charge__c) incidentCharge, MAX(Overage_Amount__c) overageCharge, SUM(Hours_Worked__c) totalHoursWorked FROM Billing__c WHERE Case__c in : caseIds GROUP BY Case__c, Product__c]); decimal total_hours = 0; decimal total_dollars = 0; decimal total_overage_hours = 0; // create a map to hold results from query Map<Id, ID> arCaseProduct = new Map<Id, Id>(); list<Object> myList = new list<Object>(); // loop through and get billing for (AggregateResult ar : results){ System.debug('in aggregate debug'+ar.get('Case__c')+'-'+ar.get('Product__c')+'-'+ar.get('incidentHours')+'-'+ar.get('incidentCharge')+'-'+ar.get('overageCharge')+'-'+ar.get('totalHoursWorked')); arCaseProduct.put((ID)ar.get('Case__c'), (id)ar.get('Product__c')); system.debug('arCaseProduct is '+ arCaseProduct); } }
All Answers
Am I on the right track?