About this question
I am trying to set up my first apex trigger/class but I'm having trouble interpreting and applying the docs on my use case.
Use Case: I'm trying to invoke a rest service through an apex class whenever an apex trigger is launched. This apex trigger should launch when an opportunity records' stage is updated to a certain value. This apex class will post the opportunity record id to an ESB consumer.
This is how I thought this would work this:
Create an apex trigger This should only trigger when a certain stage is met on the opportunity record. For this step I'm a bit puzzled on how I could add the if condition to check on the opportunity stage.
trigger TriggerOpportunity on Opportunity (after update) { OpportunityClass.PushOpportunity(); }
Create an apex class Here i'm a bit stuck. Essentially in here I would like to be able to:
1. specify which record id triggered the apex class
2. Do authentication if necessary?
3. Add a body which is filled with the json values of the record that triggered the apex or just send the record id in the body so the id can be used to retrieve the record through another call
4. Add a post method which posts the json body to a specific endpoint url
Best I could come up with was something like this:
public class OpportunityClass {
@future(callout=true)
public static void PushOpportunity() {
HttpRequest request = new HttpRequest();
// Set the endpoint URL.
String endpoint = 'http://yourHost/yourService';
request.setEndPoint(endpoint);
// Set the HTTP verb to GET.
request.setMethod('GET');
// Send the HTTP request and get the response.
HttpResponse response = new HTTP().send(request);
}
}
I am not familiar with apex syntax. I was wondering if this is the correct approach and how I would be able to include the record id which triggered the apex class in a body to send to my endpoint url. Also was wondering if it was needed to add some kind of authentication to this apex class?