function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brooks Johnson 6Brooks Johnson 6 

Method not populating field

Hi Friends,  I have a method getPardotTaskCount() that is called from a trigger handler.  It is designed to take the count of all tasks associated with a contact ID  that meet certain criteria and update a number field.  For the life of me, I can't get it to work right. I have put System.debug statements everywhere. When I test from the UI all the values are correct but the field does not get updated. In my test class, the assertions also fail.  I would appreciate a second set of eyes. I am sure  I am missing something simple.
The method
public static void getPardotTaskCount(Set<Id> contactIds){
        System.debug('GetpardotTaskCount method called, contactIds.size() = ' + contactIds.size());

        //loop through the Contact Ids and get all tasks where Who Id = Contact ID
        //Then update the Contact Total emails field
        List<Contact> contacts = [SELECT Id, Total_Emails_Sent__c FROM Contact
                                    WHERE Id =: contactIds];
        Map<Id, AggregateResult> aggregateResultMap = new Map<Id, AggregateResult>([SELECT WhoId Id, COUNT(Subject) subjectCount
        From Task
        WHERE WhoId =: contactIds
        AND Subject LIKE '%Pardot%'
        AND WhoId != null
        GROUP BY WhoId
        ALL ROWS
        ]);
        System.debug('subjectCount.values() = ' +  aggregateResultMap.Values());
        for(Contact c : contacts){
            if (aggregateResultMap.containsKey(c.Id)) {
                System.debug('Contained  Contact Key branch of If Statement');
                c.Total_Emails_Sent__c = Integer.valueOf(aggregateResultMap.get(c.Id).get('subjectCount'));
            }else {
                c.Total_Emails_Sent__c = 0;
            }
        }
    }
The Test 
@IsTest
    public static void testPardotEmailCount(){
        //Given
        Account acc = new Account(Name = 'Test Acc');
        Insert acc;
        Integer numTasks = 1;
        List<Task> tasks = new List<Task>();
        Contact contact = new Contact(LastName = 'Test Influencer', AccountId = acc.Id);
        insert contact;
        for (Integer i = 0; i < numTasks; i++){
            Task t = new Task(Subject = 'Pardot List Email ' + String.valueOf(i), WhoId = contact.Id);
            tasks.add(t);
        }
        insert tasks;
        Contact updatedContact = [SELECT Id, Total_Emails_Sent__c FROM Contact LIMIT 1];
        //then
        System.assertEquals(1, updatedContact.Total_Emails_Sent__c);

    }




 
Best Answer chosen by Brooks Johnson 6
Maharajan CMaharajan C
Hi Brooks,

Please update the Contact using DML in your class:


public static void getPardotTaskCount(Set<Id> contactIds){
        System.debug('GetpardotTaskCount method called, contactIds.size() = ' + contactIds.size());

        //loop through the Contact Ids and get all tasks where Who Id = Contact ID
        //Then update the Contact Total emails field
        List<Contact> contacts = [SELECT Id, Total_Emails_Sent__c FROM Contact
                                    WHERE Id =: contactIds];
        Map<Id, AggregateResult> aggregateResultMap = new Map<Id, AggregateResult>([SELECT WhoId Id, COUNT(Subject) subjectCount
        From Task
        WHERE WhoId =: contactIds
        AND Subject LIKE '%Pardot%'
        AND WhoId != null
        GROUP BY WhoId
        ALL ROWS
        ]);
        System.debug('subjectCount.values() = ' +  aggregateResultMap.Values());
        for(Contact c : contacts){
            system.debug('@@@ For');
            if (aggregateResultMap.containsKey(c.Id)) {
                System.debug('Contained  Contact Key branch of If Statement');
                c.Total_Emails_Sent__c = Integer.valueOf(aggregateResultMap.get(c.Id).get('subjectCount'));
            }else {
                system.debug('@@@ else');
                c.Total_Emails_Sent__c = 0;
            }
        }
        
        update contacts;   /// Add this Line to update the contact
    }
    
Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Brooks,

Please update the Contact using DML in your class:


public static void getPardotTaskCount(Set<Id> contactIds){
        System.debug('GetpardotTaskCount method called, contactIds.size() = ' + contactIds.size());

        //loop through the Contact Ids and get all tasks where Who Id = Contact ID
        //Then update the Contact Total emails field
        List<Contact> contacts = [SELECT Id, Total_Emails_Sent__c FROM Contact
                                    WHERE Id =: contactIds];
        Map<Id, AggregateResult> aggregateResultMap = new Map<Id, AggregateResult>([SELECT WhoId Id, COUNT(Subject) subjectCount
        From Task
        WHERE WhoId =: contactIds
        AND Subject LIKE '%Pardot%'
        AND WhoId != null
        GROUP BY WhoId
        ALL ROWS
        ]);
        System.debug('subjectCount.values() = ' +  aggregateResultMap.Values());
        for(Contact c : contacts){
            system.debug('@@@ For');
            if (aggregateResultMap.containsKey(c.Id)) {
                System.debug('Contained  Contact Key branch of If Statement');
                c.Total_Emails_Sent__c = Integer.valueOf(aggregateResultMap.get(c.Id).get('subjectCount'));
            }else {
                system.debug('@@@ else');
                c.Total_Emails_Sent__c = 0;
            }
        }
        
        update contacts;   /// Add this Line to update the contact
    }
    
Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
This was selected as the best answer
Brooks Johnson 6Brooks Johnson 6
Hi Maharajan,  that worked and this seems like a learning moment for me. The method is called from a before trigger. I thought that DML was not required in that case?