0

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

2
  • Where does via_publisher__c get set? Is it possible this is being set to true before the asynchronous process is completed? Commented Jul 14, 2015 at 19:43
  • that field is only set when the post is done via a chatter feed (so we don't double publish), and the events i am testing save with that as false when I check
    – jaw999
    Commented Jul 14, 2015 at 19:47

1 Answer 1

2

Your insert is failing on insert for an event where a whoid and whatid are both not present.

In your else if statement you are setting fitem.ParentId = t.WhoId which most likely does not exist. You need to change that to the assignedTo user.

You were not getting any errors because of setting Database.insert(feedItems,false). If you turn it to true you will receive the required field error needed to troubleshoot this issue.

0

You must log in to answer this question.

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