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
Raghu Cheruvu 1Raghu Cheruvu 1 

Incompatible value type Object for Map

Hi, 

I am adding getting following error while adding the elements to a map. 

Error:- Incompatible value type Object for Map

Code:-

public class CR_Report{

public void Completed_CR_Report(){
     
         //Compare old zip  on account with the new zip.. if it is different insert the record into
         // CR_Audit_Move__c
         
         
      List<Change_Request_MDV__c> CRCompletedList=new List<Change_Request_MDV__c>([Select Account__c,Name,Status__C,Reason__c,
                                                                                   New_Territory__c,Zip_MDVN__c, State_MDVN__c,CreatedDate,LastModifiedDate
                                                                                from Change_Request_MDV__c
                                                                                where Status__C='On Hold'
                                                                                and Reason__c in ('Completed')
                                                                                and LastModifiedDate=Today]);
      
     Map<String,String> AccCRMap=new Map<String,String>(); 
      
      for(Change_Request_MDV__c c: CRCompletedList){
          
          AccCRMap.put(c.Account__c,c.Id);
          
          
      }
     
      List<Account> AccCRList=new List<Account>([Select Id from Account
                                                 where ID IN:AccCrMap.keyset()]);
                                                 
      Map<String,String> OldAccMap=new Map<String,String>();
      
      Map<String,String> NewAccMap=new Map<String,String>();
      
      List<AccountHistory> AccHistoryList=new List<AccountHistory>([Select AccountId,oldvalue,newvalue from 
                                                                     AccountHistory
                                                                     where field='Territory_vod__c'
                                                                     and AccountId IN:AccCrMap.keyset()]);
                                                                     
         for(AccountHistory ah:AccHistoryList){
             
             oldAccMap.put(ah.AccountId,ah.oldvalue);//>>>>>>>>>>>>>>>> Error Line<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
             
             //NewAccMap.put(ah.AccountId,ah.newvalue);
             
             
         }                                                                
                                                                                                                
      
      
     
     
     
     }


}
siddarth rajsiddarth raj

try declaring like this. It will work.

Map<ID,String> OldAccMap=new Map<ID,String>();
      
      Map<ID,String> NewAccMap=new Map<ID,String>();

                                Or

While adding to Map Concat emptry string (This depends on the purpose of Map)

Thanks
Sid
Raghu Cheruvu 1Raghu Cheruvu 1
Hi,

I tried using the below decalration:-

Map<Id, String> OldAccMap=new Map<String,String>();

When i tried to add elements to the map i still get the same error. 
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@Raghu.,

  Try type casting.,

Change the statement

 oldAccMap.put(ah.AccountId,ah.oldvalue)

to 

 oldAccMap.put(string.valueof(ah.AccountId),String.valueOf(ah.oldvalue))


and see if it works.,

Thanks,
balaji
siddarth rajsiddarth raj

Hi

Sorry Your mistaken in declaraation of Map. Please declare as 
Map<Id, String> OldAccMap=new Map<Id,String>();

Not

Map<Id, String> OldAccMap=new Map<String,String>();


It should work.This will sort your problem

Thanks
Sid

Raghu Cheruvu 1Raghu Cheruvu 1
Hi,

I had mistyped the declaration, 

The declaration I had was:-
Map<Id, String> OldAccMap=new Map<Id,String>();

but still i am getting the same error message..
 
siddarth rajsiddarth raj
What is the type of oldValue, that must be the reason for this Incompatability error as we know Key of the Map is accountId which is ID.  check the type of value of the Map i.e Oldvalue