I am working with events and cases but am having trouble on the concepts of joining two queries. I need to query all events that:
- completed yesterday
- are related to a case (WhatId starts with 500)
- are of event Type = 'Example'
Each of these are related to a case by definition of the original query, I now need to go through each of the cases and determine what the caseType__c is to determine the next_due_date__c which is a custom date field on the case which should calculate as:
- Event Date + (If caseType__c = "Example" + 14, 60)
Map<WhatId, Event> eventCaseMap = new Map<WhatId, Event>([Select Id, WhatId, ActivityDate, Ended_Yesterday__c From Event WHERE WhatId LIKE '500%' AND Type='Example' AND Ended_Yesterday__c = TRUE]);
Now that I have the map, I need to iterate through this to determine the caseType__c value to calculate the days until due, and then add that to the event ActivityDate so that I can update the case next_due_date__c.
If all the values existed on the same object I can get it to work, but trying to make a calculation off of two of them is where my problem is. Thanks in advance for any help/direction.My experience is that the WhatId is a special, oh so special field. Basically, i've never managed to get the related record detail using the WhatId field in a similar manner to perhaps:
List<Contact> contacts = new List<Contact>();
contacts = [SELECT Id, Name, AccountId, Account.Name From Contact];
for (Contact c : contacts ){
System.debug('$$$DEBUG' + c.Account.Name);
}
So I have generally done it as a two step process, something like below
HTHAndrewList<Id> whatIds = new List<Id>();
Event[] eventList = [SELECT Id, WhatId, What.Type, ActivityDate From Event WHERE What.TYpe IN('Case')];
for(Event e : eventList) {
System.debug('%%%DEBUG:' + e); //doesn't show .Type even though the query does not throw a syntax error or unrecognised field error
whatIds.add(e.WhatId);
}
Map<Id,Case> caseMap = new Map<Id,Case>([SELECT Id, Type FROM Case WHERE Id IN :whatIds]);
for(Event e : eventList){
Case thisCase = caseMap.get(e.WhatId);
if (thisCase.Type == 'Some Case Type') {
//do some computation and popuat the Event record
e.description = thisCase.Type;
}
}