+ Start a Discussion
lovetolearnlovetolearn 

Help on Error

Hi, 

 

I am trying to write a trigger that will count all the Case record for a Worker__c custom object record. I have about 6000 records that i need to update in one shot. Here is the error message that I keep getting: Compile Error: Initial term of field expression must be a concrete SObject: Integer on my last line of code. Here is my code:

 

trigger WorkerBulkUpdate on Worker__c (after update) {
    
    Set<ID> workerID = new Set<ID>();
    for (Worker__c worker: Trigger.new){
        workerID.add(worker.ID);
    
    Map<Worker__c, Integer> caseMap = new Map<Worker__c, Integer>();
    
    for(AggregateResult ar: [SELECT Worker__c, Count(ID) cnts FROM Case WHERE worker__c =: worker
                             GROUP BY Worker__c]){
        caseMap.put(worker, Integer.valueOf(ar.get('cnts')));
        
        if(worker.isUpdated__c == true){}
        worker.Number_of_all_cases__c = caseMap.get(worker).cnts;
    }
    }

 

Please help. Thank you. 

 

asish1989asish1989

Hi Love 

      Try this ..This is the final solutions . 

                    

                   

trigger WorkerBulkUpdate on Worker__c (before update) {

    Set<ID> workerID = new Set<ID>();
    for (Worker__c worker: Trigger.new){
          workerID.add(worker.ID);

           Map<Worker__c, Integer> caseMap = new Map<Worker__c, Integer>();

            for(AggregateResult ar: [SELECT Worker__c, Count(ID) cnts FROM Case WHERE worker__c =: workerID
                                                                                        GROUP BY Worker__c]){
                         caseMap.put(worker, Integer.valueOf(ar.get('cnts')));

                        if(worker.isUpdated__c == true){}
                    worker.Total_No_Of_Cases__c = caseMap.get(worker);
             }
         }
}

 

You have to write Number_of_cases__c in the place of  Total_No_Of_Cases__c  ..

Now Edit some worker record and save it will update Number_of_cases__c .

 

Did this post solve your problem If so please mark it solved otherwise let me know about your Issue.

 

Thanks

asish

 

 

 

cloudElephantcloudElephant

Hi lovetolearn,

 

get method for your map will return count value. So you can update the line to:

 

worker.Number_of_all_cases__c = caseMap.get(worker);

However, I found that you can optimize your code. Also you are performing aggregate query within a for loop which should be avoided. I have updated your code as below:

 

trigger WorkerBulkUpdate on Worker__c (after update)
{
	// For loop eliminated
	Set<ID> workerID = Trigger.newMap.keySet();

	// Maintain a map for worker Id to it's count
	Map<Id, Integer> caseMap = new Map<Id, Integer>();

	for(AggregateResult ar: [	SELECT Worker__c, Count(ID) cnts 
					FROM Case 
					WHERE worker__c IN :workerID
					GROUP BY Worker__c	])
	{
		caseMap.put(ar.get('Worker__c'), Integer.valueOf(ar.get('cnts')));
	}
	
	for(Worker__c worker : Trigger.new)
	{
		if(worker.isUpdated__c == true){}
			worker.Number_of_all_cases__c = caseMap.get(worker.Id);
	}
}

 

Please let me know if this helps.

 

lovetolearnlovetolearn

Hi, 

 

Thank you guys so much for your help. Thanks Asish. I finally figured it out. However, now I have second problem. The Worker records with 0 cases are getting a null value for the Total Number of Cases field. Here is my code. I don't want this, I acutally want the field to show 0 if there are no Cases associated with the Worker record. Below is my updated code. Please let me know where i went wrong. 

 

 

trigger WorkerBulkUpdate on Worker__c (before update) {
    
    Set<ID> workerID = Trigger.newMap.keyset();
    
    Map<ID, Integer> caseMap = new Map<ID, Integer>();
    
    LIST<AggregateResult> aloc = [SELECT Worker__c, Count(ID) cnts FROM Case WHERE Worker__c IN: workerID 
                                  GROUP BY Worker__c];
    
    for(AggregateResult ar: aloc){ 
        if(aloc.size() < 1){
            caseMap.put(String.valueOf(ar.get('Worker__c')), 0);
        }else{
            caseMap.put(String.valueof(ar.get('Worker__c')), Integer.valueOf(ar.get('cnts')));
        }
    }
    for(Worker__c worker : Trigger.new){
    
        if(worker.isTerminated__c == true){
                worker.Number_of_all_cases__c = caseMap.get(worker.ID);
                System.debug(caseMap.get(worker.ID));
        }
    }
}    

 

cloudElephantcloudElephant

You'll need to perform an additional null check. Here you go.

 

.
.
if(worker.isTerminated__c == true){ worker.Number_of_all_cases__c = (caseMap.get(worker.Id) == null) ? 0 : caseMap.get(worker.Id); System.debug(caseMap.get(worker.ID)); } }