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
symantecAPsymantecAP 

Too many DML rows

 

I am getting the error given below... I am trying to use @ future and it is not allowing me. Kindly help

 

 

 

 

global class schedulePMWorkOrderCreation implements Schedulable{

    public static  String CRON_EXP = '0 0 0 3 9 ? 2022'
    public date periodStart;
    public date periodEnd;
    //Override 'execute' method of Schedulable interface
    global void execute(SchedulableContext sc) {
        
        //Get Current Date
        date currentDate = Date.today();
        //periodStart = Date.today()-1;               
        //****Calculate Period Start and End Dates
        if(currentDate.month() == 12)
            periodStart = date.newInstance(currentDate.year()+1, 1, 1);
        else
            periodStart = date.newInstance(currentDate.year(), currentDate.month()+1, 1);
        
        periodEnd = periodStart.addMonths(1) - 1;
        
        System.debug('**************Period End: '+periodEnd);    
        /****Create a query for retrieving all the fields required for creating a work order
             using the conditions Next_PM_Date__c is between Period Start and End Dates and Asset is active?*/
        //String returnQuery = 'Select id, Facility__c, Primary_FSE__c, PM_Plan__c, Next_PM_Date__c, Effective_PM_Frequency__c, Last_Completed_PM_Date__c, Last_Scheduled_PM_Date__c, Facility__r.OwnerId from MVS_Asset__c where (Next_PM_Date__c >= :periodStart and Next_PM_Date__c <= :periodEnd ) and Status__c = \'Active\' and Outstanding_PM_Work_Order__c = false';
        //String returnQuery = 'Select id, Facility__c, Primary_FSE__c, PM_Plan__c, Next_PM_Date__c, Effective_PM_Schedule_Type__c, Effective_PM_Frequency__c, Last_Completed_PM_Date__c, Last_Scheduled_PM_Date__c, Facility__r.OwnerId from MVS_Asset__c where Next_PM_Date__c <= :periodEnd and Status__c in (\'Active\',\'Reactivated\') and Outstanding_PM_Work_Order__c = false';
        //Updated 2010-11-23 to add filter on Effective_PM_Frequency__c <> 'Not Required'
        String returnQuery = 'Select id, Facility__c, Primary_FSE__c, PM_Plan__c, Next_PM_Date__c, Effective_PM_Schedule_Type__c, Effective_PM_Frequency__c, Last_Completed_PM_Date__c, Last_Scheduled_PM_Date__c, Facility__r.OwnerId from MVS_Asset__c where Next_PM_Date__c <= :periodEnd and Status__c in (\'Active\',\'Reactivated\') and Outstanding_PM_Work_Order__c = false and Effective_PM_Frequency__c <> \'Not Required\'';

        
        //Create an object for createPMWorkOrder class used to create work orders.
        createPMWorkOrder b = new createPMWorkOrder ();
        b.query = returnQuery;
        //Use executeBatch method to perform the DML operations in batches
        Database.executeBatch(b);
    }
}

 

 

 

 

 

 

 

cid:image001.png@01CB90AD.30559C50

John CasimiroJohn Casimiro

I believe the error is occuring in your class, "CreatePMWorkOrder".  We'll have to see that in order to solve your problem.

symantecAPsymantecAP

Sure here is "CreatePMWorkorder" code

