I am new into the apex development. I am getting an this error in my map:
Invalid initializer type List found for Map expected a Map with the same key and value types, or a valid SObject List
Why do I get this error? How can I fix it?
Here is the code:
Trigger LastOnsiteVisit_SA on Event(Before insert, before update) {
Set<ID> eventIds = new Set<ID>();
Set<ID> AccountIDZ = new Set<ID>();
for (Event evt: Trigger.New) {
eventIds.add(evt.Id);
AccountIDZ.add(evt.AccountId);
}
Set<Account> Accountset = new Set<Account>([SELECT ID FROM Account where ID IN: AccountIDZ]);
Map<Id,Datetime> mp2 = new Map<Id,Datetime> ([SELECT AccountId, EndDateTime FROM Event WHERE RecordTypeId = '01270000000YSks'
AND AccountId IN :AccountIDZ
AND Type = 'Onsite Meeting'
ORDER BY EndDateTime DESC]);
for (Account acct: Accountset) {
if (mp2.containskey(acct.id)) {
acct.Last_Onsite_Visit__c = mp2.get(acct.Id).Date();
}
}
}
Map<Id, Datetime>
by using a queried list ofEvent
. Your mp2 map is of type:Map<Id, Event>
. Also, theId
stored in the map as key will be the Id of theEvent
, not the one of theAccount
.