# What are the best practices for Apex triggers on Salesforce Field Service objects (Service Appointment, Work Order) so they do not break scheduling or mobile sync?

**Topic:** Field Service · **Difficulty:** Medium · **Asked at:** Accenture, Deloitte Digital, PwC · **Tags:** Field Service, Apex Triggers, Best Practices

Read it on the site: https://www.forcechallenges.com/interview-questions/what-are-the-best-practices-when-writing-apex-triggers-that-interact-with-salesforce-field-service-objects-or

## Answer

Triggers on Field Service objects, such as Service Appointment, Work Order and Assigned Resource, run alongside the scheduling engine and mobile sync. Poorly written triggers cause slow scheduling, sync errors and recursion.

**Best practices**
1. **Bulkify**: handle lists, never one record at a time, and keep queries and DML outside loops.
2. **One trigger per object**, with logic in a handler class.
3. **Guard against recursion**, because the engine and automation may update the same record again.
4. **Do not call the scheduling engine synchronously** in a trigger. Use Queueable Apex with callouts.
5. **React only to real changes**, such as a status change, by comparing `Trigger.new` with `Trigger.oldMap`.
6. **Keep triggers light**, so offline uploads from mobile are not rejected or slowed.
7. **Do not overwrite fields** the package manages (such as scheduled times) unless you must.
8. Write tests with realistic bulk data (200+ records).

```apex
trigger ServiceAppointmentTrigger on ServiceAppointment (after update) {
    if (TriggerGuard.hasRun) { return; }
    TriggerGuard.hasRun = true;
    SAHandler.onAfterUpdate(Trigger.new, Trigger.oldMap);
}
```

```apex
public class TriggerGuard {
    public static Boolean hasRun = false;
}
```

```apex
public with sharing class SAHandler {
    public static void onAfterUpdate(List<ServiceAppointment> newList, Map<Id, ServiceAppointment> oldMap) {
        List<Id> completedIds = new List<Id>();
        for (ServiceAppointment sa : newList) {
            if (sa.Status != oldMap.get(sa.Id).Status && sa.Status == 'Completed') {
                completedIds.add(sa.Id);
            }
        }
        if (!completedIds.isEmpty()) {
            System.enqueueJob(new CompletionProcessor(completedIds));
        }
    }
}
```
`CompletionProcessor` is a Queueable class that does the heavy work (callouts, follow-up records) outside the trigger.

**Interview tip:** emphasize *bulk, async and recursion-safe*.

---
Source: ForceChallenges · last updated 2026-10-31
