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
AbAb 

Change record type of events based on record types of contact

Hello,

I have below code which changes the record type of "Event" based on record type "Account",
How can i modify it to have it for record type of contacts. I want change the record type of event if it belongs to record types C1, C2, C3.
Id a1 = Schema.SObjectType.Account.getRecordTypeInfosByName().get('X').getRecordTypeId();
Id e1 = Schema.SObjectType.Event.getRecordTypeInfosByName().get('Y').getRecordTypeId();
Id e2 = Schema.SObjectType.Event.getRecordTypeInfosByName().get('Z').getRecordTypeId();

List<Event> eventList = [Select AccountId,RecordTypeId from Event where AccountId != null and RecordTypeId=:e1];

Set<Id> accIds = new Set<Id>();

for(Event evt : eventList){
	accIds.add(evt.AccountId);
}

Map<Id,Account> accMap = new Map<id,account>([Select RecordTypeId from Account]);
integer count = 0;
for(Event evt : eventList){
	if(accMap.containsKey(evt.AccountId)){
		if(accMap.get(evt.AccountId).RecordTypeId == a1){
			evt.RecordTypeId = e2;
			count++;
		}
	}
}

update eventList;

It gives me error for below line when i try for Contact
List<Event> eventList = [Select ContactId,RecordTypeId from Event where ContactId != null];



 
Best Answer chosen by Ab
RAM AnisettiRAM Anisetti
minor modification....
Id a1RecordTypeIdOfContact1 = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('X').getRecordTypeId();

Id e1RecordTypeIdOfEvent = Schema.SObjectType.Event.getRecordTypeInfosByName().get('Y').getRecordTypeId();
List<Event> eventList = [Select whoid,AccountId, RecordTypeId from Event where whoid != null];


/*=======i didnt understand what is tha use of this blcock,==============


Set<Id> accIds = new Set<Id>();

for(Event evt : eventList){
   if(evt.AccountId!==null)
	accIds.add(evt.AccountId);
}

*/

Map<Id,Contact> accMap = new Map<id,Contact>([Select RecordTypeId from Contact]);


for(Event evt : eventList){
	if(accMap.containsKey(evt.whoid)){
		if(accMap.get(evt.whoid).RecordTypeId == a1RecordTypeIdOfContact1){
			evt.RecordTypeId = e1RecordTypeIdOfEvent;
			
		}
	}
}

if(eventList.size()>0){
update eventList;
}

All Answers

RAM AnisettiRAM Anisetti
Hi,
try to modifiy like this....
 
List<Event> eventList = [Select whoid,RecordTypeId from Event where whoid!= null];

 
ManojjenaManojjena
Hi Sandrine,
Event has AccountId relationship with Account ,how ever it is related to contact with WhoId .So you need to relace contactid with WhoId ,To know more detail about relationship with event with other objects please check with below link .

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_erd_activities (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_erd_activities.htm)

 
AbAb
If i change my below code like this, will it work
Id a1RecordTypeIdOfContact1 = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('X').getRecordTypeId();

Id e1RecordTypeIdOfEvent = Schema.SObjectType.Event.getRecordTypeInfosByName().get('Y').getRecordTypeId();


List<Event> eventList = [Select whoid,AccountId, RecordTypeId from Event where whoid != null];

Set<Id> accIds = new Set<Id>();

for(Event evt : eventList){
	accIds.add(evt.AccountId);
}

Map<Id,Contact> accMap = new Map<id,Contact>([Select RecordTypeId from Contact]);

integer count = 0;
for(Event evt : eventList){
	if(accMap.containsKey(evt.whoid)){
		if(accMap.get(evt.whoid).RecordTypeId == a1RecordTypeIdOfContact1){
			evt.RecordTypeId = e1RecordTypeIdOfEvent;
			count++;
		}
	}
}

update eventList;


 
RAM AnisettiRAM Anisetti
minor modification....
Id a1RecordTypeIdOfContact1 = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('X').getRecordTypeId();

Id e1RecordTypeIdOfEvent = Schema.SObjectType.Event.getRecordTypeInfosByName().get('Y').getRecordTypeId();
List<Event> eventList = [Select whoid,AccountId, RecordTypeId from Event where whoid != null];


/*=======i didnt understand what is tha use of this blcock,==============


Set<Id> accIds = new Set<Id>();

for(Event evt : eventList){
   if(evt.AccountId!==null)
	accIds.add(evt.AccountId);
}

*/

Map<Id,Contact> accMap = new Map<id,Contact>([Select RecordTypeId from Contact]);


for(Event evt : eventList){
	if(accMap.containsKey(evt.whoid)){
		if(accMap.get(evt.whoid).RecordTypeId == a1RecordTypeIdOfContact1){
			evt.RecordTypeId = e1RecordTypeIdOfEvent;
			
		}
	}
}

if(eventList.size()>0){
update eventList;
}
This was selected as the best answer
AbAb
You have added the null check, but other thatn this do you think that the logic is right ?