0

I have been trying to get a trigger to fire when a new attachment is inserted (After insert), the ParentId is Event for that attachment, and the Event is related to an Account.

So, if anyone inserts an attachment on an Event and that event is related to an Account the following needs to happen:

  1. A Chatter FeedItem needs to be inserted for the Event Owner followers.
  2. A Chatter FeedItem needs to be inserted for the related Account followers for that event.
  3. List item

Note: On the code expose here I'm trying only the account followers.

I know this can be very tricky and I'm trying to at least get it fired, not so such luck so far. The Trigger is on an After Trigger context variable.

Here is the code:

trigger AttachmentTrigger on Attachment (after insert) {


List<FeedItem> feedList = new List<FeedItem>();

    Set<Id> accIds = new Set<Id>();
    Set<Id> ownIds = new Set<Id>();
    Set<Id> eveIds = new Set<Id>();

    for(Attachment a: trigger.new)
    {
      if(a.ParentId.getSObjectType() == Event.SObjectType)
    {
      eveIds.add(a.ParentId); 
    }
  }
      System.debug('Event size ' +eveIds.size());


    if(!eveIds.isEmpty()){
    Map<Id, Event> evenList = new Map<Id, Event>([SELECT OwnerId, Account.Id FROM Event WHERE Id IN :eveIds AND Account.Id != null]); 
    for(Event e: evenList.values()){


    accIds.add(e.Account.Id);
    ownIds.add(e.OwnerId); 
   }
  }

System.debug('Account from event size ' +accIds.size());
System.debug('Owner from event size ' +ownIds.size()); 

List<EntitySubscription> entityListAcct = [SELECT id, ParentId, SubscriberId FROM EntitySubscription WHERE ParentId IN :accIds];
List<EntitySubscription> entityListOwn = [SELECT id, ParentId, SubscriberId FROM EntitySubscription WHERE ParentId IN :ownIds];

System.debug('This is the entityListAcct size: ' + entityListAcct.size());
System.debug('This is the entityListOwn size: ' +entityListOwn.size());

   for(EntitySubscription entityAcct: entityListAcct)
{
   if(!entityListAcct.isEmpty() && entityListAcct.size()>0)
{
    FeedItem accountFeedItem= new FeedItem(
    ParentId = entityAcct.SubscriberId, 
    Body ='This is a message from hell!!'); 

    feedList.add(accountFeedItem); 
    }

  }

   System.debug('This is the feedList size: '+feedList.size()); 

   if(!feedList.isEmpty() && feedList.size()>0){
   insert feedList; 
  }
}

Any tips on how to get this trigger to fire or why is not firing?? Any help would be much appreciated. Thanks.

3
  • I think your trigger is firing on the wrong object. What is the Attachment being attached to? Is it being attached to a FeedItem or to an Event?
    – crmprogdev
    Jun 2, 2015 at 17:47
  • How do you know it isn't firing? Or is it firing and you're just not getting any action? Do any of your debugs make it into the log?
    – pchittum
    Jun 2, 2015 at 20:43
  • Hello. The attachment is been attached to an Event. The FeedItems need to be added to the Account and Event owner followers. On my Developer Console there is not logs after executing the process. So if I add an attachment to and event with all he correct input, needs to have an account that I'm following, after saving the event I see that the trigger is not firing. There are no log in the developer console. Jun 3, 2015 at 7:20

1 Answer 1

0

I have sold this by using the Event object rather than the Attachment one. The code for DML insert looks like this:

trigger EventWithAttachment on Event (after insert) 
{

List<Event> eventList = new List<Event>();
List<FeedItem> feedList = new List<FeedItem>();
Set<Id> ownerIds = new Set<Id>();
Set<Id> accountIds = new Set<Id>();
Set<Id> eventAttachmentsIds = new Set<Id>();
Id eventID;

for(Event e : trigger.new)
{

    if(e.WhatId != null && e.WhatId.getSObjectType() == Account.SObjectType)
      {
        eventList.add(e); 
      }
}

Map<Id,Event> eventAttMap = new Map<Id, Event>([SELECT id, OwnerId, Account.Id, has_Attachment__c, (SELECT Id FROM Attachments) FROM Event WHERE Id IN : eventList]); 

for(Event currentEvent: eventAttMap.values())
{
    for(Attachment a : currentEvent.Attachments)
    {
       if(currentEvent.Attachments != null && currentEvent.has_Attachment__c== false)
         {
            currentEvent.has_Attachment__c = true; 
            ownerIds.add(currentEvent.OwnerId);
            accountIds.add(currentEvent.Account.Id);
            eventId = currentEvent.id; 
            update currentEvent;
         }  
    }   
}            

List<EntitySubscription> entityListAccount = [SELECT Id, ParentId, SubscriberId FROM EntitySubscription WHERE ParentId IN: accountIds]; 

for(EntitySubscription entityAccount : entityListAccount)
{

if(!entityListAccount.isEmpty() && entityListAccount.size() >0){

        FeedItem accountFeedItem = new FeedItem(
        ParentId = entityAccount.SubscriberId,
        Body = 'This is the msg',
        LinkUrl = 'https://eu5.salesforce.com/'+ eventId);

        feedList.add(accountFeedItem);

    }

}

if(!feedList.isEmpty() && feedList.size() >0){
   try 
   {
    insert feedList; 
   } 
   catch (DMLException ex)
   {
  }
 }

This works fine for inserting and it does what I want. I just need to make some modifications to cover the scenario in which a user decided to remove the attachment. In that case I need to change the "has_Attachment__c" to false. But this is the basic structure.

You must log in to answer this question.

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