+ Start a Discussion
HamptonHampton 

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

 

s_k_as_k_a

See the modified code below.

trigger ServiceOrderCount on Service_Order__c (after insert) {

    Map<String, Service_Order__c> serviceOrderMap = new Map<String,Service_Order__c>();
     for (Service_Order__c sord : Trigger.new) {
		 if(sord.Video__c=1)
		 {
           serviceOrderMap.put(sord1.Release__c, sord1);
		 }
	 }
     system.debug('*********serviceOrderMap ***********'+serviceOrderMap.keyset());
     
	 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 : serviceOrderMap.keyset() and Date_Assigned__c = Last_30_days])
     resweepMap.put(resweep1.Name, resweep1);
     system.debug('*********resweepMap***********'+resweepMap.keyset());
     
	 Map<String, Integer> serviceordercount =  new Map<String, Integer>();
	 for(AggregateResult ar: [Select Count(id) serRecs, Name  from Service_Order__c where name in :resweepMap.KeySet() group by Name])
	     serviceordercount.put((string)ar.get('Name'),(integer)(ar.get('serRecs'));
	 system.debug('*********serviceordercount***********'+serviceordercount);

	 List<Resweep_Assignment__c> resweepToUpdate = new List<Resweep_Assignment__c>();
     for(Resweep_Assignment__c a : serviceOrderMap.values())
	 {
          if(serviceordercount.containskey(a.Name))
		    {
                    a.Orders__c =serviceordercount.get(a.Name);
                    resweepToUpdate.add(a);
			}
	 }
	  system.debug('*********resweepToUpdate size***********'+resweepToUpdate.size());

     if(resweepToUpdate.size()>0)
        update resweepToUpdate;
}

 

Katia HageKatia Hage

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.