Skip to content

[RHI DEMO] salesforce-webhook-ingestor application for RHI Demo

Notifications You must be signed in to change notification settings

sa-ne/rhi-demo-app1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Salesforce Webhook Ingestor

How to build with OpenShift

oc new-build java:openjdk-11-el7~https://gitlab.com/rhi-demo/salesforce-webhook-ingestor.git

optionally, you may use incremental builds to make your future builds faster:

oc patch bc/salesforce-webhook-ingestor -p '{"spec":{ "strategy":{ "sourceStrategy":{ "incremental": true } } }}'

Required environment variables

Variable Example Value Definition

SF_CLIENT_ID

31345254523huiunexabyxvaxajijiaxaex

The Connected App Consumer Key

SF_CLIENT_SECRET

31345254523huiunexabyxvaxajijiaxaex

The Connected App Consumer Secret

SF_USERNAME

user@email.com

user used to log in the Salesforce instance

SF_PASSWORD

passswordToken

Salesforce instance user’s password + security token. If you don’t have a security token you may generate a new one by resetting it.

SF_INSTANCE

https://example-dev-ed.my.salesforce.com

Your Dev instance created

AB_JOLOKIA_OFF

true

Jolokia doesn’t work with Quarkus

Create a secret for Salesforce credentials

oc create secret generic salesforce-secret --from-literal=SF_CLIENT_ID= --from-literal=SF_CLIENT_SECRET= --from-literal=SF_USERNAME= --from-literal=SF_PASSWORD= --from-literal=SF_INSTANCE=

Salesforce Configuration

To work with Salesforce you need to apply some configurations first. Here are the steps needed to connect to Salesforce successfully.

[Optional] Create a Salesforce Developer Account

Create a Connected App

In order to authenticate the application, an application must be created in Salesforce. Go to Setup → App Manager → New Connected App to create the new Application. You must enable oauth settings to have access to the SF_CLIENT_ID and SF_CLIENT_SECRET.

Permit users to self-authorize

After creating the the connected app you need to allow self-authorization for this app. Go to Setup → Apps → Connected Apps → Managed Connected Apps → edit your app. Set the Permitted Users to All user may self-authorize.

Create an Apex Trigger

An Apex Trigger will generate a webhook to the 3scale endpoint every time an Opportunity is altered, but first we need to add an Apex Class to allow us to create webhooks. Go to Setup → Custom Code → Apex Classes and add a new class like this:

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);
    }

}

Now you can create the trigger. Go to Setup → Object Manger → Opportunity → Triggers → New. You may use the following as a template:

Trigger rhi_demo_opportunity on Opportunity (after insert,after update,after delete,after undelete) {

    String url = '$3SCALE_PROJECT_URL?user_key=1234567890';

    String content = Webhook.jsonContent(Trigger.new, Trigger.old);

    Webhook.callout(url, content);

}

The $3SCALE_PROJECT_URL will look like: https://salesforce-webhook-ingestor-product-3scale-apicast-staging.apps.domain/opportunities. Do not forget the /opportunities path.

Add the URL to Remote Site Settings

You need to allow the Apex Trigger to access the 3scale endpoint. To do that access: Setup → Remote Site Settings → New Remote Site.

About

[RHI DEMO] salesforce-webhook-ingestor application for RHI Demo

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published