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
Josh Davis 47Josh Davis 47 

APEX code working with Polymorphic ID (Event and Case)

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)

I am having a hard time figuring out how I work with values from two seperate arrays to come up with a calculation.  I was trying to use MAPs but I wasn't quite able to get it to work.

Can someone explain the approach?  Code examples would be great but I am more interested in the approach of how to do this...  So here is an example:

My SOQL Query to build the map between event and case
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.
 
Best Answer chosen by Josh Davis 47
Andrew GAndrew G
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
List<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;
    }
}

HTH

Andrew

All Answers

Andrew GAndrew G
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
List<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;
    }
}

HTH

Andrew
This was selected as the best answer
Josh Davis 47Josh Davis 47
Thank you Andrew! That was exactly what I was looking for!  Appreciate it!