Newer Version Available
Generate Work Orders on Maintenance Plans with Apex
The Generate Work Orders action on maintenance plans can also be
called using Apex code. The following code sample creates work order records by making an Apex
callout to the generateWorkOrder action REST API resource.
You can use this code sample in several different ways:
- Add it to Apex controller code for an Aura component, and tie it to a custom UI or app functionality
- Use it in an Apex trigger to semi-automate the creation of the work orders (for example, whenever a maintenance plan is created or updated)
- Use it in an Apex REST service to create work orders when called from an external integration service
1String salesforceHost = System.Url.getSalesforceBaseURL().toExternalForm();
2 String url = salesforceHost + '/services/data/v45.0/actions/standard/generateWorkOrders';
3 // Create HTTP request
4 HttpRequest request = new HttpRequest();
5 request.setEndpoint(url);
6 request.setMethod('POST');
7 request.setHeader('Content-Type', 'application/json;charset=UTF-8');
8 request.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
9 // Set the body as a JSON object
10 request.setBody('{"inputs" : [{"recordId" : "1MPR000000000Bu"}]}');
11 Http http = new Http();
12 HttpResponse response = http.send(request);
13 // Parse the JSON response
14 if (response.getStatusCode() != 201) {
15 System.debug('The status code returned was not expected: ' +
16 response.getStatusCode() + ' ' + response.getStatus());
17 } else {
18 System.debug(response.getBody());
19 }