I am trying to set up my first apex trigger/class but i'm having trouble interpreting and applying the docs on my usecase.

Usecase: 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 puzzeled 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 a specific endpoint url

Best I could come up with was something like this:

public class OpportunityClass {
    public static void PushOpportunity() {
        HttpRequest request = new HttpRequest();
        // Set the endpoint URL.
        String endpoint = 'http://yourHost/yourService';
        // Set the HTTP verb to 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 its needed to add some kind of authentication to this apex class?

Any advice, better solutions approach or ideas are appreciated.


  • 1
    the documentation includes "verbatim" samples on how to invoke a rest endpoint. what exactly is not working? where are you stuck?
    – glls
    Commented Mar 17, 2022 at 15:44
  • 4
    Building an integration to an ESB is not an appropriate project for a new engineer that is not familiar with Apex. I don't mean that as a slight to you or your skills! Doing this right requires close familiarity with how Apex works, including Async Apex, as well as authentication, governor limits, and other topics. Attempting it without that knowledge puts your systems at genuine risk. Your leadership is not serving you well if they've asked you to complete this task without adequate preparation.
    – David Reed
    Commented Mar 17, 2022 at 15:47
  • 5
    If you want to gain the required background, Trailhead modules on Apex, the documentation linked by glls, and Dan Appleman's "Advanced Apex Programming" are all great resources.
    – David Reed
    Commented Mar 17, 2022 at 15:48
  • Thanks for the feedback. No offence taken. Essentially i'm trying to figure out how to build the apex class and trigger on the Salesforce side. I've built integrations with our ESB in other systems but the Salesforce way by using Apex is quite a learning curve. glls resources were indeed helpful. I'll be checking out yours for some inspiration also.
    – ECallpani
    Commented Mar 17, 2022 at 15:55
  • 1
    If you're new to Salesforce, you might not yet know about External Services. It is theoretically possible to do this without Apex.
    – sfdcfox
    Commented Mar 17, 2022 at 16:00

1 Answer 1


After some analysis we went for this solution:

  • Created a process flow which triggers on update of an opportunity record

  • Check if a certain value is met on the opportunity object

  • Trigger apex class

    public with sharing class WS {

      private static final String esbURL = 'endpointurl here';
      public class ESBCallable {
          @InvocableVariable(label='Opportunity ID' required=true)
          public String opptyID;
      @InvocableMethod(label='ESB: Submit Opportunity')
      public static void ESBSFSubmitOpportunity(List<ESBCallable> oppties) {
          for (ESBCallable o : oppties) {
              // Fetch it
              Opportunity opp = [SELECT Name, StageName, Account.name, Owner.name, amount from Opportunity WHERE ID = :o.opptyID];
              // Prepare JSON
              Map<String,Object> msg = new Map<String,Object>();
              msg.put('tag', 'esb tag here');
              Map<String,Object> payload = new Map<String,Object>();
              Map<String,Object> identifier = new Map<String,Object>();
              identifier.put('id', o.opptyID);
              payload.put('identifier', identifier);      
              msg.put('payload', payload);
              String body = JSON.serialize(msg);  
              // Queue the call
              System.enqueueJob(new QueueableESBCall(esbURL + 'api/message/add', 'POST', body));
      public class QueueableESBCall implements System.Queueable, Database.AllowsCallouts {
          private final String url;
          private final String method;
          private final String body;
          public QueueableESBCall(String url, String method, String body) {
              this.url = url;
              this.method = method;
              this.body = body;
          public void execute(System.QueueableContext ctx) {
              HttpRequest req = new HttpRequest();
              req.setHeader('Authorization', 'Basic');
              Http http = new Http();
              if (!Test.isRunningTest()) {
                  HttpResponse res = http.send(req);
              } else {

This will:

  • do basic authentication
  • pass the opportunity record id which triggered the process flow
  • invoke a post call to endpoint url containing the opp id

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .