I'm trying to get the name of whatId and WhoId on the event object onto a string. I can do it for a single record inside a for loop, the issue I see when Im looking to bulkify the code is that events can be related to multiple objects and I need to query this dynamically while keeping a reference to the original record. my current code snippet for a single record is below
for(event e : eventList){
//get the name of the objects
String whatIdsObjectType = e.whatId.getSObjectType().getDescribe().getName();
String whoIdsObjectType = e.whoId.getSObjectType().getDescribe().getName();
//assign variables to the ids
id accId = e.whatId;
id pcID = e.whoId;
//query the name fields dymanically
string whatQueryString = 'Select Name FROM '+whatIdsObjectType+' WHERE id =:accId';
string whoQueryString = 'select Name FROM '+whoIdsObjectType+' WHERE id =:pcID';
List<sObject> relatedToName = Database.query(whatQueryString);
List<sObject> primaryContactName = Database.query(whoQueryString);
//set values for the text fields on before update,before insert
e.related_to_name__c = string.valueOf(relatedToName[0].get('Name'));
e.primary_contact_name__c = string.valueOf(primaryContactName[0].get('Name'));
}