You need to sign in to do that
Don't have an account?
Hampton
Apex Record Count
Hello:
I have two unrelated custo objects that share a common key (Service_Order__c.Release__c = Resweep_Assignment__c.Name).
Both custom objects have date fields: Service_Order__c.Order_App_Date__c and Resweep_Assignment__c.Date_Assigned__c
What I am looking to do is to update a number field (Orders__c) with a count of Service_Order__c records where Service_Order__c.Order_App_Date__c is within 30 days of Resweep_Assignment__c.Date_Assigned__c.
Here is what I have, which is not saving. Any suggestions are greatly appreciated.
trigger ServiceOrderCount on Service_Order__c (after insert) { Set<String> serviceOrder = new Set<String>(); for (Service_Order__c sord : Trigger.new) { serviceOrder.add(sord.Release__c); Map<String, Service_Order__c> sordMap = new Map<String,Service_Order__c>(); for(Service_Order__c sord1 : [Select Name, Order_App_Date__c, Release__c from Service_Order__c where Name in : serviceOrder and Video__c=1]); sordMap.put(sord1.Release__c, sord1); Map<String, Resweep_Assignment__c> resweepMap = new Map<String, Resweep_Assignment__c>(); for(Resweep_Assignment__c resweep1: [Select Name, Date_Assigned__c, Orders__c from Resweep_Assignment__c where Name in : serviceOrder]); resweepMap.put(resweep1.Name, resweep1); Set<String> resweepDate = new Set<String>(); resweepDate.add(resweep1.Date_Assigned); integer i = [Select Count() from Service_Order__c where Name =: serviceOrder and (resweepDate < Order_App_Date__c < resweepDate+30)]; List<Resweep_Assignment__c> resweepToUpdate = new List<Resweep_Assignment__c>(); for(Resweep_Assignment__c a : [Select Name, Date_Assigned__c, Orders__c where Name in : serviceOrder]){ if(serviceOrderMap.containskey(a.Name){ a.Orders__c = i; resweepToUpdate.add(a); } } update resweepToUpdate }
Thanks,
Hampton
See the modified code below.
s_k_a: In the for loop, did you mean resweepMap.values() instead? Also, is group by name necessary in the aggregate query? Thanks for contributing a solution.