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);
system.debug('===strToDec=='+strToDec);
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.