Invalid query argument constructed in PlannedMaintenanceUtils script include using method GlideSystem endOfToday() may cause duplicate wm_order records generated incorrectly by the Planned Maintenance Nightly Run job for certain repeat typesDescriptionThe job for the Planned Maintenance Nightly Run queries the sm_m2m_maint_plan_to_record table and queries it based on the next_run_time field, comparing it to the gs.endOfToday(). If a record is retrieved, the code always generates a new wm_order record, even if this record has already been processed for that month and generated a wm_order for the necessary Due day of month and Run time. If the sm_schedule record is trigger of duration, and trigger_type is monthly or annually, and we have already generated a wm_order record for the Due day of month and Run time, we will generate another wm_order record. This is also true if you use the Run on demand Related link on the sm_schedule record.Steps to Reproduce 1) Activate the Planned Maintenance and Field Service Management plugins; do not load demo data.2) Create a new Work Order template; it can be simple, just a template to create a Work Order task to process a checklist monthly.3) Create a new Maintenance Plan to process the checklist for a cmdb_ci_computer_room CI monthly.4) Create the Maintenance Schedule for the plan for:...4a) Trigger: Duration...4b) Trigger type: Monthly...4c) Due day of month: 28...4d) Due time: 23:00:00...4e) Lead Time (in days): 295) Associate the new Work Order template to the Maintenance Schedule.6) Generate the new Maintenance Plan Records for the Maintenance plan.7) On the Maintenance Schedule record, use the Run on demand related link to generate a Work Order for the next monthly period.8) Click Run on demand again; there is no check for the existing record, and a duplicate will be generated.9) On the Next run time, if it is the same month, the daily job will also select this record and generate another work order.WorkaroundYou can create a before-insert business rule, order 10001, on the wm_order table. This example checks to see if a wm_order record already exists for the month in question. (function executeRule(current, previous /*null when async*/) { // This business rule is to check if a work order has already been scheduled for this month. // It is intended to prevent the automation from creating a work order for a recurring monthly schedule. // It checks the maintenance schedule for the correct duration, and then queries for the requested_due_by date. var smschedGR = new GlideRecord('sm_schedule'); smschedGR.get(current.getValue('maintenance_schedule')); if (smschedGR.getRowCount() == 1) { var repeat = smschedGR.getValue('repetition'); var repeatType = smschedGR.getValue('trigger_type'); if (repeat == '10' && repeatType == '4') { var CheckStartTime = new GlideDateTime(gs.beginningOfNextMonth()); var CheckEndTime = new GlideDateTime(gs.beginningOfNextMonth()); CheckEndTime.addMonthsUTC(1); var wmorderGR = new GlideRecord('wm_order'); wmorderGR.addQuery("maintenance_schedule","=",current.getValue('maintenance_schedule')); wmorderGR.addQuery("requested_due_by",">=",CheckStartTime); wmorderGR.addQuery("requested_due_by","<",CheckEndTime); wmorderGR.query(); if (wmorderGR.getRowCount() > 0) { current.setAbortAction(true); } } } })(current, previous); Related Problem: PRB1375030