0

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;
0

Ok. First of all: you're not achieving what you want. You're are fetching the max value among the Factor Data with a value EQUAL to the one in the trigger. And you're limiting it to 2 accounts.

To achieve what you want, you'll need something like the code below. Note: replace Account__c with the name of the lookup field on the Factor Data.

List<Account> accountsToUpdate = new List<Account>();
Map<Id, Decimal> maxPurchasePerAccount = new Map<Id, Decimal>();
Map<Id, Integer> accountPurchaseCount = new Map<Id, Integer>();

for(Factor_Data__c fd :Trigger.new){
    //get the id of all accounts affected
    accountPurchaseCount.put(fd.Account__c, 0);
}

//fetch all factor datas for the accounts concerned with no First Full Purchasebvh (if the account already has a First Full Purchase, there's no need to look again)
for(Factor_Data__c fc : [SELECT Purchase_MTD__c, Account__c
        FROM Factor_Data__c
        WHERE Account__c IN :accountPurchaseCount.keySet()
        AND (Account__r.First_Full_Purchase__c = 0 
        OR Account__r.First_Full_Purchase__c = null)
        ORDER BY Date__c ASC]){

    //Count how many factor datas were annalyzed for each account
    Integer count = accountPurchaseCount.get(fc.Account__c)+1;
    accountPurchaseCount.put(fd.Account__c, count);

    if(count > 2) continue; //We already evaluated the first two, no need to check further

    Decimal accountPurchase = maxPurchasePerAccount.get(fc.Account__c);
    if(accountPurchase == null) maxPurchasePerAccount.put(fc.Account__c, fc.Purchase_MTD__c); //evaluating the first one
    else if(fc.Purchase_MTD__c > accountPurchase) maxPurchasePerAccount.put(fc.Account__c, fc.Purchase_MTD__c); //if the second is higher, save this one instead
}

for(Id accId : maxPurchasePerAccount.keySet()){
    //reloop through the accounts to see wich one need to be updated 
    if(accountPurchaseCount.get(accId) >= 2){
        accountsToUpdate.add(new Account(Id = accId, First_Full_Purchase__c = maxPurchasePerAccount.get(accId)));
    }
}

update accountsToUpdate;

P.s.: as a best practice, on the first loop through the trigger new, add a check to know wich accounts to add to the set. Maybe you'll just want to do that when the Factor Data is approved or something like that

| improve this answer | |
  • Hey Fernando, I apologize for the late response. Been away for a bit, but thank you so much. As always, I appreciate the help. I didn't realize I was limiting it to two accounts. I thought I was limiting the factor data to the first two entries, but that makes sense. It works great, I am just having trouble pulling the highest purchase if the second month's purchase is higher, but I will tinker around with that today and see if I can get it to work. Thanks again for all of the help. Still trying to get acclimated with all of this. – Conner Jul 28 '16 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.