0

I need to query the child objects and group them based on contact and created date

example.

customobject records queried based on account - gets me 100 records

unique contacts among 100 records - 10 (so each contact has 10 records)

im looking to get only 10 records among them - 1 for each contact based on recently created date

is it possible via soql queries? or need multiple looping to get this result?

thanks for any help!

1

user10824, I don't think you'll be able to use a query to get exactly the data you need, so you will have to resort to looping through the results in Apex to construct your results. For example, the code below will find the most recent child record by Contact ID and store that in the objectsByContactId data structure.

// Initialize the data structure that will hold the desired results, which
// is the most recent child record for each contact. The map will be keyed
// on the Contact ID, with values being the most recent child record based
// on Created Date.

Map<Id, CustomObject__c> objectsByContactId = new Map<Id, CustomObject__c>();

// Loop the records to aggregate using Apex, instead of SOQL. However,
// use the SOQL query to order the records correctly, so the Apex code only
// has to check to see whether the Contact record was already encountered.
// Replace the placeholders with desired fields and phrases.

for (CustomObject__c eachObject : [
    SELECT Id, CreatedDate, Contact__c, __
    FROM CustomObject__c
    WHERE __
    ORDER BY CreatedDate DESC
]) {

    // If the Contact is new to us, we assume the object to be the most recent
    // child record based on the sort order specified in the SOQL query

    if (!objectsByContactId.containsKey(eachObject.Contact__c))
        objectsByContactId.put(eachObject.Contact__c, eachObject);
}   // for each CustomObject__c in the list of matching records

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.