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

Trigger bulksafe - too many DML statements: 21
Hi there,
I am trying to create a trigger on a custom object "VerwijderFnc__c" what deletes records in another custom object "Functionaris__c"
process is: insert a record into VerwijderFnc__c and get the Id from the Functionaris__c to delete those Functionaris__c record(s).
Sometimes i get more then 20 records as a result! and that is too much!
so i try to make this trigger bulksafe .... But i can't get ik to work for me !
This was 1e my code for the trigger: (NOT BULKSAFE)
trigger deleteFromFunctionaris on VerwijderFnc__c (before insert) {
for(VerwijderFnc__c verwijdertabel: Trigger.new){
for (Functionaris__c fs: [SELECT Id FROM Functionaris__c WHERE mskey__c = :verwijdertabel.MSkey__c]){
//Delete all the Functionaris__c entries with given mskey__c
delete fs;
}
}
}
then i tried something like: (BUT ALSO GIVES TOO MANYDML STATMENTS)
trigger deleteFromFunctionaris on VerwijderFnc__c (before insert) {
Map<String, VerwijderFnc__c> fncMap = new Map<String, VerwijderFnc__c>(); for (VerwijderFnc__c fnc : System.Trigger.new) { if ((fnc.mskey__c != null) && (System.Trigger.isInsert || (fnc.mskey__c != System.Trigger.oldMap.get(fnc.Id).mskey__c))) {
fncMap.put(fnc.mskey__c, fnc);
}
}
for (Functionaris__c func : [SELECT Id FROM Functionaris__c WHERE mskey__c IN :fncMap.KeySet()]) { delete func;
}
}
!! Help !!
trigger deleteFromFunctionaris on VerwijderFnc__c (before insert) {
List<Id> delId = new List<Id>();
for(VerwijderFnc__c verwijdertabel: Trigger.new){
delId.Add(verwijdertabel.MSkey__c);
}
List<Functionaris__c> DeleteThese = new List<Functionaris__c>([SELECT Id FROM Functionaris__c WHERE mskey__c = :delId]);
//Delete all the Functionaris__c entries with given mskey__c
if(DeleteThese.size() > 0) {
delete DeleteThese;
}
}
All Answers
Check this out..hope this works....
trigger deleteFromFunctionaris on VerwijderFnc__c (before insert) {
for(VerwijderFnc__c verwijdertabel: Trigger.new){
List<Functionaris__c> DeleteThese = new List<Functionaris__c>([SELECT Id FROM Functionaris__c WHERE mskey__c = :verwijdertabel.MSkey__c]);
}
//Delete all the Functionaris__c entries with given mskey__c
delete DeleteThese;
}
This way should be able handle large record special when you use dataloader
trigger deleteFromFunctionaris on VerwijderFnc__c (before insert) { List<Id> delId = new List<Id>(); for(VerwijderFnc__c verwijdertabel: Trigger.new){ delId.Add(verwijdertabel.MSkey__c); } List<Functionaris__c> DeleteThese = new List<Functionaris__c>([SELECT Id FROM Functionaris__c WHERE mskey__c = :delId]); //Delete all the Functionaris__c entries with given mskey__c delete DeleteThese; }
trigger deleteFromFunctionaris on VerwijderFnc__c (before insert) {
List<Id> delId = new List<Id>();
for(VerwijderFnc__c verwijdertabel: Trigger.new){
delId.Add(verwijdertabel.MSkey__c);
}
List<Functionaris__c> DeleteThese = new List<Functionaris__c>([SELECT Id FROM Functionaris__c WHERE mskey__c = :delId]);
//Delete all the Functionaris__c entries with given mskey__c
if(DeleteThese.size() > 0) {
delete DeleteThese;
}
}
First of all, thanks for all the input!
Second, How is it possible this is the accepted answer???? I did not accept it !? ...But i like it so i will accept it anyway.
1 little thingy in the code... the "=" in the select should be an "IN"...
Thanks a lot !
Have a nice day !
Hi, I would like to know how to write the testcode for the last 3 rows of code from this trigger,
I already wrote the following testcode :
TRIGGER:
//Store all incomming values in a temp list
List<Id> delId = new List<Id>(); for(VerwijderFnc__c verwijdertabel: Trigger.new){delId.Add(verwijdertabel.MSkey__c);
}
//Delete all the Functionaris__c entries with given mskey__c
List<Functionaris__c> DeleteFNC = new List<Functionaris__c>([SELECT Id FROM Functionaris__c WHERE mskey__c IN :delId]);if(DeleteFNC.size() > 0) {
delete DeleteFNC;}
}
TESTCODE:
VerwijderFnc__c verwijdertabel2=new VerwijderFnc__c();verwijdertabel2.MSKEY__c=Bedr.LRRORG__c; //this is a prefetched textstring
insert verwijdertabel2;
This code only gives me 50% coverage for the triggercode....
Does anybody know how to complete my testcode?
//you have to insert test records for msKey__c here, please refer below code
List<msKey__c> insMS = new List<msKey__c>(); for (i,i<5,i++){ msKey__c sinMs = new msKey__c( Name = 'test' ); insMS.add(sinMS); } insert insMS; List<msKey__c> forDelete = [Select Id From msKey__c where name ='test']; //Insert Function record for testing List<Functionaris__c> insFun = new List<Functionaris__c>(); for (msKey__c a:forDelete){ Functionaris__c sinFun = new Functionaris__c( name = 'test', msKey__c = a.Id ); insFun.add(sinFun); } insert insFun; //Insert VerwijderFnc__c for testing List<VerwijderFnc__c> insVer = new List<VerwijderFnc__c>(); for (msKey__c a:forDelete){ VerwijderFnc__c sinFun = new VerwijderFnc__c( name = 'test', msKey__c = a.Id ); insVer.add(sinFun); } insert insVer;
Thanks for the testcode...
after trying to implement the testcode i discoverd that my initial testcode gave me 100% coverage afterall... i don't know what i did to NOT get the 100% coverage.
Also i want to mention that the given anwser which is accepted is not an real Bulksafe trigger but it will allow about 200 entries at a time!