I understand the polymorphic field issue around tasks and activities (sort of). I've been able to get around what I need using a custom report type so far, however, I now have a new requirement. I need to display a field that is on the Account record that the activity is related to and/or a checkbox field that is on the opportunity object that the task or event is related to. How can I do this? Thank you in advance for any help! I've not written a trigger but would be willing to try if I have guidance.
-
I'm not sure what you're after. Do you need to display these fields on a report or do you need to access them in code? Or are you thinking about formula fields to show it on Event's detail page? Or ... do you think that some workaround to store values in helper field on Task/Event will be needed and that's why you've tagged the question as trigger-related?– eyescreamFeb 22, 2014 at 18:59
-
Thanks for your reply. The business case is that activities are logged against opptys and accounts. Some of these acccounts and opptys have been marked as "priority". So the report is an activity report that needs to also display the fields from the acct or oppty object that tell them it is one of their priority accts or opptys.– Tracy6982Feb 23, 2014 at 20:42
2 Answers
I'm afraid I still don't understand what exactly gives you trouble :/
I need to display a field that is on the Account record that the activity is related to and/or a checkbox field that is on the opportunity object that the task or event is related to.
Task and Event objects have these weird polymorphic fields WhoId/WhatId
. But if you'll read the Task spec you'll realize there's a hidden field AccountId
:
ID of the related Account. This is the account of
WhatId
if it exists, otherwise it’s the account ofWhoId
if it exists, otherwise it’s set tonull
.
So - even though the formula editor doesn't indicate it's possible - you can create custom field on activity of type formula and have there Account.Priority__c
for example. And creation of such fields usually is easier than making a trigger to fill them in.
You can also access it in queries:
SELECT AccountId, Account.Name
FROM Task
WHERE WhatId = '0067000000AH3ME'
And last but not least - the standard "Activities with Opportunities" report type should out-of-the-box allow you to use Activity, Opportunity and Account fields in the report?
Have you tried making a report with filters "(Opportunity) Priority = true OR (Account) Priority = true"? The "OR" can be set by using the dropdown next to "Filter: Add" in the report builder, you need to select "Filter logic".
Use Describe results to determine what the RelatedTo ID type is...
i.e.
if(t.WhatID != null){
if( ( (String)t.whatID).startsWith(Account.sObjectType.getDescribe().getKeyPrefix()){
//This is an account, process here
}else if( ( (String)t.whatID).startsWith(Opportunity.sObjectType.getDescribe().getKeyPrefix()){
//This is an Opportunity process here
}
}