---
title: "Preventive maintenance"
canonical: "https://help.fieldbuddy.com/space/FBDOCS/4907466764/Preventive%20maintenance"
format: markdown
---
## 🔖 Introduction

Preventive maintenance allows you to configure service plans for your customers that will automatically generate recurring work orders to ensure that you will maintain the installations before issues occur. These maintenance plans can be based on service level agreements (SLA) with specific customers, but also on specific products. 

A Service Plan describes which Installed Products or Locations will automatically get a Work Orders created. Based on settings you configure on the Service Plan, the system will generate these Work Orders automatically or manually. 


> ℹ️ For the data model of preventive maintenance see: [https://upperdeck.atlassian.net/wiki/spaces/FBDOCS/pages/4910448713](https://upperdeck.atlassian.net/wiki/spaces/FBDOCS/pages/4910448713)

## Data Model

There are 3 main objects that are used by Preventive Maintenance module - Service Plan, Service Plan Coverage, Service Plan Item and Service Plan Execution

## Service Plan

An example Service Plan

![image](media://be520713-a3af-49c7-bbfb-3fb0e5405377)

Explanation of the fields on Service Plan:

| **Field** | **Explanation** | **Required** |
| --- | --- | --- |
| Service Plan Name | This number is automatically generated by the system | - |
| Service Agreement | This is a look-up to Service Agreement, in order to relate a Service Plan to a contract. | NO |
| Active | This enables/disables the Service Plan. It is required to be enabled for it to work. | YES |
| Service Plan Coverages | A field that displays the amount of coverages on this plan. | - |
| Additional Configuration | An advanced way to set default field values for the work orders that are generated by the system. This requires a JSON input. Check below the table for an example | NO |
| Start Date | The start date of the period in which the Service Plan is valid. When the Service Plan is executed. The date should be within the start and end-date. | YES |
| End Date | The end date of the period in which the Service Plan is valid. | NO |
| Service Plan Items | A field that displays the amount of items on this plan. | - |
| Work Order Type | The API value of the Work Order type picklist on FIELDBUDDY__Work_Order__c object. In case its empty it will automatically be ‘PREVENTIVE_MAINTANCE’. | NO |
| **Interval** |
| Schedule Interval | Schedule Interval, together with the Schedule Interval Unit, defines the frequency of the Service Plan. | YES |
| Schedule Interval Unit | The time unit used for the Interval field. | YES |
| **Schedule** |
| Work Order Create In Advance | Work Order Create In Advance defines how much time in advance, relative to the Covered until on the Service Plan Coverage, Work Orders will be created. | YES |
| Work Order Create In Advance Unit | The time unit used for the 'In Work Order Create In Advance' field. | YES |
| Work Order Schedulable Period Start | The time allowed to later plan/execute the Work Order relative to the Covered until on Service Plan Coverage. | YES |
| Work Order Schedulable Period Start Unit | The time unit used for the Work Order Schedulable Period Start field. | YES |
| Work Order Schedulable Period End | The time allowed to later plan/execute the Work Order relative to the Covered until on Service Plan Coverage. | YES |
| Work Order Schedulable Period End Unit | The time unit used for the Work Order Schedulable Period End‏ field. | YES |

### Additional configuration example

Additional Configuration allows to specify a certain hardcoded field values to be populated on created Service Requests, Work Orders and other records. Format is 

```json
{
  "SObjectApiName" : 
    {
      "fieldApiName": "fieldvalue",
      // more fields
    },
   //more objects
}
```

An example of ‘**Additional configuration**’ is the following:

<details>
<summary>Additional Configuration</summary>

```
{
    "FIELDBUDDY__Service_Request__c" : 
    {
        "FIELDBUDDY__Status__c" :"Open",
        "FIELDBUDDY__Type__c" : "Preventive Maintenance"
    },
    "FIELDBUDDY__Work_Order__c" : 
    {
        "FIELDBUDDY__Type__c" :"INSPECTION"
    },
    "FIELDBUDDY__Work_Order_Line_Item__c" : 
    {
        "FIELDBUDDY__Status__c" :"Closed"
    }
}
```
</details>

In this example the fields Status and Type are set on Service Request. The type on Work Order and the status on Work Order Line Item. Any field on Service Request, Work Order, Work Order Line Item can be used.

### Service Plan Items

The Service Plan Items on a Service Plan are the Work Order Items that are automatically added to the Work Orders that are generated by the Preventive Maintenance module. For example, which parts or checklists should be automatically be available on the Work Order. These are then directly shown to FieldBuddy Swift user when opening the Work Order.

An example of Service Plan Items:

![image](media://32e4cc2b-dad5-4ae6-a1c2-77e4a279c431)

A Work Order Item can be part of multiple Service Plans and a Service Plan can have multiple Items.

Explanation of the fields on Service Plan Item:

| **Field name** | **Explanation** | **Required** |
| --- | --- | --- |
| Service Plan Item Name | This number is automatically generated by the system | - |
| Service Plan | The look-up to the related Service Plan | YES |
| Work Order Item | The look-up to the related Work Order Item | YES |
| Default Quantity | In case of quantity being used (often for Parts) the optional default value | NO |

### Service Plan Coverages

The Service Plan Coverages show which Installed Products or Locations are related to the Service Plan. An Installed Product and Location can be part of multiple Service Plans and a Service Plan can cover multiple Installed Products or Locations.

An Installed Product named “Samsung LPC250SM - 786” is part of **two **Service Plans (see example below). This could be an applicable situation if this Installed Product requires to be maintained once every quarter for it to be cleaned and once every year for replacing parts. In this case you have two separate Service Plans, one with a frequency of every quarter and includes an activity of cleaning and one that has a frequency of every year and has a few parts added.

Installed product:

![image](media://b0892a64-a153-4468-81d0-129ca36f4e71)

Service Plan coverages on the Service Plan:

![image](media://af04ac3d-396d-4c70-b0d9-e6ebb3bfefc1)

Explanation of the fields on Service Plan Coverage:

| **Field ** | **Explanation** | **Required** |
| --- | --- | --- |
| Service Plan Item Coverage | This number is automatically generated by the system | - |
| Type | There can be three types:<br>- Installed Product (The system will generate a regular Work Order that is related to a single Installed Product.)
- Location All (The system will generate a Work Order related to a Location and all Installed Products on that Locations are part of the Work Order.)
- Location Selected (The system will generate a Work Order related to a Location but it is required to specify which Installed Products should be part of the Work Order) | YES, if empty ‘Installed Product’ is assumed. |
| Installed Product | If Type is set to ‘Installed Product’ this field is required to be filled in with an Installed Product | YES, if type is Installed Product |
| Location | If Type is set to ‘Location All’ or ‘Location Selected’ this field is required to be filled in with an Location. | YES, if type contains Location |
| Covered until | The date until the Installed Product or Location is covered. This is based on the previously generated Work Order and will be updated by the system automatically | YES |
| Next Work Order Plan Date | This field displays what the next ‘Covered until’ value will be when the next Work Order is generated by the system | - |
| Next Work Order Creation Date | This field displays the date when the next Work Order will be created and it depends on the configured settings on the Service Plan | - |
| Service Plan Coverage | <span style="color: #ff5630">This field is used in the case when 'Location Selected' is chosen as Type.</span> | NO |

### Service Plan Executions

The Service Plan Executions are records that are automatically created each time a valid and active Service Plan is executed manually by the user or automatically by the system.

It contains the technical log file (diagnostic data) of what happend when the system tried creating the required Work Orders. This helps an administrator in configuring the module correctly and the Service Plan Execution is the relation between the newly created Work Order (via Service Request) and the Service Plan.

Service Plan Execution examples:

![image](media://056696e7-d0fa-4c4c-afbb-844e85635088)

Explanation of the fields on Service Plan Execution:

| **Field ** | **Explanation** | **Required** |
| --- | --- | --- |
| Service Plan Execution Name | This number is automatically generated by the system | - |
| Service Plan | The related Service Plan | YES |
| Insert Data | If enabled, the data actually generated by the system. If disabled, it will display a log file of what data should have been created. This helps an administrator during configuration of the Service Plan | - |
| Status | If ‘Succeeded’ the Service Plan was properly executed without issues. If ‘Failed’ there’s an error message in the diagnostic data. | - |
| Target Date | The date that the system uses as input for it to generate data. By default its always today. An administrator can choose to overwrite this for configuration purposes | YES |
| Diagnostic Data | The technical information on how many records were generated by the system and if they are actually inserted yes/no. Error messages are shown here. | - |

## Global Settings

There are few options that needs to be taken into account during Preventive Maintenance.  
Those are located at Custom Settings - Fieldbuddy Settings  
Those are

- PM: Disable Blank ServicePlan Executions (default - false) -  In case if there are no Work Orders/Service Requests created, there will be no Service Plan Executions inserted
- PM: Default Work Order Duration - Default Duration of Work Order (in minutes)
- PM: Insert Service Requests (default - false) - Determines if Service Requests are inserted for Location-Based Work Orders

## How to Run preventive maintenance

During the configuration of Preventive Maintenance an administrator would like to run his Service Plan manually to monitor the result. In order to make sure he gets the expected result. Running it manually is then a good option. 

### Execute Single Service Plan

In order to run one Service Plan manually, an action Run can be used from Service Plan layout (if it is not on page layout – it could be added there)

![image](media://9b72fdfd-ea7a-481a-9a4a-74eff017b3a4)

When clicked, it will show the following screen

![image](media://0f3bf760-6c81-43d7-9b11-737276d1dd5f)

Then Execute Now button will start the schedule job. It will take few minutes till it is completed.

In the end, you can return back to the Service Plan page and review Service Plan Execution records and Diagnostic Data

In case if it is not on the page layout – it can be added as an Quick Action on page layout editor

### Execute all Service Plans 

Preventive Maintenance can be executed via Configuration Panel → Preventive Maintenance  


![image-20250925-130752.png](media://0c022ba1-5e73-4361-9a2f-b62de82249c9)

It is possible to click Run Now Once to run Preventive Maintenance job once

There is also an alternative way - (older) page Preventive Maintenance Schedule Overview

In order to manually execute all (or few selected) Service Plans, you need to navigate to the following page - `<ORG URL>/apex/FIELDBUDDY__PMScheduleOverview`

Then you will be able to see similar page 

![image](media://0a62ecb7-e04c-4118-87e7-6064eb68b882)

In that page it will possible to specify IDs service plans of comma-separated format. If no ids are specified – it will run all of the Service Providers

Below you  can also see list of the historical executions and appropriate system logs. The second one contains diagnostic data with totals of how many records did it try to generated or if there are any errors

IF you do not see buttons OR input fields – please check if your profile has an access to all fields on objects from data model

### Execute all Service Plans via Code

Another powerful snippet could be if you need it to run for a lot of Service Plans. IF there are lot of WOLI to be generated and also some automations on those – it could be that there will be a limits hit.  
Snippet below will run it with lower batch size (10)

```
FIELDBUDDY.cls_ServicePlanSchedulerExecutor preventiveMaintenanceJob = new FIELDBUDDY.cls_ServicePlanSchedulerExecutor();
Database.executeBatch(preventiveMaintenanceJob, 10);
```

If you want to only run it for specified Service Plans - you can use the snippet below (for `ServicePlanId1`, `ServicePlanId2`, `ServicePlanId3` service plans)  


```
FIELDBUDDY.cls_ServicePlanSchedulerExecutor job = 
    new FIELDBUDDY.cls_ServicePlanSchedulerExecutor(
      Date.today(), 
      true, 
      new List<String>{'ServicePlanId1','ServicePlanId3','ServicePlanId3'}
  );
Database.executeBatch(job, 10);
```

### Extra Options

As mentioned in screenshots below, there are few extra options which can be handy during configuration and testing of the Preventive Maintenance module

- **Insert Data** – checkbox, checked by default. If not checked – data (SR, WO, WOLI) will not be inserted, as well as Covered Until dates will not be updated. Only totals of records to be generated will be saved in Service Plan Executions and appropriate System Logs
- **Target Date** – date, today by default. Allows to adjust “todays” date in order in to testing as which work orders will be generated in case if I run it in 2 weeks, or Jan 1 of 2030.

## How to schedule automatic Service Plan executions

Preventive Maintenance can be Scheduled via Configuration Panel → Preventive Maintenance

It is possible to see if Job is currently Scheduled, and Start/Stop it respectively. By default it is scheduled to be run at 2 AM every night UTC time.

![image-20250925-130752.png](media://0c022ba1-5e73-4361-9a2f-b62de82249c9)

If you need a greater flexibility Preventive Maintenance can be scheduled manually via **Schedule Apex** button: Setup → Custom Code → Apex Classes → Schedule Apex  


![image](media://5ec4f730-61f7-4d87-a526-901f28458682)

Then you pick class **cls_ServicePlanSchedulerExecutor, **adjust Frequency and Preferred Start Time and click Save.

![image](media://be73497a-4481-4cab-bd37-71c4cdf8bdf6)

In order to stop or change it – please check list of Scheduled Jobs and remove/update the one scheduled previously.

In case if even greater flexibility is needed – It is possible by executing following snippet – to schedule it daily at 4 AM:  


```
String cron = '0 0 4 * * ? *';

System.schedule('FB:Preventive Maintenance',
                cron,
                new FIELDBUDDY.cls_ServicePlanSchedulerExecutor()
               );
```



## How to debug a Service Plan executions

It can also occur that there are errors during Preventive Maintenance  – for example, some validation rules or Flows prevent work orders to be saved. Therefore below are the steps to check in case if you see any issues:

1. Check the Service Plan
  1. It should be Active (Active checkbox is Checked)
  2. It  should have Service Plan Coverages and Service Plan Items
  3. There should be Start date, and Service Plan Coverages must have a Covered Until date specified
2. Check for Service Plan Executions. Usually on every Service Plan with every execution there is a Service Plan Execution record created with Diagnostic Data such as – how many Work Orders did it try to generate and if there are any errors
3. Check for System Log. Every time when Preventive Maintenance is executed, there are 2 system logs created with Source equal to `cls_PreventiveMaintenanceExecutor` and Type equal to `SF:PreventiveMaintenance`, and Action equal to `ServicePlanSchedulerEnd`. Those contains generic information about all Service Plans scheduled in terms of amount of generated/inserted Work Orders and other records, as well as if there are any issues
4. If there are no 2 system logs, but only one (with action `ServicePlanSchedulerStart`) - it means job fails on level that error can not be handled with standard exception handling in the platform (for example, limits issue or so). Therefore please check the Apex Jobs if there are any failures for class `cls_ServicePlanSchedulerExecutor`


## Example Service Plan

An example service plan:

![image](media://26581e7f-fd65-497a-98ef-bac5f878bfc6)

In this example: 

- the Service Plan will never expire (since end date is empty)
- the frequency is one year, so Work Orders will be generated once every year
- The Work Orders will be created two months before the day that they should be scheduled
- And the period in which the Work Orders should be scheduled is a 1 month from that day

This is a common example that if a customer has a contract that states that every year the Installed Product requires to be maintained. And that the company has a month to do the work and the planner has two months to schedule the Work Order.