You need to sign in to do that
Don't have an account?
BHASKAR GANGULY
too many DML apex superbadge
HI,
i am doing the Apex Specialist superbadge.I have completed the 1st 3 challenges successfully.
while doing the 4 th challenge i am getting govornence limits exception for bulk testing.
can some body please verify my code and tell me what is the problem here.i am stuck in this for lat 2 days.Please help.
below is my code :
This is the Trigger :
trigger MaintenanceRequest on Case (before update, after update) {
// call MaintenanceRequestHelper.updateWorkOrders
//list<case> scase1=[select id from case where id in := trigger.new.id limit 200];
// set<Id> ccase=new set<Id>();
if(trigger.isafter)
{
for(Case c:trigger.new)
{
if((c.Type=='Routine Maintenance' || c.Type=='Repair'))
{
if(c.status=='Closed')
{
// c.adderror('Hi this is bhaskar');
//ccase.add(c.id);
MaintenanceRequestHelper.updateWorkOrders(trigger.new);
}
}
}
}
}
Helper Class :
public class MaintenanceRequestHelper {
public static void updateWorkOrders(list<case> scase){
list<case> acc= new list<Case>();
// list<product2> pp =[select Maintenance_Cycle__c,(select id from case) from product2];
// scase=[select id from case where id in :limit 200];
list<id> sid= new list<id>();
for(case aa:scase)
{
sid.add(aa.Equipment__c);
}
system.debug('sid :'+sid.size());
list<product2> sp=[select id,Maintenance_Cycle__c from product2 where Id =:sid];
system.debug(sp.size());
Map<id, product2> emailToUserMap = new Map<id, product2>();
for (product2 u : sp) {
emailToUserMap.put(u.id, u);
}
for(case a:scase)
{
// product2 p=[select Maintenance_Cycle__c from product2 where Id =:a.Equipment__c];
// double p1= double.valueOf(emailToUserMap.get(Maintenance_Cycle__c));
product2 pp= new product2();
pp= emailToUserMap.get(a.Equipment__c);
decimal p= pp.Maintenance_Cycle__c;
integer p2=p.intValue();
acc.add(new case(type='Routine Maintenance',status='New',Vehicle__c=a.Vehicle__c,Equipment__c=a.Equipment__c,subject='New Routin Maintenance',Date_Reported__c=system.today(),Date_Due__c=(system.today()+p2)));
// a.Date_Due__c
}
insert acc;
}
}
Test class :
/*
* This class can be safely deleted from your org.
* It was only used to ensure 100% test coverage
* for the managed package installation.
*/
@IsTest
private class InstallationTests {
static testmethod void testMaintenancebulkRequest()
{
list<case> a= new list<case>();
list<case> aa= new list<case>();
Vehicle__c v=new Vehicle__c(name='Test BMV');
insert v;
product2 p=new product2(name='Test Product',Maintenance_Cycle__c=10);
insert p;
for(integer i =0;i<200;i++)
{
a.add(new case(type='Repair',status='New'));
}
test.startTest();
insert a;
for(case cc:a)
{
cc.Status='Closed';
cc.Vehicle__c=v.id;
cc.Equipment__c=p.id;
aa.add(cc);
}
// system.debug(aa.size());
update aa;
test.stopTest();
}
}
Thanks,
Bhaskar
i am doing the Apex Specialist superbadge.I have completed the 1st 3 challenges successfully.
while doing the 4 th challenge i am getting govornence limits exception for bulk testing.
can some body please verify my code and tell me what is the problem here.i am stuck in this for lat 2 days.Please help.
below is my code :
This is the Trigger :
trigger MaintenanceRequest on Case (before update, after update) {
// call MaintenanceRequestHelper.updateWorkOrders
//list<case> scase1=[select id from case where id in := trigger.new.id limit 200];
// set<Id> ccase=new set<Id>();
if(trigger.isafter)
{
for(Case c:trigger.new)
{
if((c.Type=='Routine Maintenance' || c.Type=='Repair'))
{
if(c.status=='Closed')
{
// c.adderror('Hi this is bhaskar');
//ccase.add(c.id);
MaintenanceRequestHelper.updateWorkOrders(trigger.new);
}
}
}
}
}
Helper Class :
public class MaintenanceRequestHelper {
public static void updateWorkOrders(list<case> scase){
list<case> acc= new list<Case>();
// list<product2> pp =[select Maintenance_Cycle__c,(select id from case) from product2];
// scase=[select id from case where id in :limit 200];
list<id> sid= new list<id>();
for(case aa:scase)
{
sid.add(aa.Equipment__c);
}
system.debug('sid :'+sid.size());
list<product2> sp=[select id,Maintenance_Cycle__c from product2 where Id =:sid];
system.debug(sp.size());
Map<id, product2> emailToUserMap = new Map<id, product2>();
for (product2 u : sp) {
emailToUserMap.put(u.id, u);
}
for(case a:scase)
{
// product2 p=[select Maintenance_Cycle__c from product2 where Id =:a.Equipment__c];
// double p1= double.valueOf(emailToUserMap.get(Maintenance_Cycle__c));
product2 pp= new product2();
pp= emailToUserMap.get(a.Equipment__c);
decimal p= pp.Maintenance_Cycle__c;
integer p2=p.intValue();
acc.add(new case(type='Routine Maintenance',status='New',Vehicle__c=a.Vehicle__c,Equipment__c=a.Equipment__c,subject='New Routin Maintenance',Date_Reported__c=system.today(),Date_Due__c=(system.today()+p2)));
// a.Date_Due__c
}
insert acc;
}
}
Test class :
/*
* This class can be safely deleted from your org.
* It was only used to ensure 100% test coverage
* for the managed package installation.
*/
@IsTest
private class InstallationTests {
static testmethod void testMaintenancebulkRequest()
{
list<case> a= new list<case>();
list<case> aa= new list<case>();
Vehicle__c v=new Vehicle__c(name='Test BMV');
insert v;
product2 p=new product2(name='Test Product',Maintenance_Cycle__c=10);
insert p;
for(integer i =0;i<200;i++)
{
a.add(new case(type='Repair',status='New'));
}
test.startTest();
insert a;
for(case cc:a)
{
cc.Status='Closed';
cc.Vehicle__c=v.id;
cc.Equipment__c=p.id;
aa.add(cc);
}
// system.debug(aa.size());
update aa;
test.stopTest();
}
}
Thanks,
Bhaskar
Best Answer chosen by BHASKAR GANGULY
Raj Vakati
try this code