• CNM
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
Hello all, 

I have been trying to test an Event trigger but I'm kind of lost in a particular part of the execution that I want and need to test.
On the Event object I have a Boolean custom field, has_Attachment__c. So if you insert an attachment in an event this Boolean field will be set to true. How do I test this?  
I use a centralize object initialization class: 
 
public with sharing class EventCentralizeObjectInitialization {
	
	
	 public static List<Event> InitTestInsertEvents(Integer count)
     {
     	
     	Account account = new Account(Name ='testEventAfterInsertAccount'); 
    	insert account; 
    	
    	Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
        User u = new User(Alias = 'systemAd',
                          Email='systemAd@testorg.com', 
       					  EmailEncodingKey='UTF-8', 
       					  LastName='Testing', 
       					  LanguageLocaleKey='en_US', 
            			          LocaleSidKey='en_US', 
            			          ProfileId = p.Id, 
            			          TimeZoneSidKey='America/Los_Angeles', 
            			          UserName='systemAdEvent@testorg.com');
    	
    	insert u; 
    	
    	DateTime dt = System.now(); 
    	
    		List<Attachment> eventAttMap = new List<Attachment>();
     		Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
     		
     		List<Event> eventList = new List<Event>(); 
     		    		    		        	        	
        	for(Integer x=0; x< count; x++)
        	{
        		eventList.add(new Event(OwnerId = u.Id,
        		                  Subject ='The test subject',
        				  StartDateTime= dt.addMinutes(-120),
        				  EndDateTime = dt,
        				  WhatId = account.id,
        				  Activity_Type__c = 'Inspection')); 
        	}
        	
        	        		
        	return eventList;	
    
       }
         
 }

Then I pull the method InitTestInsertEvents to my test class like this: 
 
@isTest
private class TestEventAfterInsert {

    public static testMethod void testPrepareEventAfterInsert()
    {
    		 
    	List<Event> eventList = EventCentralizeObjectInitialization.InitTestInsertEvents(2); 
    	List<Attachment> attList = new List<Attachment>();
    	List<FeedItem> feedList = new List<FeedItem>();
    	List<Event> eventToUpdate = new List<Event>();
    	Set<Id> ownerIds = new Set<Id>();
    	Set<Id> accountIds = new Set<Id>();
    	Set<Id> eventAttIds = new Set<Id>(); 
    	Id eventID;
    	
    	
    	Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
    	
    	
    	Test.StartTest();  
    	insert eventList;
    	Test.StopTest(); 
    	
    	Map<Id, Event> eventmap = new Map<Id, Event>(eventList);
    	List<Event> events = [SELECT Id, OwnerId, Account.Id, has_Attachment__c, (SELECT Id FROM Attachments) FROM Event WHERE Id IN :eventmap.keySet()]; 	
    	
    	System.assertEquals(events.size(), 2); 
     
     	for(Event e: events)
     	{
     		if(e.Id!=null){
     			attList.add(new Attachment(
     			Name='The test attachment',
     			Body=bodyBlob,
     			ParentId= e.id,
     			OwnerId = e.OwnerId));
     			    			
     		}
     		
     	System.assertNotEquals(attList.size(), 0); 
     	
     	   	
     	}
     	
     	if(!attList.isEmpty() && attList.size()>0)
     	{
     		insert attList; 
       	}
     
     	for(Attachment a: attList)
     	{
     		if(!attList.isEmpty())
     		{
     			Id eventIdAtt = a.ParentId;
     			eventAttIds.add(eventIdAtt);				
     			}
     		}
     		
     	List<Event> eventListAttIds = [SELECT Id, OwnerId, Account.Id, has_Attachment__c FROM Event WHERE Id IN: eventAttIds];	
     	
     		for(Event e: eventListAttIds)
     		{
     			if(!eventListAttIds.isEmpty() && eventListAttIds.size()>0)
     			{
     				e.has_Attachment__c = true;
     				ownerIds.add(e.OwnerId);
     				accountIds.add(e.Account.Id);
     				eventId = e.Id;
     				eventToUpdate.add(e);
     				
     				System.assertEquals(e.has_Attachment__c, true); 
     			}
     		}update eventToUpdate;
     		
     
  }	
}

