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
ajinkya shahane 1ajinkya 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();

  }
 }
}
Best Answer chosen by ajinkya shahane 1
Amit Chaudhary 8Amit Chaudhary 8
Hi ajinkya shahane 1 / Akhilesh Reddy Baddigam,

You are trying to update Account record from Event trigger so you need to update the account record like below
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,Last_Onsite_Visit__c FROM Account where ID IN: AccountIDZ]);
	Id eventRecordTypeId = Schema.SObjectType.Event.getRecordTypeInfosByName().get('Relationship Management Event').getRecordTypeId();
	list<Event> eventList= [SELECT AccountId, EndDateTime FROM Event WHERE RecordTypeId = :eventRecordTypeId
											  AND AccountId IN :AccountIDZ
											  AND Type = 'Onsite Meeting'
											  ORDER BY EndDateTime DESC];

	Map<id,Datetime> mp3=new Map<id,Datetime>();
	for(event e:eventList){
		mp3.put(e.id,e.EndDateTime);
	}

	List<Account> lstAccToUpdate = new List<Account>();
	
	for (Account acct: Accountset) {
		if (mp3.containskey(acct.id)) {
			acct.Last_Onsite_Visit__c = mp3.get(acct.Id).EndDateTime;
			lstAccToUpdate.add(acct);
		}
	}
	
	if(lstAccToUpdate.size() > 0 )
	{
		update lstAccToUpdate;
	}
}

or like below
Trigger LastOnsiteVisit_SA on Event(Before insert, before update)
{
    Set<ID> eventIds = new Set<ID>();
    Set<ID> AccountIDZ = new Set<ID>();


    Id eventRecordTypeId = Schema.SObjectType.Event.getRecordTypeInfosByName().get('Relationship Management Event').getRecordTypeId();

    for (Event evt: Trigger.New)
    {
		if(evt.RecordTypeId ==eventRecordTypeId && evt.Type == 'Onsite Meeting' )
		{
			eventIds.add(evt.Id);
			AccountIDZ.add(evt.AccountId);
		}	
    }

    


    Map<Id,Account> mp2 = new Map<Id,Account>( [SELECT id,Last_Onsite_Visit__c FROM Account WHERE  id in :AccountIDZ ] );
	List<Account> lstAccToUpdate = new List<Account>();
	
    for (Event evt: Trigger.New ) 
    {
		if(evt.RecordTypeId ==eventRecordTypeId && evt.Type == 'Onsite Meeting' )
		{
			if (mp2.containsKey(evt.AccountId))
			{
				Account acct = mp2.get(evt.AccountId);
				acct.Last_Onsite_Visit__c = evt.EndDateTime;
				lstAccToUpdate.add(acct);
			}
		}
    }
	if(lstAccToUpdate.size() > 0 )
	{
		update lstAccToUpdate;
	}
}


Let us know if this will help you

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Dnt add hardcoded Id. Please add your Record Type name in below like.

Id eventRecordTypeId = Schema.SObjectType.Event.getRecordTypeInfosByName().get('NameOfRecordType').getRecordTypeId();

Try to update your code like below
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('NameOfRecordType').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 ) 
	{
		if (mp2.containsKey(acct.id)) 
		{
			acct.Last_Onsite_Visit__c = mp2.get(acct.Id).EndDateTime;
		}
	}
}

