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 

Assigning an sobject to a map using .get

Hi

 

I am trying to use the following code for a bulkified trigger to roll up and update the number of course applications for each course.  I then want to update each course with the aggregate value into the 'numberOfCourseApps' field.  I'm pretty sure that I have the right syntax but get an error - incompatible key type eor Object for MAP.

 

Is this possible ? If not how can I then take the course ID and update the 'numberOfCourseApps' field

 Map<course__c, Integer> coursesToassigntsMap = new Map<course__c, Integer>();
	   set<Id> courseIds = new Set<Id>();
	   for(Course_Application__c a: trigger.new){
		   courseIds.add(a.Id);	   
   		}
	   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(agr.get('course_del__c'), integer.valueof(agr.get('idCount')));
		}

 

Best Answer chosen by Admin (Salesforce Developers) 
lescclarkcpdlescclarkcpd

i solved it with this

 

 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

Sagarika RoutSagarika Rout

Hello

 

What evere you have written the logic, it is perfectly right but the Map declaration is wrong.

First thing you need to understand , for map first we declare the key , then values,(for unique identification of records)

 

below is the right syntax for declaring a Map

 

Map<Integer,course__c> coursesToassigntsMap = new Map<Integer,course__c>();

 

lescclarkcpdlescclarkcpd

but i wanted set the key as the object, each object has a value asssigned to it

lescclarkcpdlescclarkcpd

i solved it with this

 

 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