I am totally new at this and this actually one of my first triggers so I'm really proud that it is working (when used on a Case__c object) but it is not working when triggered on any other object. I'm probably making a rookie mistake here. We are a law firm so I wrote a trigger to make a billable record when specific strings of text are contained in a chatter post. This trigger works well for the Case__c object but I'm trying to include an "if" statement that will also allow it to work on a Task object.

trigger ChatterBillable on FeedItem (after insert, after update) {

    for (FeedItem f: trigger.new){
        if (f.body.contains('$b') || f.body.contains('$B')){
            Billable__c b               = new Billable__c();
            b.Billable_Date__c          = Date.today();
            String billtime             = f.body.substringAfter('=').substringBefore('hr');
            Decimal strToDec            = decimal.valueOf(billtime);
            b.Time__c                   = strToDec;
            b.Person_Billing__c         = f.CreatedById;
            b.Internal_Description__c   = f.Body;
            String xd                   = f.body.substringAfter('XD').substringBefore('::').replaceAll('<[^>]+>',' ');
            String idType               = f.ParentId.getSobjectType().getDescribe().getName();
            b.Service_Note__c           = xd + idType;
            b.RecordTypeId              = '012d0000001taJa';
            //if the chatter post is on a Case__c record this code is working by creating a Billable_c record as intended
            if (idType == 'Case__c') {
                b.Case__c       = f.ParentId;
            // However, if the chatter post triggering this code is on a Task record I want to relate the Billable__c record 
            // to the same Case__c record as the Task is related to.
            } else if (idType   == 'Task'){
                b.Case__c       = f.ParentId.What;
            insert b;

I'm getting an error on the line 23 "Invalid foreign key relationship: FeedItem.ParentId"

b.Case__c = f.ParentId.What;

I wasn't sure if I was accessing the relationship record correctly so I also tried

b.Case__c = f.Parent.What;

And with this change I get an error "Invalid field What for SObject Name." This is confusing me, I went to look at the API names for Task and "What" appears to be the API name for a lookup relationship field which includes Case__c. Anyone know what I'm doing wrong? Help would be very much appreciated.

2 Answers 2


The field is WhatId. However, you have to query for the parent records, as you don't get cross-object data in a trigger context.

String taskPrefix = SObjectType.Task.getKeyPrefix();
Set<Id> taskIds = new Set<Id>();
for (FeedItem record : trigger.new)
    if (record.ParentId.getSObjectType() == Task.sObjectType)

Map<Id, Task> tasks = new Map<Id, Task>([SELECT WhatId FROM Task WHERE Id IN :taskIds]);
for (FeedItem record : trigger.new)
    Task parentTask = tasks.get(record.ParentId);
    if (parentTask != null)
        Id parentWhatId = parentTask.WhatId;

That said, you should also remove your insert statement from your loop:

List<Billable__c> billableRecords = new List<Billable__c>();
for (Task record : trigger.new)
    billableRecords.add(new Billable__c(
        // populate fields
insert billableRecords;
  • Ok. I did change to WhatId and I'm still getting an error, but I understand from your comment that this may be a result of trying to get cross-object data. So I will take a stab at implementing your code. I'm brand new at this so may take me a minute to implement this.
    – Sethball
    May 11, 2017 at 16:22
  • I appreciate your feedback. I looked at your code, and tried copying it and modifying it to fit into my trigger. Unfortunately I'm not knowledgeable enough to understand the code. I'm actually brand new at this and have a very limited experience with triggers only. Would you mind showing me where or how in my code I would insert this?
    – Sethball
    May 12, 2017 at 0:33
  • Let's chat.
    – Adrian Larson
    May 12, 2017 at 0:35

It should be b.Case__c = f.Parent.WhatId;

as you need to assign the WhatId to CaseId

Refer Understanding Polymorphic Keys and Relationships

  • Don't use What.Id where you can use WhatId. :)
    – Adrian Larson
    May 11, 2017 at 15:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .