+ Start a Discussion
lovetolearnlovetolearn 

Trigger Error: Incompatible key type Object for Map

Please help with this error: Incompatible key type Object for Map. I am trying to find all Cases associated with a custom Worker object. Please also let me know if my trigger does not look right. Here is my code:

 

trigger TestWorkerBulkUpdate on Worker__c (before update) {
	
	Set<ID> workerID = new Set<ID>();
	for (Worker__c worker: Trigger.new)
		workerID.add(worker.ID);
 	
	Map<ID, Integer> caseMap = new Map<Id, Integer>();
	
	for(AggregateResult ar: [SELECT ID, Count(ID) cnts FROM Case WHERE Worker__c =: workerID])
		caseMap.put(ar.get('ID'), ar.get('cnts'));
	
	for (Worker__c worker: trigger.new)	
    	worker.Number_of_all_cases__c = caseMap.get(worker.ID).cnt;
}

 

 Thank you.

Saikishore Reddy AengareddySaikishore Reddy Aengareddy
Casemap is a map of caseid and count...In the last for loop you are trying to get the value from casemao using worker__c id as key.
asish1989asish1989

Hi Love

     Try this

          

trigger TestWorkerBulkUpdate on Account(before update) {
Integer Total;
Set<ID> AccountSETID = new Set<ID>();
for (Account acc: Trigger.new)
AccountSETID .add(acc.ID);

//Map<ID, Integer> caseMap = new Map<Id, Integer>();

for(AggregateResult ar: [SELECT Count(ID) cnts FROM Case WHERE AccountID IN :AccountSETID ])
//caseMap.put(ar.get('ID'), Integer.valueOf(ar.get('cnts')));
Total = Integer.valueOf(ar.get('cnts'));
for (Account acc: trigger.new)
//acc.Number_of_all_cases__c = caseMap.get(acc.ID).cnt;
acc.Account_Id_salesforce__c = Total ;
}

 

Try by adding Wroker in place of Account object ..

 

Did thi solve your problem if so please mark it solved so that others get benifited.

 

Thanks

asish 

asish1989asish1989

Hi Love 

     This is anathor approach you can refer .

          

trigger TestWorkerBulkUpdate on Worker__c (before update) {

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

Map<ID, Integer> caseMap = new Map<Id, Integer>();

for(AggregateResult ar: [SELECT ID, Count(ID) cnts FROM Case WHERE Worker__c IN : workerID])
caseMap.put(string.valueOf(ar.get('ID')), Integer.valueof(ar.get('cnts')));

for (Worker__c worker: trigger.new)
worker.Number_of_all_cases__c = caseMap.get(worker.ID).cnt;
}

 

 

Did this post solve your problem..if so please mark it solved ...so that others get benifited.

 

Thanks

asish

Gunners_23Gunners_23

Problem in the code is you're inserting 'CaseId' as key to the map in the below code snippet

 

for(AggregateResult ar: [SELECT ID, Count(ID) cnts FROM Case WHERE Worker__c =: workerID])
        caseMap.put(ar.get('ID'), ar.get('cnts'));

 

Whereas while retrieving the value from the map you're passing the Worker Id as key

 

for (Worker__c worker: trigger.new)    
        worker.Number_of_all_cases__c = caseMap.get(worker.ID).cnt;

asish1989asish1989

Hi Love

   For best pratice we need to avoid for loop if possible. so try this...

    

 

trigger TestWorkerBulkUpdate on Worker__c (before update) {

        Sting querystring = '[select Id from Worker__c]';  

Set<ID> workerID = new Set<ID>(database.query(querystring));  

 Map<ID, Integer> caseMap = new Map<Id, Integer>();

 for(AggregateResult ar: [SELECT ID, Count(ID) cnts FROM Case WHERE  Worker__c IN : workerID])   

caseMap.put(string.valueOf(ar.get('ID')), Integer.valueof  (ar.get('cnts')));

 for (Worker__c worker: trigger.new)  

 worker.Number_of_all_cases__c = caseMap.get(worker.ID).cnt;

 }

 

if we avoid loop we can decrease number of script .

 

Thanks

asish

sfdcfoxsfdcfox
Map<Id,AggregateResult> sums = new map<id,aggregateresult>([select worker__c id, count(id) cases from case where worker__c in :trigger.newmap.keyset() group by worker__c]);
for(worker__c worker:trigger.new) {
    worker.number_of_all_cases__c = (decimal)sums.get('cases');
}

 

lovetolearnlovetolearn

Thanks everyone.

 

I realize the mistake that I made in SOQL query. So instead of querying for the Case ID, I queried for the Worker ID. But I am still getting the same error. So I editted the mapping a bit as ASISH suggested. I am getting this error now: "Error: Compile Error: Initial term of field expression must be a concrete SObject: Integer" on the last line. Here is my code: 

 

 

