You need to sign in to do that
Don't have an account?
ajinkya shahane 1
Invalid initializer type List found for Map expected a Map with the same key and value types, or a valid SObject List
Hello All,
I am trying to write a trigger and getting this error.
here's 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();
}
}
}
I am trying to write a trigger and getting this error.
here's 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();
}
}
}
You are trying to update Account record from Event trigger so you need to update the account record like below
or like below
Let us know if this will help you
All Answers
Id eventRecordTypeId = Schema.SObjectType.Event.getRecordTypeInfosByName().get('NameOfRecordType').getRecordTypeId();
Try to update your code like below
Let us know if this will help you
Thank you!
Thanks for your reply. I have updated the code here
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);
}
Id eventRecordTypeId = Schema.SObjectType.Event.getRecordTypeInfosByName().get('Relationship Management Event').getRecordTypeId(); // Add Name here
Map<Id,Event> mp2 = new Map<Id,Event>([SELECT AccountId, EndDateTime FROM Event WHERE RecordTypeId = :eventRecordTypeId
AND AccountId IN :AccountIDZ
AND Type = 'Onsite Meeting'
ORDER BY EndDateTime DESC]
);
for (Account acct: Trigger.New ) // getting error here as "Invalid loop variable type expected Event was Account"
{
if (mp2.containsKey(acct.id))
{
acct.Last_Onsite_Visit__c = mp2.get(acct.Id).EndDateTime.date();
}
}
}
Thanks For Your reply. The code clears all the errors but value in acct.Last_Onsite_Visit__c is blank. no value is populating there. i am trying to figure it out , your help is appreciated
On 11th and 26th Row getting an error as "Expression cannot be assigned"
Thanks once agian
You are trying to update Account record from Event trigger so you need to update the account record like below
or like below
Let us know if this will help you
Thank you so Much both of you for your time. Amit The code below "or like below " statement worked.
Thanks a lot
Cheers:)