global class createPMWorkOrder implements Database.Batchable<sObject>{
    
    public String query;
    
    //Override start method of Database.Batchable interface
    global Database.QueryLocator start(Database.BatchableContext BC){
        //Get Current Date
        date currentDate = Date.today();
        date periodStart;
     
        date periodEnd;
        
        //****Calculate Period Start and End Dates
        if(currentDate.month() == 12)
            periodStart = date.newInstance(currentDate.year()+1, 1, 1);
        else
            periodStart = date.newInstance(currentDate.year(), currentDate.month()+1, 1);
        //periodStart = Date.today()-1;
        periodEnd = periodStart.addMonths(1) - 1;
            
        /****Create a query for retrieving all the fields required for creating a work order
             using the conditions Next_PM_Date__c is between Period Start and End Dates and Asset is active?*/
        //String returnQuery = 'Select id, Facility__c, Primary_FSE__c, PM_Plan__c, Next_PM_Date__c, Effective_PM_Frequency__c, Last_Completed_PM_Date__c, Last_Scheduled_PM_Date__c, Facility__r.OwnerId from MVS_Asset__c where (Next_PM_Date__c >= :periodStart and Next_PM_Date__c <= :periodEnd) and Status__c = \'Active\'';
        
        return Database.getQueryLocator(query);
    }
    
    //Override execute method of Database.Batchable interface
    global void execute(Database.BatchableContext BC, List<MVS_Asset__c> assets){
        List<Case> pmWorkOrders = new List<Case> ();
        List<MVS_Asset__c> mvsAssetList = new List<MVS_Asset__c>();
        
        //Get Record Type ID of Preventative Maintenance
        RecordType RecType = [select id from RecordType where name = 'Preventative Maintenance' and SobjectType = 'Case'];
        
        //Create Work Orders for all the assets in the context
        for (MVS_Asset__c mvsAsset : assets){
            //Determine Work Order Owner
            Id workOrderOwner = mvsAsset.Primary_FSE__c;
            if (workOrderOwner == null){
                workOrderOwner = mvsAsset.Facility__r.OwnerId;
            }
            
            //Initiate a Work Order
            Case pmWorkOrder = new Case (   OwnerId = workOrderOwner,
                                            MVS_Asset__c = mvsAsset.Id,
                                            AccountId = mvsAsset.Facility__c,
                                            Status = 'Open',
                                            Priority = 'Schedule Activity',
                                            RecordTypeId = RecType.Id);
            //Add Work Order to the list                                
            pmWorkOrders.add(pmWorkOrder);
            
            //Assigning the List values to variables for better performance
            //date lastScPMDate = mvsAsset.Last_Scheduled_PM_Date__c;
            //date nextPMDate = mvsAsset.Next_PM_Date__c;
            //date lastComPMDate = mvsAsset.Last_Completed_PM_Date__c;
            String effectivePMFreq = mvsAsset.Effective_PM_Frequency__c;
            
            //Assign Last Scheduled PM Date to Next PM Date
            //mvsAsset.Last_Scheduled_PM_Date__c = mvsAsset.Next_PM_Date__c;
            
            mvsAsset.Last_Scheduled_PM_Date__c = System.today();
            
            Integer effPMFrequency = 0;
            if (effectivePMFreq != 'Manufacturer\'s Recommendation' && effectivePMFreq!='Not Required')
                effPMFrequency = decimal.valueOf(effectivePMFreq).intValue();
            
            //Calculate Next PM Date    
            if (mvsAsset.Effective_PM_Schedule_Type__c == 'Float')
                mvsAsset.Next_PM_Date__c = null;
            else{
                mvsAsset.Next_PM_Date__c = System.today().addMonths(effPMFrequency);
            }
                
            mvsAsset.Outstanding_PM_Work_Order__c = true;
            /*if (mvsAsset.PM_Schedule_Type__c == 'Floating'){
                if(mvsAsset.Last_Completed_PM_Date__c != null)
                    mvsAsset.Next_PM_Date__c = mvsAsset.Last_Completed_PM_Date__c.addMonths(effPMFrequency);
                else
                    mvsAsset.Next_PM_Date__c = nextPMDate.addMonths(effPMFrequency);
            }
            else{
                if(lastScPMDate != null)
                    mvsAsset.Next_PM_Date__c = lastScPMDate.addMonths(effPMFrequency);
                else
                    mvsAsset.Next_PM_Date__c = nextPMDate.addMonths(effPMFrequency);
            }*/
            
               //Calculate Last Scheduled Date
              date lstschdPMDate = mvsAsset.Last_Scheduled_PM_Date__c;
              Integer currentmonth = system.today().month();
            if(mvsAsset.Next_PM_Date__c.month() == currentmonth )
            
                        mvsAsset.Last_Scheduled_PM_Date__c = System.today();
            if(mvsAsset.Effective_PM_Schedule_Type__c=='fixed')
            mvsAsset.Next_PM_Date__c = lstschdPMDate.addMonths(effPMFrequency);

            if(mvsAsset.Next_PM_Date__c.month() < currentmonth )
            
                        mvsAsset.Last_Scheduled_PM_Date__c = System.today()+1;
            if(mvsAsset.Effective_PM_Schedule_Type__c=='fixed')
            mvsAsset.Next_PM_Date__c = lstschdPMDate.addMonths(effPMFrequency);
            
            
            mvsAssetList.add(mvsAsset);
        }
        System.debug('*******************************Updating MVS Asset');
        //Insert all new Work Orders
        update mvsAssetList;
        System.debug('*********************************Inserting Work Orders');
        insert pmWorkOrders;
    }
    
    //Override finish method of Database.Batchable interface
    global void finish(Database.BatchableContext BC){
    }
}

 

 

symantecAPsymantecAP

any updates please

John CasimiroJohn Casimiro

I don't see anything out of the ordinary in your batch apex. It looks like you are creating a Case and updating the MVS_Asset. As far as I can tell you should have a max of 400 DML rows(200 for the update and 200 for the cases), which is well below the 10k limit.

 

Have you tried dumping mvsAssetList.size() and pmWorkOrders.size() to the debug log and watching the debug log as it excutes to insure that they aren't over 10k combined?

 

You can also use Limits.getDMLRows() to keep track where you are in relation to the governor limit.

 

Here's more info on Limits Methods,

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_methods_system_limits.htm?SearchType=Stem

 

Wish I could be more help. Good luck!