trigger TestWorkerBulkUpdate on Worker__c (before update) {
	
	Set<ID> workerID = new Set<ID>();
	for (Worker__c worker: Trigger.new)
		workerID.add(worker.ID);
 	
	Map<ID, Integer> caseMap = new Map<Id, Integer>();
	
	for(AggregateResult ar: [SELECT Worker__c, Count(ID) cnts FROM Case WHERE Worker__c =: workerID])
		caseMap.put(string.valueof(ar.get('ID')), integer.valueof(ar.get('cnts')));
	
	for (Worker__c worker: trigger.new)	
    	worker.Number_of_all_cases__c = caseMap.get(worker.ID).cnt;
}

 Please help. Thank you.

asish1989asish1989

Hi

  Try this

        for(AggregateResult ar: [SELECT Worker__c, Count(ID) cnts FROM Case WHERE Worker__c =: Trigger.new[0].Id)  {

String str = string.valueof(ar.get('ID'));   

caseMap.put(str , integer.valueof(ar.get('cnts')));  

}

 

     Integer Total  = caseMap.get(str);

Did this post solve your problem if so please mark it solved so that others can get benifited .

 

Thanks

asish

lovetolearnlovetolearn

But I am not trying to update the total number of case field with the ID. Instead I want the count.

asish1989asish1989

Hi Love

    Please let me clear ,You want total number of case of a perticular Worker__c record or simple total no of case of all record of Worker__c object .

 

 

Thanks

asish

lovetolearnlovetolearn

Sorry about that, I should've specify. I want a the number of Case record associated with each worker. I need this trigger to be a bulkify because I need to update 5000 records in one update call. 

asish1989asish1989

Hi Love

   Sroy for asking . But I need to know about relationship between Case and Worker__c object .

    I think Worker__c has a lookup relationship field of Case . If so please tell me the Lookup filed name .

 

Its Emergency ..

 

 

Thanks

asish

lovetolearnlovetolearn

No the Worker__c object does not have a lookup relationship field to Case. However, the Case object does have a lookup relationship field to Worker__c. It's called Worker__c. 

asish1989asish1989

Hi Love

 

     Try this

             trigger TestWorkerBulkUpdate on Worker__c (before update) {

     if(trigger.new[0].Id != NULL) {  

for(AggregateResult ar: [SELECT ID, Count(ID) cnts FROM Case WHERE Worker__c =: Trigger.new[0].Id)  

 Integer Total = Integer.valueOf(sobj.get('cnts'));  

}

}

 

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

 

Thanks

asish

lovetolearnlovetolearn

Hi Asish, 

 

That works, but that does not solve my problem of processing 5000 records in one update call. That will only process one record at a time. 

asish1989asish1989

Hi Love

    Try this

       trigger TestWorkerBulkUpdate on Worker__c (before update) {
           Set<ID> workerID = new Set<ID>();

          for (Worker__c worker: Trigger.new)

               workerID.add(worker.ID);

          for(AggregateResult ar: [SELECT ID, Worker__c  , Count(ID) cnts FROM Case WHERE Worker__c IN : workerID]){

                  Integer Total = Integer.valueOf(sobj.get('cnts')); 

       }

 

Did this post solve your problem if so please mark it solved otherwise let me know about your issue

 

Thanks

asish

 

lovetolearnlovetolearn

Hi, 

 

I added a few extra line to the code that you provided: 

 

for(Worker__c worker : trigger.new){
     if (worker.isTerminated == true){
        worker.total_number_of_cases__c = total;
     }
}

 

I didn't get an error, but the field did not update.

 

asish1989asish1989

Hi Love

   Try this

        

trigger TestWorkerBulkUpdate on Worker__c (before update) {

      for(Worker__c worker : trigger.new){  

             for(AggregateResult ar: [SELECT ID, Worker__c  , Count(ID) cnts FROM Case WHERE Worker__c =: worker.ID]){

                     Integer Total = Integer.valueOf(sobj.get('cnts'));  

               }

 

                    if (worker.isTerminated == true){  

                           worker.total_number_of_cases__c = Total;  

                     }

}

 

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

 

Thanks

asish

 

 

lovetolearnlovetolearn

Hi Asish, 

 

That's the exact code that I used, but its not working. It's not giving me an error, but field is not updating. 

asish1989asish1989

Hi Love

     Try this

     

trigger TestWorkerBulkUpdate on Worker__c (before update , before Insert) {

      for(Worker__c worker : trigger.new){ 

             for(AggregateResult ar: [SELECT ID, Worker__c  , Count(ID) cnts FROM Case WHERE Worker__c =: worker.ID]){

                     Integer Total = Integer.valueOf(sobj.get('cnts')); 

               }

 

                    if (worker.isTerminated == true){ 

                           worker.total_number_of_cases__c = Total; 

                     }

}

 

please try creating new record  and updating Existing record of Worker__c object .

 

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

 

Thanks

asish