I have a trigger that collects Events without a related to and pushes them to a Future Method class - because events added via outlook don't have a WhatId! - and then publishes a Chatter post on the Account's feed.
I want to Chatter an Event summary like this, but for Events without a related to Account - this should go to the User's feed.
Here's my code:
public class eventChatterFuture{
@future
public static void FutureEventChatterMethod(set <id> empId){
Set < id> empIds = empId;
Set<ID> ownerIds = new Set<ID>();
List<FeedItem> feedItems = new List<FeedItem>();
list <event> theEs = [select WhatId, WhoId, Subject, ActivityDate, ownerID, Via_Publisher__c, Description from event where ID IN: empids];
system.debug ('theEs===================='+ theEs);
for (event E : theEs){
ownerIds.add(e.ownerId);
}
system.debug('ownerIDs-------------' + ownerids);
Map<ID,User> userMap = new Map<ID,User>([SELECT ID, Name FROM User WHERE ID IN :ownerIds]); //This is our user map
System.debug('usermap------------------------' + usermap);
for (event t : theEs) {
if (t.WhatId != null && t.via_publisher__c == false) {
FeedItem fitem = new FeedItem();
fitem.type = 'LinkPost';
fitem.ParentId = t.WhatId;
fitem.LinkUrl = '/' + t.id; //This is the url to take the user to the activity
fitem.Title = 'View'; //This is the title that displays for the LinkUrl
//Get the user by checking the userMap we created earlier
User assignedTo = userMap.get(t.ownerId);
system.debug('assignedTo-------------------' + assignedTo );
fitem.Body = ' Meeting' + ((t.ActivityDate != null) ? t.ActivityDate.format() :'')
// + '\nWho: ' + t.Who
+ '\nOwner: ' + assignedTo.name
+ '\nSubject: ' + t.Subject
+ '\nNotes: ' + t.Description
;
feedItems.add(fitem);
}
else if (t.WhatId == null && t.via_publisher__c == false) {
FeedItem fitem = new FeedItem();
fitem.type = 'LinkPost';
fitem.ParentId = t.WhoId;
fitem.LinkUrl = '/' + t.id; //This is the url to take the user to the activity
fitem.Title = 'View'; //This is the title that displays for the LinkUrl
//Get the user by checking the userMap we created earlier
User assignedTo = userMap.get(t.ownerId);
system.debug('assignedTo-------------------' + assignedTo );
fitem.Body = ((t.ActivityDate != null) ? t.ActivityDate.format() :'')
+ '\nNotes: ' + t.Description
;
feedItems.add(fitem);
}
}
if (feedItems.size() > 0) {
Database.insert(feedItems,false); //notice the false value. This will allow some to fail if Chatter isn't available on that object
}
} }
It is supposed to first look if there's now a WhatId and post to the Account, if no WhatId, then post to User. But it's not posting anywhere
thanks