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
RamakarryRamakarry 

Incompatible key type Datetime for Map<String,List<Event>>

hai friends !
Can any one plz fix the issue
Compile Error: Incompatible key type Datetime for Map<String,List<Event>> at line 51 column 1 for the bellow code
code:
public with sharing class samplecls{ 
public Event e;    
public list<Event> Events{set;get;}
public  map<string, List<Event>> eventmap{set;get;}
public Boolean abool {get;set;}
public samplecls(){
eventmap=new map<string, List<Event>>(); 
}
public samplecls(ApexPages.StandardController controller) {
    e=(Event)controller.getrecord();   
    }   
//fetch the Events between two required dates
public PageReference Query() {
abool = false; 
if(e.StartDateTime<e.EndDateTime){
Events=new list<Event>();
Events.clear(); 
eventmap=new map<string, List<Event>>();  
Events = [Select Subject,StartDateTime,EndDateTime,Owner.Name,CreatedDate from Event where StartDateTime >=: e.StartDateTime and EndDateTime<=:e.EndDateTime order BY StartDateTime];
system.debug('Events'+Events );
if(Events.size()>0){abool = true;}
Datetime d;
    for (Event e : Events){ 
             TimeZone tz=UserInfo.getTimeZone();  
             string t1 = e.StartDateTime.format('dd/MM/yyyy hh:mm a',tz.getId());
             e.StartDateTime = DateTime.parse(t1);
             string t2 = e.EndDateTime.format('dd/MM/yyyy hh:mm a',tz.getId());
             e.EndDateTime = DateTime.parse(t2);              
    if(e.StartDateTime!=null )
          {
           d=e.startdatetime;      
          list<event> mapevents=new list<event>();
                    for(Event che: Events )
              {
              if(d==che.startdatetime)
              mapevents.add(che);  
if(eventmap.get(che.StartDateTime)== null)  {
 eventmap.put(string.ValueOf(d.format('dd-MM-yyyy-EEEE')),mapevents );
         System.debug('mapelementstype1'+eventmap);
       }
else
eventmap.get(d).add(che);    
system.debug('Event Map type2'+eventmap);
               } 
               }
            }
          }
       
       else
       {
        if((e.StartDateTime==null || e.EndDateTime==null))
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please Enter valid Date'));
        else
        if(e.StartDateTime>e.EndDateTime)
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'End date should be grater than start Date'));
        }
      return null;  
    }
    }
Abhishek BansalAbhishek Bansal
Hi Amruta,

I think you are getting the error because the Key of your map "eventmap" is of String type and at line no. 37 you are passing a DateTime variable (che.StartDateTime) to get the value of map.
Please change Line no.37 i.e. if(eventmap.get(che.StartDateTime)== null)  { from below line :
if(eventmap.get(string.ValueOf(d.format('dd-MM-yyyy-EEEE'))== null)  {
Please let me know if you have any futher issue on this or if you want more help/information on this.

Thanks,
Abhisehk Bansal.