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
akallioWileyakallioWiley 

SOQL Query works in after update, but not after delete

The class posted below is fed a set of Contact Ids that comes from a trigger on the Task object. We use particular Task record type to track what we call 'Market Development'. A Market Development task contributes towards a total Market Development score for the contact. Therefore we have this class which will add or subtract MD points for a Contact as MD tasks are added or deleted. 

 

My issue is that Total Points are not being subtracted when an MD task is deleted. As I said the class receives a set of contact ids and processes MD points from there by using SOQL to select the Contacts and their MD Tasks. What I have found is that, in after delete trigger context only, the SOQL query that is supposed to get the Contact fails to find the contact. And this where I get stuck. I have put comments in the code below to hopefully help you understand how I am trying to debug this.

 

Any thoughts on why this is happening or a different debugging technique would be great.

public class MarketDevelopmentScorecard {

    public static void updateMarketDevelopmentScorecard(Set<Id> contactIds) {
    	RecordType mdRecordType = RecordTypeUtil.getRecordType('Task', 'Market Development');
    	if (mdRecordType != null) {
            Date recentActivityDate = System.today().addDays(-1095); // 3 years ago
	        
	        List<Task> mdTasks =
	            [select Id,
	                    ActivityDate,
	                    MD_Activity_Type__c,
	                    WhoId
	            from    Task
	            where   WhoId in :contactIds
	                and RecordTypeId = :mdRecordType.Id
	                and ActivityDate >= :recentActivityDate];
	                
            Map<Id, Set<String>> contactMarketDevelopmentActivities = new Map<Id, Set<String>>();
	        for (Task t : mdTasks) {
	        	if (t.MD_Activity_Type__c != null && t.MD_Activity_Type__c != '') {
	                Set<String> mdActivities = contactMarketDevelopmentActivities.get(t.WhoId);
	                if (mdActivities == null) {
	                    mdActivities = new Set<String>();
	                    contactMarketDevelopmentActivities.put(t.WhoId, mdActivities);
	                }
	                mdActivities.add(t.MD_Activity_Type__c);
	        	}
	        }

            List<Market_Development_Rating__c> marketDevelopmentRatings = Market_Development_Rating__c.getAll().values();
            
            for (Id cId : contactIds) {
            	System.debug('JJB contactId: ' + cId);
            }
            
            List<Contact> contacts = 
                [select Id,
                        MD_Total_Points__c,
                        MD_Activities__c
                from    Contact
                where   Id in :contactIds];
            //this system assert shows that I am receiving a contact from the trigger, but that the SOQL query doesn't find it
            //system.assert(false, 'Contacts from Trigger '+contactIds.size()+'Contacts found in database '+contacts.size());
            for (Contact c : contacts) {
            	Decimal totalPoints = 0.0;
            	String activities = '';
                //in after delete context this system assert never gets hit because this for loop never starts because there is nothing in the List contacts
            	//system.assert(false, 'hello!! ');            	
            	Set<String> mdActivities = contactMarketDevelopmentActivities.get(c.Id);
            	if (mdActivities == null) {
            		mdActivities = new Set<String>();
            	}
            	
            	for (Market_Development_Rating__c mdr : marketDevelopmentRatings) {
            		if (mdActivities.contains(mdr.Name)) {
            			totalPoints += mdr.MD_Points__c;
            			if (mdr.Contact_Field_Name__c != null && mdr.Contact_Field_Name__c != '') {
            				c.put(mdr.Contact_Field_Name__c, mdr.MD_Points__c);
            				//system.assert(false, 'the total points are: '+mdr.MD_Points__c);
            			}
            		} else {
                        if (mdr.Contact_Field_Name__c != null && mdr.Contact_Field_Name__c != '') {
                            c.put(mdr.Contact_Field_Name__c, 0.00);
                        }
            		}
            	}

 

 

 

carlocarlo

Need to see the trigger code.

akallioWileyakallioWiley

Here is the trigger:

 

trigger TaskMarketDevelopmentScorecardUpdate on Task (after delete, after insert, after undelete, after update) {

    Set<Id> contactIds = new Set<Id>();

    RecordType mdRecordType = RecordTypeUtil.getRecordType('Task', 'Market Development');
    if (mdRecordType != null) {
        if (Trigger.isDelete) {
            for (Task t : Trigger.old) {
                if (t.RecordTypeId == mdRecordType.Id) {
                    contactIds.add(t.WhoId);
                    
                }
            }
        } else {
            for (Task t : Trigger.new) {
                if (t.RecordTypeId == mdRecordType.Id) {
                    contactIds.add(t.WhoId);
                }
            }
        }
    }
    
    if (contactIds.size() > 0) {
        MarketDevelopmentScorecard.updateMarketDevelopmentScorecard(contactIds);
    }
}

 

carlocarlo

Have you tested the trigger?

akallioWileyakallioWiley

yes, it was a system.assert in the test class that alerted me to the issue.

carlocarlo

Can I see that too?

akallioWileyakallioWiley

the test method is at the bottom. I have added a comment to the assert that fails.

 

public class MarketDevelopmentScorecard {

    public static void updateMarketDevelopmentScorecard(Set<Id> contactIds) {
    	RecordType mdRecordType = RecordTypeUtil.getRecordType('Task', 'Market Development');
    	if (mdRecordType != null) {
            Date recentActivityDate = System.today().addDays(-1095); // 3 years ago
	        
	        List<Task> mdTasks =
	            [select Id,
	                    ActivityDate,
	                    MD_Activity_Type__c,
	                    WhoId
	            from    Task
	            where   WhoId in :contactIds
	                and RecordTypeId = :mdRecordType.Id
	                and ActivityDate >= :recentActivityDate];
	                
            Map<Id, Set<String>> contactMarketDevelopmentActivities = new Map<Id, Set<String>>();
	        for (Task t : mdTasks) {
	        	if (t.MD_Activity_Type__c != null && t.MD_Activity_Type__c != '') {
	                Set<String> mdActivities = contactMarketDevelopmentActivities.get(t.WhoId);
	                if (mdActivities == null) {
	                    mdActivities = new Set<String>();
	                    contactMarketDevelopmentActivities.put(t.WhoId, mdActivities);
	                }
	                mdActivities.add(t.MD_Activity_Type__c);
	        	}
	        }//system.assert(false, 'hello!! ');

            List<Market_Development_Rating__c> marketDevelopmentRatings = Market_Development_Rating__c.getAll().values();
            
            for (Id cId : contactIds) {
            	System.debug('JJB contactId: ' + cId);
            }
            
            List<Contact> contacts = 
                [select Id,
                        MD_Total_Points__c,
                        MD_Activities__c
                from    Contact
                where   Id in :contactIds];
            system.assert(false, 'Contacts from Trigger '+contactIds.size()+'Contacts found in database '+contacts.size());
            for (Contact c : contacts) {
            	Decimal totalPoints = 0.0;
            	String activities = '';
            	//system.assert(false, 'hello!! ');            	
            	Set<String> mdActivities = contactMarketDevelopmentActivities.get(c.Id);
            	if (mdActivities == null) {
            		mdActivities = new Set<String>();
            	}
            	
            	for (Market_Development_Rating__c mdr : marketDevelopmentRatings) {
            		if (mdActivities.contains(mdr.Name)) {
            			totalPoints += mdr.MD_Points__c;
            			if (mdr.Contact_Field_Name__c != null && mdr.Contact_Field_Name__c != '') {
            				c.put(mdr.Contact_Field_Name__c, mdr.MD_Points__c);
            				//system.assert(false, 'the total points are: '+mdr.MD_Points__c);
            			}
            		} else {
                        if (mdr.Contact_Field_Name__c != null && mdr.Contact_Field_Name__c != '') {
                            c.put(mdr.Contact_Field_Name__c, 0.00);
                        }
            		}
            	}
            	List<String> mdActivityList = new List<String>();
            	mdActivityList.addAll(mdActivities);
            	mdActivityList.sort();
            	if (mdActivityList.size() > 1) {
            		activities = mdActivityList.get(0);
            		for (Integer i=1; i < mdActivityList.size(); ++i) {
            			activities += ', ' + mdActivityList.get(i);
            		}
            	} else if (mdActivityList.size() == 1) {
                    activities = mdActivityList.get(0);
            	}
            	
            	c.MD_Total_Points__c = totalPoints;
            	c.MD_Activities__c = activities;
            }
            update contacts;
		}
    }

    public static testMethod void testMarketDevelopmentScorecard() {
        List<SObject> testObjects = TestData.generateTestFacultyContact();
        
        // Add Test Method Market Development Rating
        Market_Development_Rating__c mdr1 = new Market_Development_Rating__c(Name = 'B Test Method MDR', MD_Points__c = 10);
        Market_Development_Rating__c mdr2 = new Market_Development_Rating__c(Name = 'A Test Method MDR', MD_Points__c = 20);
        insert new Market_Development_Rating__c[] {mdr1, mdr2};
        
        Contact testContact1 = (Contact) testObjects.get(2);
        Contact testContact2 = (Contact) testObjects.get(3);

        // Add Market Development Task            
        RecordType mdRecordType = RecordTypeUtil.getRecordType('Task', 'Market Development');
        Task t1 = new Task(ActivityDate = System.today(), WhoId = testContact1.Id, RecordTypeId = mdRecordType.Id, MD_Activity_Type__c = mdr1.Name);
        Task t2 = new Task(ActivityDate = System.today(), WhoId = testContact1.Id, RecordTypeId = mdRecordType.Id, MD_Activity_Type__c = mdr2.Name);
        insert new Task[] {t1, t2};
        
        Set<Id> contactIds = new Set<Id>{testContact1.Id, testContact2.Id};
        MarketDevelopmentScorecard.updateMarketDevelopmentScorecard(contactIds);
        
        Contact assertContact1 = [select Id, MD_Total_Points__c, MD_Activities__c from Contact where Id = :testContact1.Id limit 1];
        System.assertEquals(30.0, assertContact1.MD_Total_Points__c);
        System.assertEquals('A Test Method MDR, B Test Method MDR', assertContact1.MD_Activities__c);   
        
        Contact assertContact2 = [select Id, MD_Total_Points__c, MD_Activities__c from Contact where Id = :testContact2.Id limit 1];
        System.assertEquals(0.0, assertContact2.MD_Total_Points__c);   
        
        delete t2;
        
        Contact assertContact3 = [select Id, MD_Total_Points__c, MD_Activities__c from Contact where Id = :testContact1.Id limit 1];
        System.assertEquals(10.0, assertContact3.MD_Total_Points__c);//THIS IS THE SYSTEM ASSERT THAT FAILS
        System.assertEquals('B Test Method MDR', assertContact3.MD_Activities__c); 

    }    
}

 

carlocarlo

So how could you break down the trigger to see what's happening?

akallioWileyakallioWiley

The code in my original post has comments that explain where it breaks down.

carlocarlo

From what I understand you think there is a problem with your SOQL query not finding the contact/s passed from the trigger.  This is what you are asking for help with.  I am trying to help you figure it out.

 

I think the poblem is in the trigger.  Therefore the comments in your original post do not explain the problem.

 

Did you check the system debug logs from the below?

 

System.debug('JJB contactId: ' + cId);