1

Any way I can do this via report? or apex if not?

Thanks for your help!

1

You can run this query (it will have as sublist for each account 1 Withdrawal__c with the earliest CreatedDate):

SELECT Id, (SELECT Id, Amount__c FROM Withdrawals__r ORDER BY CreatedDate ASC LIMIT 1) FROM Account

After querying it, you'll have:

List<Account> accs = [SELECT Id, (SELECT Id, Amount__c FROM Withdrawals__r ORDER BY CreatedDate ASC LIMIT 1) FROM Account];

Decimal totalAmountWithdrawn = 0;
for (Account acc : accs) {
    if (acc.Withdrawals__r.size() > 0 && acc.Withdrawals__r[0].Amount__c != null) {
        totalAmountWithdrawn += acc.Withdrawals__r[0].Amount__c;
    }
}
3
  • @user256515 Have updated the answer, your relationship is called: Withdrawals__r, not Withdrawal__r. – Mariia Illarionova Jun 16 at 13:02
  • Thank you, this does work! Unfortunately I'm hitting a "too many query rows error", is there any way to bypass this? i.imgur.com/omAfl3j.jpeg – user256515 Jun 16 at 13:31
  • You are welcome! Yes, you should limit account records, so there are 2 records per account (account + withdrawal). You can also limit query like this SELECT Id, (SELECT Id, Amount__c FROM Withdrawals__r ORDER BY CreatedDate ASC LIMIT 1) FROM Account WHERE Id IN (SELECT Account__c FROM Withdrawal__c), so there will not be listed accounts with NO withdrawals. – Mariia Illarionova Jun 16 at 14:25
1

Try this

SELECT Id, (SELECT Amount__c FROM Withdrawal__r ORDER BY CreatedDate ASC LIMIT 1) FROM Account where CreadtedDate = THIS_YEAR

And then iterate on the list to calculate total

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.