You need to sign in to do that
Don't have an account?

Need Help SOQL Query not returning a value in Trigger?
This is an excerpt of code running in an After Update query:
Set<Id> setOpptsToDelete = new Set<Id>();
// Loop through the Opportunities being updated or deleted and see if there are any for the record types we want.
if(trigger.isDelete){
for (Opportunity o : Trigger.old) {
If (s.contains(o.RecordTypeId)){
setOpptsToDelete.add(o.Id);
}
}
}
List<AdvisorGAS_Production__c> listAdvisorGasDelete = new List<AdvisorGAS_Production__c>();
for (AdvisorGAS_Production__c agpDelete : [Select Id from AdvisorGAS_Production__c where Opportunity__c in :setOpptsToDelete]) {
listAdvisorGasDelete.add(agpDelete);
}
The query in red returns nothing yet when I manually query against the specific Opportunity Id (ie: Select Id from AdvisorGAS_Production__c where Opportunity__c = '0068000000X2fFg'), I get a value.
I can't figure out why it's not returning anything. Here's some debug data I've set up and am getting:
USER_DEBUG|[51,5]|DEBUG|#### Trigger is delete. setOpptsToDelete.size = 1
|SOQL_EXECUTE_BEGIN|[98,47]|Aggregations: 0 |Select Id from AdvisorGAS_Production__c where Opportunity__c in :setOpptsToDelete
USER_DEBUG|[158,2]|DEBUG|### listAdvisorGasDelete.size = 0
For anyone who stumbles upon this...
Ultimatey I stopped trying to handle the "After Delete" event in this trigger and moved the code that I wanted run if an Opportunity was getting deleted, to a "Before Delete" trigger. Once I did that, it worked fine.
All Answers
First of all, thanks for helping!
Trying your suggestions, first I tried changing to trigger.new but got this null error when trying to delete an Opportunity...
System.NullPointerException: Attempt to de-reference a null object
Trigger.AdvisorGasSyncFromOpportunity: line 37, column 25
Here's the debug logged (cleaned up a bit to make it easier to read):
USER_DEBUG|[30,2]|DEBUG|### Trigger is Update = false
USER_DEBUG|[31,2]|DEBUG|### Trigger is Insert = false
USER_DEBUG|[32,2]|DEBUG|### Trigger is Delete = true
USER_DEBUG|[33,2]|DEBUG|### Trigger is Undelete = false
USER_DEBUG|[39,5]|DEBUG|#### Record Type set = 6
USER_DEBUG|[41,4]|DEBUG|Id that was added 0068000000X2ftlAAB
USER_DEBUG|[45,5]|DEBUG|#### Trigger is delete. Count of Oppt in Trigger.old before Record Type filter = 1
USER_DEBUG|[46,5]|DEBUG|#### Trigger is delete. opptsToDelete.size = 1
USER_DEBUG|[47,5]|DEBUG|#### Oppt Record Type ID = 01280000000Ell4AAC
SOQL_EXECUTE_BEGIN|[87,41]|Aggregations:0|Select Id from AdvisorGAS_Production__c where Opportunity__c in :opptsToUnDelete
SOQL_EXECUTE_BEGIN|[93,41]|Aggregations:0|Select Id from AdvisorGAS_Production__c where Opportunity__c in :opptsToDelete
SOQL_EXECUTE_BEGIN|[98,41]|Aggregations:0|Select Id from AdvisorGAS_Production__c where Opportunity__c in :opptsToUpdate
SOQL_EXECUTE_BEGIN|[106,22]|Aggregations:0|Select AccountId, Amount, CloseDate, Contact__c, Contact_Split__c, Contact_2nd__c, Contact_2nd_Split__c, Contact_3rd__c, Contact_3rd_Split__c, Contact_4th__c, Contact_4th_Split__c, StageName, Target_Premium__c, RecordTypeId, Product_Premium_Factor__c from Opportunity where Id in :opptsToUpdate
USER_DEBUG|[151,2]|DEBUG|### listAdvisorGasUnDelete.size = 0
USER_DEBUG|[152,2]|DEBUG|### listAdvisorGasDelete.size = 0
USER_DEBUG|[153,2]|DEBUG|### AdvisorGasInsert.size = 0
Here's the complete trigger code, sorry for the length! There are a lot of debug statements I setup:
This doesn;t have a line number in the code snippet. Can you highlight the line that is getting dereferenced.
Try adding at the end of your trigger the following statement. This will first check if the list is not null. Once we can alleviate that we can then try and see if right opportunities are being added to the list.
This is the line that is null - trying to loop through trigger.new when it's a delete.
There's no need to check for null in the last one because it has no count and therefore it doesn't try the delete.
Greg
For anyone who stumbles upon this...
Ultimatey I stopped trying to handle the "After Delete" event in this trigger and moved the code that I wanted run if an Opportunity was getting deleted, to a "Before Delete" trigger. Once I did that, it worked fine.