What's the apex solution to invoke Salesforce web service?

220    Asked by ClaudineTippins in Salesforce , Asked on Mar 1, 2023

 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?

Answered by David EDWARDS

After some analysis we went for this solution to invoke Salesforce web service: 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 oppties) {
      for (ESBCallable o : oppties) {
          // Fetch it
          Opportunity opp = [SELECT Name, StageName, Account.name, Owner.name, amount from Opportunity WHERE ID = .opptyID];
          // Prepare JSON
          Map msg = new Map();
          msg.put('tag', 'esb tag here');
          Map payload = new Map();
          Map identifier = new Map();
          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.setEndpoint(url);
          req.setMethod(method);
          req.setBody(body);
          req.setHeader('Authorization', 'Basic');
          Http http = new Http();
          if (!Test.isRunningTest()) {
              HttpResponse res = http.send(req);
              //System.debug(res);
          } else {
              System.debug(body);
          }
      }
  }
This will:
do basic authentication
pass the opportunity record id which triggered the process flow
invoke a post call to the endpoint url containing the opp id.


Your Answer

Interviews

Parent Categories