-
Notifications
You must be signed in to change notification settings - Fork 35
/
REST_AccountService_V5.cls
130 lines (111 loc) · 4.66 KB
/
REST_AccountService_V5.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/****************************************************************************************************
* Description - Sample Apex REST service with GET and POST methods
* Author - Matthew Lamb
* Example request:
*
{ "acct" :
{
"Name" : "Test Company 1",
"AccountNumber" : "123",
"Website" : "www.salesforce.com"
}
}
*
****************************************************************************************************/
@RestResource(urlMapping='/v5/accounts/*')
global with sharing class REST_AccountService_V5 {
@HttpPost
global static AccountWrapper doPost(Account acct) {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
AccountWrapper response = new AccountWrapper();
try {
insert acct;
response.acctList.add(acct);
response.status = 'Success';
response.message = 'Your Account was created successfully';
}
catch(Exception exc) {
res.StatusCode = 500;
response.acctList = null;
response.status = 'Error';
response.message = 'Your request failed with the following error: ' + exc.getMessage();
}
return response;
}
@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>();
}
}
}