+ Start a Discussion
Chris Toews 9Chris Toews 9 

Having trouble updating custom field on Event object

I am new to writing Apex Triggers. I eventually want to count notes and attachments and update Event object. I am able to get the list of Event objects from the trigger attachment objects. I am able to get the count of attachments for each event. I can iterate through the Event items, and then the update doesn't seem to work.

I have a feeling this is something stupid.
I get this back when I run the test:
FATAL_ERROR System.AssertException: Assertion Failed: Expected: 1, Actual: null

Here is my trigger:
trigger Update_events_for_attachments on Attachment (after insert, after update) {

    List<Id> parentIDs = new List<Id>();
    
    for(Attachment att:Trigger.New){
		if(att.Parentid.getSObjectType().getDescribe().getName() == 'Event'){
            parentIDs.add(att.Parentid);
            System.debug('parent ID:' + att.Parentid);
        }
    }
    
    List<Event> evs = new List<Event>([select id, Note_Count__c from Event where id in :parentIDs]);
    System.debug('evs size:' + evs.size() + '    parentidsize:' + parentIDs.size());
    
    if(parentIDs.isEmpty()){
        System.debug('we didn\'t find any events');
        //if empty, we have no events to update
        return;
    }
    
    Map<ID, Integer> mymap = new Map<ID, Integer>();
    AggregateResult[] ARs = [select count(id) mycount, parentid  from attachment where parentid in :parentIDs group by parentID];
        
    for (AggregateResult ar : ARs){
        Integer thisCount = (Integer) ar.get('mycount');
        ID thidID = (ID) ar.get('parentid');
        mymap.put(thidID, thisCount);
    }
    
    
    for (Event thisEvent : evs){
        System.debug('looping thorugh evs');
        ThisEvent.Note_Count__c = mymap.get(thisEvent.ID);
        System.debug('ThisEvent.id:' + thisEvent.Id + '    ThisEvent.Note_Count__c: '+ ThisEvent.Note_Count__c);
    }
    
    update evs;
    
}

and here is the test class I'm trying to use:
@isTest
private class test_Update_events_for_attachments {

    @isTest static void TestAddingSingleAttachment(){
        Event newEvent = new Event();
        newEvent.Subject ='Test';
        newEvent.DurationInMinutes =1440;
        newEvent.ActivityDate = System.today();
        newEvent.ActivityDateTime = System.today();
        insert newEvent;
        
        Test.startTest();
        Attachment attach=new Attachment();   	
    	attach.Name='Unit Test Attachment';
    	Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
    	attach.body=bodyBlob;
        attach.parentId=newEvent.id;
        insert attach;
 
        System.debug('newEvent.Id:' + newEvent.Id + '    attach.parentID: ' + attach.parentID + '       newEvent.Note_Count__c: ' + newEvent.Note_Count__c);
        System.assertEquals(newEvent.Id, attach.parentID);
        System.assertEquals(1, newEvent.Note_Count__c);
        Test.stopTest();
    }
    
    
}


 
Best Answer chosen by Chris Toews 9
logontokartiklogontokartik
In your test class you need to query the Event object, you cannot directly do an assert for a field that you dont have in your Event instance. It returns null. After line 18, add the following
newEvent = [Select Id, Subject, ActivityDate, ActivityDateTime, DurationInMinutes, Note_Count__c from Event where Id = :newEvent.Id];
Now you can asset if the count is getting updated or not.

 

All Answers

logontokartiklogontokartik
In your test class you need to query the Event object, you cannot directly do an assert for a field that you dont have in your Event instance. It returns null. After line 18, add the following
newEvent = [Select Id, Subject, ActivityDate, ActivityDateTime, DurationInMinutes, Note_Count__c from Event where Id = :newEvent.Id];
Now you can asset if the count is getting updated or not.

 
This was selected as the best answer
Chris Toews 9Chris Toews 9
Thanks,
That fixed it so my test class worked. But the trigger didn't update the Event object when I added attachments in Salesforce web interface. Where a user has to selectd "edit" on the Event page to add an attachment, I found if I made a similar trigger on Event object and looked after update I was able to successfully count the attachments. The only thing I had to overcome with a problem with recursion.
I used this solution to get around my recursion issues:
https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US

Thanks,
Chris Toews