Let us know if this will help you
 
Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
Hi shahane to auto populate maps with SOQL queries (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_map_sobject.htm) you need to declare values as events in this case. please use this below approach to achieve the same. 
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]);

 list<Event> eventList= [SELECT AccountId, EndDateTime FROM Event WHERE RecordTypeId = '01270000000YSks'
                                              AND AccountId IN :AccountIDZ
                                              AND Type = 'Onsite Meeting'
                                              ORDER BY EndDateTime DESC];
    
    Map<id,Datetime> mp3=new Map<id,Datetime>();
    for(event e:eventList){
        mp3.put(e.id,e.EndDateTime);
        
    }
    
 for (Account acct: Accountset) {
  if (mp3.containskey(acct.id)) {
   acct.Last_Onsite_Visit__c = mp3.get(acct.Id).Date();

  }
 }
}
If this solves your problem, please choose this as the best answer.
Thank you!
ajinkya shahane 1ajinkya shahane 1
Hello Amit,

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();
 
        }
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
It look like you want to update Last_Onsite_Visit__c on account object ? if yes then your code should be like below
Trigger LastOnsiteVisit_SA on Event(Before insert, before update)
{
    Set<ID> eventIds = new Set<ID>();
    Set<ID> AccountIDZ = new Set<ID>();


    Id eventRecordTypeId = Schema.SObjectType.Event.getRecordTypeInfosByName().get('Relationship Management Event').getRecordTypeId();

    for (Event evt: Trigger.New)
    {
		if(evt.RecordTypeId ==eventRecordTypeId && Type = 'Onsite Meeting' )
		{
			eventIds.add(evt.Id);
			AccountIDZ.add(evt.AccountId);
		}	
    }

    


    Map<Id,Account> mp2 = new Map<Id,Account>( [SELECT id,Last_Onsite_Visit__c FROM Account WHERE  id in :AccountIDZ ] );
	List<Account> lstAccToUpdate = new List<Account>();
	
    for (Event evt: Trigger.New ) 
    {
		if(evt.RecordTypeId ==eventRecordTypeId && Type = 'Onsite Meeting' )
		{
			if (mp2.containsKey(evt.AccountId))
			{
				Account acct = mp2.get(evt.AccountId);
				acct.Last_Onsite_Visit__c = evt.EndDateTime;
				lstAccToUpdate.add(acct);
			}
		}
    }
	if(lstAccToUpdate.size() > 0 )
	{
		update lstAccToUpdate;
	}
}


 
ajinkya shahane 1ajinkya shahane 1
Hello Akhilesh,

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
Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
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,Last_Onsite_Visit__c FROM Account where ID IN: AccountIDZ]);

 list<Event> eventList= [SELECT AccountId, EndDateTime FROM Event WHERE RecordTypeId = '01270000000YSks'
                                              AND AccountId IN :AccountIDZ
                                              AND Type = 'Onsite Meeting'
                                              ORDER BY EndDateTime DESC];
    
    Map<id,Datetime> mp3=new Map<id,Datetime>();
    for(event e:eventList){
        mp3.put(e.id,e.EndDateTime);
        
    }
    
 for (Account acct: Accountset) {
  if (mp3.containskey(acct.id)) {
  
   acct.Last_Onsite_Visit__c = mp3.get(acct.Id).EndDateTime;

  }
 }
}

ajinkya shahane 1ajinkya shahane 1
Hello Amit,

On 11th and 26th Row getting an error as "Expression cannot be assigned"

Thanks once agian
Amit Chaudhary 8Amit Chaudhary 8
Hi ajinkya shahane 1 / Akhilesh Reddy Baddigam,

You are trying to update Account record from Event trigger so you need to update the account record like below
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,Last_Onsite_Visit__c FROM Account where ID IN: AccountIDZ]);
	Id eventRecordTypeId = Schema.SObjectType.Event.getRecordTypeInfosByName().get('Relationship Management Event').getRecordTypeId();
	list<Event> eventList= [SELECT AccountId, EndDateTime FROM Event WHERE RecordTypeId = :eventRecordTypeId
											  AND AccountId IN :AccountIDZ
											  AND Type = 'Onsite Meeting'
											  ORDER BY EndDateTime DESC];

	Map<id,Datetime> mp3=new Map<id,Datetime>();
	for(event e:eventList){
		mp3.put(e.id,e.EndDateTime);
	}

	List<Account> lstAccToUpdate = new List<Account>();
	
	for (Account acct: Accountset) {
		if (mp3.containskey(acct.id)) {
			acct.Last_Onsite_Visit__c = mp3.get(acct.Id).EndDateTime;
			lstAccToUpdate.add(acct);
		}
	}
	
	if(lstAccToUpdate.size() > 0 )
	{
		update lstAccToUpdate;
	}
}

or like below
Trigger LastOnsiteVisit_SA on Event(Before insert, before update)
{
    Set<ID> eventIds = new Set<ID>();
    Set<ID> AccountIDZ = new Set<ID>();


    Id eventRecordTypeId = Schema.SObjectType.Event.getRecordTypeInfosByName().get('Relationship Management Event').getRecordTypeId();

    for (Event evt: Trigger.New)
    {
		if(evt.RecordTypeId ==eventRecordTypeId && evt.Type == 'Onsite Meeting' )
		{
			eventIds.add(evt.Id);
			AccountIDZ.add(evt.AccountId);
		}	
    }

    


    Map<Id,Account> mp2 = new Map<Id,Account>( [SELECT id,Last_Onsite_Visit__c FROM Account WHERE  id in :AccountIDZ ] );
	List<Account> lstAccToUpdate = new List<Account>();
	
    for (Event evt: Trigger.New ) 
    {
		if(evt.RecordTypeId ==eventRecordTypeId && evt.Type == 'Onsite Meeting' )
		{
			if (mp2.containsKey(evt.AccountId))
			{
				Account acct = mp2.get(evt.AccountId);
				acct.Last_Onsite_Visit__c = evt.EndDateTime;
				lstAccToUpdate.add(acct);
			}
		}
    }
	if(lstAccToUpdate.size() > 0 )
	{
		update lstAccToUpdate;
	}
}


Let us know if this will help you
This was selected as the best answer
ajinkya shahane 1ajinkya shahane 1
Hey Amit and Akhilesh,

Thank you so Much both of you for your time. Amit The code below "or like below " statement worked.
Thanks a lot
Cheers:)