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

current month methods

The logic we need to have to update these 2 fields are:


  • If the Next PM Date is in the current month (e.g. incoming inspections):

o   Set Last Scheduled date equal to today’s date

o   If Effective PM Schedule Type = fixed, Set Next PM date equal to the newly calculated Last Scheduled date + Effective frequency

  • If the Next PM Date is before the current month (e.g. regularly scheduled PM’s):

o   Set Last Scheduled date equal to today’s date+ 1 month (because that’s when the PM will actually be due)

o   Again, if Effective PM Schedule Type = fixed, set Next PM date equal to the newly calculated Last Scheduled date + Effective frequency

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 periodStart;
        date periodEnd;
        //****Calculate Period Start and End Dates
        if(currentDate.month() == 12)
            periodStart = date.newInstance(currentDate.year()+1, 1, 1);
            periodStart = date.newInstance(currentDate.year(), currentDate.month()+1, 1);
        //periodStart =;
        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                                
            //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 =;

            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;
                mvsAsset.Next_PM_Date__c =;
            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);
                    mvsAsset.Next_PM_Date__c = nextPMDate.addMonths(effPMFrequency);
                if(lastScPMDate != null)
                    mvsAsset.Next_PM_Date__c = lastScPMDate.addMonths(effPMFrequency);
                    mvsAsset.Next_PM_Date__c = nextPMDate.addMonths(effPMFrequency);
 if ( mvsAsset.Next_PM_Date__c == currentDate.month)
                        mvsAsset.Last_Scheduled_PM_Date__c =;
 if ( mvsAsset.Next_PM_Date__c == currentDate.month)                        mvsAsset.Last_Scheduled_PM_Date__c =;                      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){ } }

 i am trying the above highlited code.. i am getting error


kindly help



What you should try rather is-



mvsAsset.Next_PM_Date__c.month == currentDate.month


As Next_PM_Date__c is a DATE while currentDate.month returns an integer representing the month of year (1- January).



- Satgur




Date.Day(), Date.Month(), and Date.Year() are getter functions; make sure you're using parenthesis as if it were a function call. Secondly, you can use date operations to make it easier to determine the date:; // Same "date" next month from today.
currentDate.addMonths(1).addMonths(frequency); // You can chain them together, too.


Please check the documentation for more details on the functions available.