0

My plan is to create integration in which i need to be able to receive webhooks from salesforce. I already have connected app and access to salesforce API.

I am familiar with the steps i need to do before i can recieve webhooks (I already did this using UI). From my understanding the flow is:

  1. Add remote site setting: to allow sending hooks to external site
  2. Add apex class definition: This class should have method for creating and sending request.
  3. Add trigger for SObject: in this trigger you specify events, when the trigger should be called and then you import and use class from step 2. to send this data as a request.

How i can do this using simple-salesforce package or using requests ?

This is class i am struggling (cant find how to) to upload to salesforce

public class Webhook implements HttpCalloutMock {


    public static HttpRequest request;
    public static HttpResponse response;

    public HTTPResponse respond(HTTPRequest req) {
        request = req;
        response = new HttpResponse();
        response.setStatusCode(200);
        return response;
    }

    public static String jsonContent(List<Object> triggerNew, List<Object> triggerOld) {
        String newObjects = '[]';
        if (triggerNew != null) {
            newObjects = JSON.serialize(triggerNew);
        }

        String oldObjects = '[]';
        if (triggerOld != null) {
            oldObjects = JSON.serialize(triggerOld);
        }

        String userId = JSON.serialize(UserInfo.getUserId());

        String content = '{"new": ' + newObjects + ', "old": ' + oldObjects + ', "userId": ' + userId + '}';
        return content;
    }

    @future(callout=true)
    public static void callout(String url, String content) {

        if (Test.isRunningTest()) {
            Test.setMock(HttpCalloutMock.class, new Webhook());
        }

        Http h = new Http();

        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/json');
        req.setBody(content);

        h.send(req);
    }

}
4
  • Why do you want to create a class instead of creating a package and installing such package in the org? Does the class need to be dynamic in some way? Commented Oct 19, 2023 at 14:24
  • no it does not need to be dynamic. Could the package be installed via API ? because the main idea is to integrate Salesforce sobject rows (Accounts, Leads, ....) with our system. And i want to receive newly created, updated, or deleted items via webhooks and then apply this change in our system so items are in sync in both systems. Commented Oct 19, 2023 at 15:14
  • i found a project which basically does what i need to do, but im not very proficient in scala so reverse engeneering specific REST calls would take so much time and it is basically 3 calls that i need to know how to build. This is project: github.com/jamesward/salesforce-webhook-creator Commented Oct 19, 2023 at 15:26
  • If you create a package with your class on it, you can just install the package through the API, yes. But for your specific requirement you don't need Apex, I think. You might wanna take a look at the Change Data Capture documentation instead of building this from scratch: developer.salesforce.com/docs/… Commented Oct 19, 2023 at 16:38

1 Answer 1

1

REST

You can create Apex Class through REST API. This is an example in Apex, but you can rewrite it on python. Basically, you need you instance_url and access_token from authorization response. Replace RestUtils.getSalesforceDomainURL() with instance_url and UserInfo.getSessionId() with access_token. That will do it.

HttpRequest req = new HttpRequest();
req.setEndpoint(RestUtils.getSalesforceDomainURL() + '/services/data/v59.0/sobjects/ApexClass');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());

String json = '{"Name": "YourClassName", "Body": "';
json += 'your class hoes here';
json += '"}';

req.setBody(json);
Http http = new Http();
HttpResponse res = http.send(req);

SOAP

To upload class to your organization, you need to use Metadata API. For this:

Here is an example of Apex Class Metadata:

https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_classes.htm

3
  • provided example throws 503 response code. Commented Oct 19, 2023 at 15:33
  • but isnt Tooling API what i need to use ? specifically POST /tooling/sobjects/ApexClass ? Commented Oct 19, 2023 at 15:57
  • You are right, it is also possible with REST API (not tooling api, but I guess that's also an option), will update the answer. @JanBalucha Commented Oct 19, 2023 at 16:23

You must log in to answer this question.

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