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
Keith Stephens 18Keith Stephens 18 

Trigger get count

Hello All,
I have the follwoing code and I want to return the count of records.
trigger Pupdate on Procedure__c (before update, before insert) {

Map<Id,AggregateResult> cprCnt = new Map<id,AggregateResult>([SELECT Count(id) maxRates
 		FROM Center_Procedure_Rate__c group by CPT_Code__c, Center__c]);
		
		if (Trigger.isInsert || (Trigger.isUpdate && proc.Amount_To_Pay__c == NULL && proc.Procedure_Cost__c == NULL) || 
					(Trigger.isUpdate && (proc.Procedure_Status__c != 'OK to Pay' && proc.Procedure_Status__c != 'Paid' &&
					   (proc.Amount_To_Pay__c != NULL && proc.Procedure_Cost__c != NULL)))) {
					// get count of Center Procedure Rates that match current procedure
					//centerRateCount = 
					//	[SELECT COUNT(Id) maxRates FROM Center_Procedure_Rate__c 
					//	 WHERE CPT_Code__c = :proc.CPT_Code__c AND Center__c = :proc.Center__c];
						 
					//maxCtrRates = centerRateCount[0].get('maxRates') == null ? 0 : (Integer)centerRateCount[0].get('maxRates');
					
					maxCtrRates = cprCnt.get(proc.Center__c, proc.CPT_Code__c);
					system.debug(maxCtrRates);
		}
}
In the code you can see I am doing it through Soql, but I need to have this bulkified.
we are not hiting our soql limtis.
Thansk,
K
 
Best Answer chosen by Keith Stephens 18
Saravanan Gengan 9Saravanan Gengan 9
Create a map with string and integer. 
Map<String,Integer> map = new Map<String,Integer>(); 
loop through Center_Procedure_Rate__c  and add combination of CPT_Code__c, Center__c as string and count as integer. 
map.add(CPT_Code__c+Center__c, count(Id)) ;
&
Line 14 get from the map like 
map.get(proc.CPT_Code__c+proc.Center__c); 

All Answers

Saravanan Gengan 9Saravanan Gengan 9
Create a map with string and integer. 
Map<String,Integer> map = new Map<String,Integer>(); 
loop through Center_Procedure_Rate__c  and add combination of CPT_Code__c, Center__c as string and count as integer. 
map.add(CPT_Code__c+Center__c, count(Id)) ;
&
Line 14 get from the map like 
map.get(proc.CPT_Code__c+proc.Center__c); 
This was selected as the best answer
Keith Stephens 18Keith Stephens 18
Hello Saravanan,
That is exacly what I did a few days ago, and it works great, but thanks.