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
lnryanlnryan 

Trouble returning deleted records with ALL ROWS key word

I have a procedure that can be triggered by an update to or deletion of any one of several inter-related objects based around Opportunities. The trigger retrieves the related Opportunity and all the relevant sub-records in all the relevant objects by way of a single query in the form of

 

 

Opportunity[] opps = Database.query('SELECT [...fields and children...] FROM Opportunity WHERE Id IN (SELECT Opportunity__c FROM '+childObjectName+' WHERE Id IN :triggeredIds)');

 

The trouble, of course, is that when this is coming from a delete trigger, it finds no Opportunities related to the deleted record. I tried putting in the keyword 'ALL ROWS' but when this was placed outside the WHERE clause subselect it returned no records and when it was placed within the WHERE clause subselect (shown below), I received the error "Expecting ')'. Found ALL"

 

FROM Opportunity WHERE Id IN (SELECT Opportunity__c FROM '+childObjectName+' WHERE Id IN :triggeredIds ALL ROWS)');

 

 Does anyone know If there is a proper syntax for retrieving non-deleted parents of deleted child records using subselects?

hisrinuhisrinu

How are you passing the values to triggeredIds, is it a trigger.old or trigger.new

 

 

It would be great if you could post your code

 

lnryanlnryan

It passes Trigger.oldMap from the delete trigger and trigger.newMap for all other cases. I've already verified that the keys are passed into the SOQL statement correctly, so if possible, i just need to find out the SOQL syntax needed to retrieve parent records through their relationship to deleted child records.

 

Thanks!