I need to write a sample rest class where i have custom button on account object.and When i click on the the button it takes a values from account and perform get method with external site and update the account record with the returned value
1 Answer
There are lots of unknowns in your question so general guidance can be offered. The general approach may be:
- Query the account to get the data values needed.
- Make the http callout
- Deserialise the response
- Update the account with DML
For the callout principles I would use this resource as a starting point : https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http.htm
For reference the sample class is shown below, you would need to build out the url as required to add on your parameters, assuming the parameters are to be passed in the url string, your url may look like: https://www.example.com/myservice?param1=abc¶m2=xyz
public class HttpCalloutSample {
// Pass in the endpoint to be used using the string url
public String getCalloutResponseContents(String url) {
// Instantiate a new http object
Http h = new Http();
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');
// Send the request, and return a response
HttpResponse res = h.send(req);
return res.getBody();
}
}
This method returns the request body, depending how your service response you would need to handle this data, likely it will be as a json response. Depending on the complexity and format of the data returned you will need to deserialise in some way, this resource is a good starting point: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_json_json.htm
On the whole, it is best if you attempt something and they ask specific questions for specific answers to the issues that you meet.