I hope everyone is doing well. I'm new to SalesForce and have been trying to learn Apex for a few weeks without much headway. Our org has a custom object called Factor Data that is a related list at the bottom of the Account page, much like opportunity/contact/etc. We have a field called First Full Purchase on the Account page that is supposed to display the higher purchase of the first two purchases made. Purchase MTD is a field on each inputted Factor Data record. I have the following trigger, which passes ye ole syntax check, but gives me this error message:
PurchaseForBrokerBonus: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.PurchaseForBrokerBonus: line 27, column 1
I know I'm neglecting to list the IDs. I'm just not sure how to query for IDs on a custom object, or if I'm completely missing something. Any help is appreciated. Thanks for y'alls time and have a good one.
Set<Decimal> accountPurchases = new Set<Decimal>();
List<Account> accountsToUpdate = new List<Account>();
for(Factor_Data__c fd :Trigger.new){
accountPurchases.add(fd.Purchase_MTD__c);
}
accountPurchases.remove(null);
for(AggregateResult ar :[SELECT MAX(Purchase_MTD__c) maxPurchase, Date__c
FROM Factor_Data__c
WHERE Purchase_MTD__c != null
AND Purchase_MTD__c IN :accountPurchases
GROUP BY Date__c
ORDER BY Date__c ASC
LIMIT 2]){
accountsToUpdate.add(new Account(
First_Full_Purchase__c = (Decimal)ar.get('maxPurchase')
));
}
update accountsToUpdate;