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
KrishnadasKrishnadas 

using @future

Hi,

I am writing a trigger that runs on a before delete event. I am also trying to use the @future call. I am not sure whether I am using it correctly and require some help. I have 3 objects Obj1__c, Obj2__c, Obj3__c that are related but not in a Master-Detail way.
There is a field Qty__c in Obj1__c. Depending on the value entered for Qty it creates that many Obj2__c with reference to Obj1__c. Every Obj2__c can have Obj__3 associated to it. What I am trying to do is a Cascade delete. When I delete one Obj1__c record, I would like to delete all Obj2__c records and when the Obj2__c records are deleted I would like to all Obj3__c records associated with it.
The problem I face is that I either keep entering DML row exception or Query rows exception. To overcome this I tried the @future option, though I hardly get any errors but it does not delete too. And the ApexJobs shows them as complete while the Batch Processed shows as 0.
I am attaching the latest code I tried to achieve this.

Thanks
Krishnadas
www.steadfastglobal.com
 

trigger recDelete on Obj1__c (before delete) { Set<Id> obj1Id = new Set<Id>(); for(Obj1__c obj1 : trigger.old) { obj1Id.add(obj1.Id); } triggerController.deleteObj1(obj1Id);}public class triggerController { @future public static void deleteObj1(Set<Id> obj1) { List<Obj1__c> obj1List = new List<Obj1__c>(); List<Obj2__c> obj2List = new List<Obj2__c>(); List<Obj3__c> obj3List = new List<Obj3__c>(); Set<Id> obj2 = new set<Id>(); double Qty, obj2DelQty, obj3DelQty; double obj2Count, obj3Count; obj1List = [select Id, Qty__c from Obj1__c where Id in :obj1]; for(Obj1__c o1 : Obj1List) { Qty = o1.Qty__c; } obj2DelQty = Qty; while(obj2DelQty > 0) { obj2List = [select Id from Obj2__c where Obj1Id__c in :obj1 limit 999]; obj3Count = [select id from Obj3__c where Obj2Id__c in :obj2List]; obj3DelQty = obj3Count; while(obj3DelQty > 0) { obj3List = [select Id from Obj3__c where Obj2Id__c in :obj2List limit 999]; obj3DelQty = obj3DelQty – obj3List.size(); delete obj3List; } obj2DelQty = obj2DelQty – obj2List.size(); delete obj2List; } }}

 

 
ShikibuShikibu

Krishnadas,

 

Your code formatting is all broken; I've discovered that happens when I post in Safari, but not in Firefox.

 

Salesforce: can you fix this? Safari 4 is acid3 compliant. 

KrishnadasKrishnadas

the trigger

 

trigger recDelete on Obj1__c (before delete) {
Set<Id> obj1Id = new Set<Id>();
for(Obj1__c obj1 : trigger.old) {
obj1Id.add(obj1.Id);
}
triggerController.deleteObj1(obj1Id);
}

 the class

 

public class triggerController {
@future
public static void deleteObj1(Set<Id> obj1) {
List<Obj1__c> obj1List = new List<Obj1__c>();
List<Obj2__c> obj2List = new List<Obj2__c>();
List<Obj3__c> obj3List = new List<Obj3__c>();
Set<Id> obj2 = new set<Id>();
double Qty, obj2DelQty, obj3DelQty;
double obj2Count, obj3Count;
obj1List = [select Id, Qty__c from Obj1__c where Id in :obj1];
for(Obj1__c o1 : Obj1List) {
Qty = o1.Qty__c;
}
obj2DelQty = Qty;
while(obj2DelQty > 0) {
obj2List = [select Id from Obj2__c where Obj1Id__c in :obj1 limit 999];
obj3Count = [select id from Obj3__c where Obj2Id__c in :obj2List];
obj3DelQty = obj3Count;
while(obj3DelQty > 0) {
obj3List = [select Id from Obj3__c where Obj2Id__c in :obj2List limit 999];

obj3DelQty = obj3DelQty – obj3List.size();
delete obj3List;
}
obj2DelQty = obj2DelQty – obj2List.size();
delete obj2List;
}
}
}

 

 

 

 

 

 

SteveBowerSteveBower

Well, you're running into problems because you have many SELECT statments within loops.  You might want to go back to the APEX documentation and read up on Bulk triggers. 

 

I also think you're going about this the wrong way.  I think that unless there are extenuating circumstances, simple things like delete triggers should be in their own triggers related to their own objects.

 

So, if you just want a simple cascading delete perhaps you could create a simple trigger on Object 2:

 

// Clearly you need to put in Try/Catch and some error processing... 

 

trigger delObj3 on Obj2__c(before delete) {

     delete [select id from Obj3__c where Obj2Id__c in trigger.oldMap.keySet()]

}

 

 

and a similar trigger on Object 1

 

trigger delObj2 on Obj1__c(before delete) {

     delete [select id from Obj2__c where Obj1Id__c in trigger.oldMap.keySet()]

}

 

Hope this helps... Steve
KrishnadasKrishnadas

Hi Steve,

Initially I had believed that the simple delete in the corresponding objectw will be good and followed what you had suggested. But when I started to encounter problems in that I had to change, but the problem is still not resolved. for example Obj1__c has 1 record. It is related to 100 records of Obj__2 and each of the 100 records of Obj2__c is related to 20 records of Obj3__c. Now the list size on Obj1__c for delete grows to 2000. And it cannot be managed. So how do I avoid this.

Thanks

Krishnadas

www.steadfastglobal.com 

SteveBowerSteveBower

I'd have to look at this more carefully.  What problems did you run into? The triggers on Object 1 and Object 2 don't seem to be pushing any limits if they are only handling 100 and 20 records respectively.   How are you deleting your 2000 object 1's?

 

Best, Steve.

 

P.S. However. Consider that the main benefit Salesforce.com brings to the table is NOT it's prowess as a database system.  If you've got a situation where you're deleting 2000 first level objects at a time, and each one cascades down to 2000 more records, then you're deleting 4 million records at a time.

 

One has to ask the question: Is Salesforce really the right solution for this particular database need?  If you're doing this kind of delete so often that you need a trigger to manage it, then who is really looking at those 4 million records?

 

But I digress.

KrishnadasKrishnadas
Hi,

Let me make it more clear. 
Obj1__c = 1 record
Obj2__c = 100 records
Obj3__c = 20 records for all 100 Obj2__c
All that I am trying to do is a cascade delete operation. I delete the record of Obj1__c which will lead to the deletion of the 100 Obj2__c records and for each of the 100 Obj2__c records the 20 Obj__3 records are deleted.

This number of records are just examples. It can be more or it can be less. I keep encountering Too many DML rows problem. This happens even if I am trying to pass the records to a list and then delete the list. My next question is that when inserting a list with 1000 records using the insert statement it counts it as 1 DML statement, but when doing the same but it instead of insert it is delete then it goes out of bounds and give me the exception stating too many DML rows showing the number of records in the List. Why is it so? 
 
Thanks
Krishnadas
www.steadfastglobal.com