The @HttpGet annotation is used at the method level and enables us to expose an Apex method as a REST resource. To use the @HttpGet annotation our apex method must be defined as global static.To create a custom webservice the first step is to create an apex class annoted with @RestResource annotation.
In a class annoted with @RestResource we can create 5 methods each with different annotations like GET,DELETE,POST,PUT,PATCH. Multiple methods cannot be annoted with same notation, for example in a class there can be only one method annoted with GET,DELETE,POST,PUT,PATCH at a time.
Note: Methods annotated with @HttpGet are also called if the HTTP request uses the HEAD request method.
Below is an example of webservice which need id or any unique identifier as a parameter in request and it will return record associated with it.
@RestResource(urlMapping='/getAccountOnExternalIdtofetchsinglerecord/*')
global with sharing class getAccounttoSingleRecord {
@Httpget
global static String fettringchAccount(){
Account obj=new Account();
RestRequest req = RestContext.request;
RestResponse res = Restcontext.response;
string accId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
obj=[Select id , name from Account where id=:accId];
string serializeResponse=JSON.Serialize(obj);
return serializeResponse;
}
}
- The above web service will be accessible at below URL with GET method from external system after successful authentication.
'https://instance.salesforce.com/services/apexrest/getAccountOnExternalIdtofetchsinglerecord/' + externalAccId;
- The method "fetchAccount()" will be automatically called once the request is made at above URL with GET method.
- The externalAccId mentioned in above URL can be any unique parameter using which we can identify the correct record,
In our example we are expecting id of the account record.
- We use the RestRequest class to access incoming request.
For more details visit: What is RestRequest class in salesforce?
- We use the RestResponce class and it is used to pass data from an apex RESTful web service method to an HTTP response.
For more details visit: What is RestResponse class in salesforce?
Now, let us try to this test our webservice from workbench.
/services/apexrest/getAccountOnExternalIdtofetchsinglerecord/0017F00004eD8XzQAK
Click "Execute" button and now click "Show Raw Response".
"{\"attributes\":{\"type\":\"Account\",\"url\":\"/services/data/v55.0/sobjects/Account/0017F00004eD8XzQAK\"},
\"Id\":\"0017F00004eD8XzQAK\",\"Name\":\"Test 105 Account\"}"
As we can see the response we are returning here is of string type, if we want to return JSON format we need to modify the above webservice class as shown below,
@RestResource(urlMapping='/getAccountOnExternalIdtofetchsinglerecord/*')
global with sharing class getAccounttoSingleRecord {
@Httpget
global static Account fettringchAccount(){
Account obj=new Account();
RestRequest req = RestContext.request;
RestResponse res = Restcontext.response;
string accId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
obj=[Select id , name from Account where id=:accId];
// string serializeResponse=JSON.Serialize(obj);
return obj;
}
}
Now, Click "Execute" button and now click "Show Raw Response".
{
"attributes" : {
"type" : "Account",
"url" :
"/services/data/v49.0/sobjects/Account/0017F00004eD8XzQAK"
},
"Id" : "0017F00004eD8XzQAK",
"Name" : "Test 105 Account"
}
No comments:
Post a Comment