function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lescclarkcpdlescclarkcpd 

Help Bulkifying roll up summary Trigger

Hi

 

I am pretty new to apex and have had a good few goes at trying to get my head around bulkyfying this trigger.  I know I need to use a map & an aggregateresult loop but just cant seem to get the syntax right.

 

any help appreciated

 

for(Course_Application__c c:Trigger.new)      
        {
        Course__c c = [select id, Confirmed_applications__c  from Course__c where Id =:c.id];
        Integer u = [select Count() from Course_Application__c where course_del__r.Id =:crsId and status__c='Ordered'];
        c.Confirmed_applications__c=u;
        update c;
}

 thanks

les

 

 

Best Answer chosen by Admin (Salesforce Developers) 
lescclarkcpdlescclarkcpd

used this in the end

 

  Map<id, Integer> coursesToassigntsMap = new Map<id, Integer>();
	  set<Id> courseIds = new Set<Id>();
	   for(Course_Application__c a: trigger.new){
		   courseIds.add(a.course_del__c);	   
   		}
   	    List<course__c> CList = [Select id,Confirmed_applications__c from Course__C where id in :CourseIds];
	   
	   
	   for (AggregateResult agr :[select count(id) idCount,course_del__c from Course_Application__c where course_del__r.Id in :CourseIds and status__c='Ordered'group by course_del__c])
	   						     
		{
          coursesToassigntsMap.put((ID)agr.get('course_del__c'), integer.valueof(agr.get('idCount')));
         
		}
	   
	  List<Course__C> UpdateList = new List<Course__C>();
		for(course__c ids: CList)
		{
		
		ids.Confirmed_applications__c = coursesToassigntsMap.get(ids.id);
		UpdateList.add(ids);	
	}	

	if(UpdateList.size()>0)
	Update UpdateList;		

 

All Answers

georggeorg

I hope the follwowing code will help you!.

 

List<Id> CourseIds =new List<Id>();
for(Course_Application__c c:Trigger.new)      
{
	CourseIds.add(c.id);
}

Map<Id,Course> Map_IdCourse = [Select id,Confirmed_applications__c from Course__C where id in :CourseIds];

Map<Id,List<Course_Application__C>> Map_IdListCourse = new Map<Id,List<Course_Application__C>>();
For(Course_Application__C CA: [select Id from Course_Application__c where course_del__r.Id in:CourseIds and status__c='Ordered']])
{
	if(!Map_IdListCourse.COntainsKey(CA.course_del__r.Id))
	{
		List<Course_Application> capp = new List<Course_Application>()
		capp.add(ca);
		Map_IdListCourse.put(CA.course_del__r.Id,capp);	
	}
	else
	{	
		List<Course_Application> capp = Map_IdListCourse.get(CA.course_del__r.Id);
		Map_IdListCourse.remove(CA.course_del__r.Id);
		capp.add(CA);
		Map_IdListCourse.put(CA.course_del__r.Id,capp);
	}
}

List<Course__C> UpdateList = new List<Course__C>();
for(Id ids: Map_IdListCourse.keySet())
{
	Course__C co = Map_IdCourse.get(ids).
	co.Confirmed_applications__c = Map_IdListCourse.get(ids).size();
	UpdateList.add(co);	
}

if(UpdateList.size()>0)
	Update UpdateList;

 

 

 

Thanks,

George

Visit my blog here

lescclarkcpdlescclarkcpd

hi

 

sorry it's taken a while to respond.  I have had to tidy up a few bits but I get an error, 'illegal assignment from LIST <Course__c> to MAP <Id,Course__c>' relating to the line in bold

 

List<Id> CourseIds =new List<Id>();
		for(Course_Application__c c:Trigger.new)      
		{
			CourseIds.add(c.course_del__r.id);
		}

		Map<Id,Course__c> Map_IdCourse = [Select id,Confirmed_applications__c from Course__C where id in :CourseIds];

		Map<Id,List<Course_Application__C>> Map_IdListCourse = new Map<Id,List<Course_Application__C>>();
		For(Course_Application__C CA: [select Id from Course_Application__c where course_del__r.Id in:CourseIds and status__c='Ordered'])
			{
		if(!Map_IdListCourse.COntainsKey(CA.course_del__r.Id))
			{
			List<Course_Application__c> capp = new List<Course_Application__c>();
			capp.add(ca);
			Map_IdListCourse.put(CA.course_del__r.Id,capp);	
		}
		else
		{	
			List<Course_Application__c> capp = Map_IdListCourse.get(CA.course_del__r.Id);
			Map_IdListCourse.remove(CA.course_del__r.Id);
			capp.add(CA);
			Map_IdListCourse.put(CA.course_del__r.Id,capp);
		}
	}

	List<Course__C> UpdateList = new List<Course__C>();
	for(Id ids: Map_IdListCourse.keySet())
		{
		Course__C co = Map_IdCourse.get(ids).
		co.Confirmed_applications__c = Map_IdListCourse.get(ids).size();
		UpdateList.add(co);	
	}	

	if(UpdateList.size()>0)
	Update UpdateList;	

 

lescclarkcpdlescclarkcpd

used this in the end

 

  Map<id, Integer> coursesToassigntsMap = new Map<id, Integer>();
	  set<Id> courseIds = new Set<Id>();
	   for(Course_Application__c a: trigger.new){
		   courseIds.add(a.course_del__c);	   
   		}
   	    List<course__c> CList = [Select id,Confirmed_applications__c from Course__C where id in :CourseIds];
	   
	   
	   for (AggregateResult agr :[select count(id) idCount,course_del__c from Course_Application__c where course_del__r.Id in :CourseIds and status__c='Ordered'group by course_del__c])
	   						     
		{
          coursesToassigntsMap.put((ID)agr.get('course_del__c'), integer.valueof(agr.get('idCount')));
         
		}
	   
	  List<Course__C> UpdateList = new List<Course__C>();
		for(course__c ids: CList)
		{
		
		ids.Confirmed_applications__c = coursesToassigntsMap.get(ids.id);
		UpdateList.add(ids);	
	}	

	if(UpdateList.size()>0)
	Update UpdateList;		

 

This was selected as the best answer