You need to sign in to do that
Don't have an account?
Lam Corporation
Are there any SOQL whizzes out there? Bulk Date Query
Hi all,
I am working on building a Trigger on a child object that has a lookup field on it to the parent Contact object.
I have a date field on both the child object and on the Contact.
I would like to design a bulk trigger that performs a query of all the children of the child object and returns the latest date from all the children that are related to the Contact parent. Then take the latest date and update the Contact parent with the latest date. Creating this for one record would be quite simple.
Single Query
Child_Object__c a = [SELECT Id, End_Date__c FROM Child_Object__c WHERE Contact__c = contactParentId ORDER BY End_date__c ASC LIMIT 1];
I can't picture how to build this query for all records in Trigger.new. If I return a list of all records and then optimise is and LIMIT 1 I am only going to get one result. I would like to have a query that performs the single query but for all affected records in Trigger.new.
Can anyone help please? Many thanks for your time.
I am working on building a Trigger on a child object that has a lookup field on it to the parent Contact object.
I have a date field on both the child object and on the Contact.
I would like to design a bulk trigger that performs a query of all the children of the child object and returns the latest date from all the children that are related to the Contact parent. Then take the latest date and update the Contact parent with the latest date. Creating this for one record would be quite simple.
Single Query
Child_Object__c a = [SELECT Id, End_Date__c FROM Child_Object__c WHERE Contact__c = contactParentId ORDER BY End_date__c ASC LIMIT 1];
I can't picture how to build this query for all records in Trigger.new. If I return a list of all records and then optimise is and LIMIT 1 I am only going to get one result. I would like to have a query that performs the single query but for all affected records in Trigger.new.
Can anyone help please? Many thanks for your time.
SELECT Contact__c contactid, MAX(End_Date__c) maxdate FROM Child_Object__c WHERE Contact__c in (select Contact__c from Child_Object__c where id in : Trigger.new) group by Contact__c
or
Set<Id> contactIds = new Set<Id>();
for(Child_Object__c co : Trigger.new)
contactIds .add(co.Contact__c);
SELECT Contact__c contactid, MAX(End_Date__c) maxdate FROM Child_Object__c WHERE Contact__c in:contactIds group by Contact__c
the return type of the above SOQL will be AggreateResult[]. please refere below.
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_agg_fns.htm
All Answers
SELECT Contact__c contactid, MAX(End_Date__c) maxdate FROM Child_Object__c WHERE Contact__c in (select Contact__c from Child_Object__c where id in : Trigger.new) group by Contact__c
or
Set<Id> contactIds = new Set<Id>();
for(Child_Object__c co : Trigger.new)
contactIds .add(co.Contact__c);
SELECT Contact__c contactid, MAX(End_Date__c) maxdate FROM Child_Object__c WHERE Contact__c in:contactIds group by Contact__c
the return type of the above SOQL will be AggreateResult[]. please refere below.
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_agg_fns.htm
@BalajiRanganathan yours worked for me.