If I run the test class I only get 47% of the test coverage.
I have been trying different things without getting that percentage higher than 47 :( 

So the question is, how can I pass the events to the test class with an attachment if I can't insert the attachments becasue the ParendIt is not present? Anyone knows how to tests this particular scenario?
Thanks. 
 
  • June 26, 2015
  • Like
  • 0
 have been trying to get a trigger to fire when a new attachment is inserted (After insert), the ParentIdis 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:

A Chatter FeedItem needs to be inserted for the Event Owner followers.
A Chatter FeedItem needs to be inserted for the related Account followers for that event.
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.
  • June 02, 2015
  • Like
  • 0
Hello all, 

I have been trying to test an Event trigger but I'm kind of lost in a particular part of the execution that I want and need to test.
On the Event object I have a Boolean custom field, has_Attachment__c. So if you insert an attachment in an event this Boolean field will be set to true. How do I test this?  
I use a centralize object initialization class: 
 
public with sharing class EventCentralizeObjectInitialization {
	
	
	 public static List<Event> InitTestInsertEvents(Integer count)
     {
     	
     	Account account = new Account(Name ='testEventAfterInsertAccount'); 
    	insert account; 
    	
    	Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
        User u = new User(Alias = 'systemAd',
                          Email='systemAd@testorg.com', 
       					  EmailEncodingKey='UTF-8', 
       					  LastName='Testing', 
       					  LanguageLocaleKey='en_US', 
            			          LocaleSidKey='en_US', 
            			          ProfileId = p.Id, 
            			          TimeZoneSidKey='America/Los_Angeles', 
            			          UserName='systemAdEvent@testorg.com');
    	
    	insert u; 
    	
    	DateTime dt = System.now(); 
    	
    		List<Attachment> eventAttMap = new List<Attachment>();
     		Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
     		
     		List<Event> eventList = new List<Event>(); 
     		    		    		        	        	
        	for(Integer x=0; x< count; x++)
        	{
        		eventList.add(new Event(OwnerId = u.Id,
        		                  Subject ='The test subject',
        				  StartDateTime= dt.addMinutes(-120),
        				  EndDateTime = dt,
        				  WhatId = account.id,
        				  Activity_Type__c = 'Inspection')); 
        	}
        	
        	        		
        	return eventList;	
    
       }
         
 }

Then I pull the method InitTestInsertEvents to my test class like this: 
 
@isTest
private class TestEventAfterInsert {

    public static testMethod void testPrepareEventAfterInsert()
    {
    		 
    	List<Event> eventList = EventCentralizeObjectInitialization.InitTestInsertEvents(2); 
    	List<Attachment> attList = new List<Attachment>();
    	List<FeedItem> feedList = new List<FeedItem>();
    	List<Event> eventToUpdate = new List<Event>();
    	Set<Id> ownerIds = new Set<Id>();
    	Set<Id> accountIds = new Set<Id>();
    	Set<Id> eventAttIds = new Set<Id>(); 
    	Id eventID;
    	
    	
    	Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
    	
    	
    	Test.StartTest();  
    	insert eventList;
    	Test.StopTest(); 
    	
    	Map<Id, Event> eventmap = new Map<Id, Event>(eventList);
    	List<Event> events = [SELECT Id, OwnerId, Account.Id, has_Attachment__c, (SELECT Id FROM Attachments) FROM Event WHERE Id IN :eventmap.keySet()]; 	
    	
    	System.assertEquals(events.size(), 2); 
     
     	for(Event e: events)
     	{
     		if(e.Id!=null){
     			attList.add(new Attachment(
     			Name='The test attachment',
     			Body=bodyBlob,
     			ParentId= e.id,
     			OwnerId = e.OwnerId));
     			    			
     		}
     		
     	System.assertNotEquals(attList.size(), 0); 
     	
     	   	
     	}
     	
     	if(!attList.isEmpty() && attList.size()>0)
     	{
     		insert attList; 
       	}
     
     	for(Attachment a: attList)
     	{
     		if(!attList.isEmpty())
     		{
     			Id eventIdAtt = a.ParentId;
     			eventAttIds.add(eventIdAtt);				
     			}
     		}
     		
     	List<Event> eventListAttIds = [SELECT Id, OwnerId, Account.Id, has_Attachment__c FROM Event WHERE Id IN: eventAttIds];	
     	
     		for(Event e: eventListAttIds)
     		{
     			if(!eventListAttIds.isEmpty() && eventListAttIds.size()>0)
     			{
     				e.has_Attachment__c = true;
     				ownerIds.add(e.OwnerId);
     				accountIds.add(e.Account.Id);
     				eventId = e.Id;
     				eventToUpdate.add(e);
     				
     				System.assertEquals(e.has_Attachment__c, true); 
     			}
     		}update eventToUpdate;
     		
     
  }	
}

If I run the test class I only get 47% of the test coverage.
I have been trying different things without getting that percentage higher than 47 :( 

So the question is, how can I pass the events to the test class with an attachment if I can't insert the attachments becasue the ParendIt is not present? Anyone knows how to tests this particular scenario?
Thanks. 
 
  • June 26, 2015
  • Like
  • 0
 have been trying to get a trigger to fire when a new attachment is inserted (After insert), the ParentIdis 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:

A Chatter FeedItem needs to be inserted for the Event Owner followers.
A Chatter FeedItem needs to be inserted for the related Account followers for that event.
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.
  • June 02, 2015
  • Like
  • 0