-
Notifications
You must be signed in to change notification settings - Fork 35
/
REST_AccountService_V3.cls
98 lines (82 loc) · 3.77 KB
/
REST_AccountService_V3.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/****************************************************************************************************
* Description - Sample Apex REST service with GET method
* Author - Matthew Lamb
****************************************************************************************************/
@RestResource(urlMapping='/v3/accounts/*')
global with sharing class REST_AccountService_V3 {
@HttpGet
global static AccountWrapper doGet() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
AccountWrapper response = new AccountWrapper();
String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
if(doSearch(accountId)) {
searchAccounts(req, res, response);
}
else {
findAccount(res, response, accountId);
}
return response;
}
// If the item to the right of the last forward slash is "accounts", the request went to v3/accounts?Name=United
// Else the request went to v3/accounts/<something>, which is not a search, but a specific entity
private static boolean doSearch(String accountId) {
if(accountId == 'accounts') {
return true;
}
return false;
}
//If the request came to /v3/accounts, then we want to execute a search
private static void searchAccounts(RestRequest req, RestResponse res, AccountWrapper response) {
//Use the RestRequest's params to fetch the Name parameter
String searchTerm = req.params.get('Name');
if(searchTerm == null || searchTerm == '') {
response.status = 'Error';
response.message = 'You must provide a Name for your search term.';
res.StatusCode = 400;
}
else {
String searchText = '%'+searchTerm+'%';
List<Account> searchResults = [SELECT Id, Name, Phone, Website FROM Account WHERE Name LIKE : searchText];
if(searchResults != null && searchResults.size() > 0) {
response.acctList = searchResults;
response.status = 'Success';
response.message = searchResults.size() + ' Accounts were found that matched your search term.';
}
else {
response.status = 'Error';
response.message = 'No Accounts where found based on that Name, please search again.';
}
}
}
//If the request came to v3/accounts/<external_Id>, then we want to find a specific account
private static void findAccount(RestResponse res, AccountWrapper response, String accountId) {
// Provided we recevied an External Id, perform the search and return the results
if(accountId != null && accountId != '') {
List<Account> result = [SELECT Id, Name, Phone, Website FROM Account WHERE External_Id__c =: accountId];
if(result != null && result.size() > 0) {
response.acctList.add(result[0]);
response.status = 'Success';
}
else {
response.status = 'Error';
response.message = 'This account could not be found, please try again.';
res.StatusCode = 404;
}
}
// If the request came to /v3/accounts/ (without an Account Id), return an error
else {
response.status = 'Error';
response.message = 'You must specify an External Id.';
res.StatusCode = 400;
}
}
global class AccountWrapper {
public List<Account> acctList;
public String status;
public String message;
public AccountWrapper(){
acctList = new List<Account>();
}
}
}