You need to sign in to do that
Don't have an account?
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); } } }
Need to see the trigger code.
Here is the trigger:
Have you tested the trigger?
yes, it was a system.assert in the test class that alerted me to the issue.
Can I see that too?
the test method is at the bottom. I have added a comment to the assert that fails.
So how could you break down the trigger to see what's happening?
The code in my original post has comments that explain where it breaks down.
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?