Not sure how to handle this in SOQL:

There are Customer objects and Order objects. A customer can have many orders

I want to select the DollarValue__c of each customer's oldest order?

I'm thinking something like this but it's wrong.

select DollarValue__c, min(OrderTime__c) from Order__c group by customer__r.id

You can use nested query. Try this:

[SELECT Id, (SELECT DollarValue__c FROM Orders__r ORDER BY OrderTime__c LIMIT 1) FROM Customer__c]

And then for each Customer__c record you can access the list of Orders which will only consist of 1 record, the oldest one:

if (customer.Orders__r.size() == 1) 
    // The oldest order for this customer

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.