You need to sign in to do that
Don't have an account?
Counting Records and then Updating That Value to Different Object
Hello. I am fairly new to Apex programming and have been trying to cobble together a solution via an earlier post, in addition to other ideas seen on this board. That said, I am still having issues. I would be very appreciative for any guidance.
Specifically, I have two custom objects - one each for customers (Customer__c) and deliveries (Deliveries__c). I am trying to update a field (NumberOfDeliv__c) in the customers object to reflect the current number of deliveries that have a status of “pending” for each customer. This value is derived by totaling the number of occurrences of “Deliveries__c” records for each customer that have a “DelivStatus__c” value of “pending”. Due to a separate reason, I must do this update via Apex.
I am doing this in two phases. 1) Doing an AggregateResult function to total the number of pending deliveries for each customer…and then 2) Using a map to update the respective value for each customer.
My initial problem is that I am getting an error message "Initial term of field expression must be a concrete SObject: MAP<Id,Customer__c>".
Any suggestions on why I am getting this error - or also alternate code ideas to arrive at my overall goal - would be very appreciated. Thanks.
**********************************
trigger CountCustomers on Deliveries__c (before insert) {
Map <String, Integer> DeliveryCount = new Map <String, Integer> ();
String DelivID = null;
Integer nbr = null;
list<string> DelivList = new list<string>();
for (Deliveries__c dlv : Trigger.new){
if (dlv.DelivStatus__c != null)
DelivList.add(dlv.DelivStatus__c);
}
//Counting the occurrences of pending deliveries for each cusotmer (using CustomerID__c)
//The CustomerID__c field is simply a string representation in the Deliveries__c object of the customer record ID
for (AggregateResult agr: [SELECT CustomerID__c, count(Id) NumOfCust FROM Deliveries__c
WHERE CustomerID__c in :DelivList
AND (DelivStatus__c = 'pending')
GROUP BY CustomerID__c]){
DelivID = string.valueOf(agr.get('CustomerID__c'));
nbr = integer.valueOf(agr.get('NumOfCust'));
DeliveryCount.put (DelivID, nbr);
}
List<Id> Cstmrs = new List<Id>{};
//Creating Map of CustomerObject
Map <ID, Customer__c> CustFields = new Map <ID, Customer__c>([select id, NumberOfDeliv__c from Customer__c where id in:Cstmrs]);
//Looping through the DeliveryObject
for(Deliveries__c Deliv : Trigger.new)
{
if (Deliv.CustomerID__c != null){
//Holding the field updates for each MenuTemsObject
Customer__c cust = CustFields.get(Deliv.CustomerID__c);
IF(cust.NumberOfDeliv__c == null || (Trigger.isupdate && cust.NumberOfDeliv__c == Trigger.oldMap.get(Deliv.id).Integer.valueOf(DeliveryCount.get(CustFields.Id))))
cust.NumberOfDeliv__c = Integer.valueOf(DeliveryCount.get(CustFields.Id));
CustToUpdate.add(cust);
}
}
}
Hey
Any reason you can't use rollup fields? This would be zero development, testing etc.
Wes
All Answers
Hey
Any reason you can't use rollup fields? This would be zero development, testing etc.
Wes
Thanks. I initially did not think that a rollup would work...but I just changed some things around and I think I can do it now without Apex.
Thanks
Glad to help, you live you learn :)
Mighty fine avatar btw.