• Chris Toews 9
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
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();
    }
    
    
}


 

Hi there,

 

I am getting the following ERROR:

Exception: System.JSONException: Illegal value for primitive

When I try to deserialize the HTTP response from Jive to an object.

 

JivePersonObject personRecord = (JivePersonObject) System.JSON.deserialize(jiveResponse, JivePersonObject.class);

 

I have validated the JSON string at http://jsonlint.com/ and it is valid. 

 

This is not happening for all the response. It is happening for some response and some of them don't have the issue.

 

I was wondering if anyone have come across this issue before. Not sure what I am doing wrong here. Thanks.

 

Sanch