19
Publishing Data to REST APIs with Lightning Process Builder Scott Coleman Principal Solutions Architect, Cloud Giants [email protected] @scottsalesforce [email protected] @KenneyAlexandra Alex Kenney Solutions Consultant, Cloud Giants

Publishing Data to REST APIs with Lightning Process Builder

Embed Size (px)

Citation preview

Page 1: Publishing Data to REST APIs with Lightning Process Builder

Publishing Data to REST APIs with Lightning Process Builder

Scott ColemanPrincipal Solutions Architect, Cloud Giants

[email protected]@scottsalesforce

[email protected]@KenneyAlexandra

Alex KenneySolutions Consultant, Cloud Giants

Page 2: Publishing Data to REST APIs with Lightning Process Builder

Welcome to Dreamforce

Why implement simple data integrations using Process Builder and Invocable Apex?• Maximum use of platform features, filling gaps with code when necessary• Using clicks instead of code increases maintainability and reusability

Key takeaways• REST web service basics• A mostly clicks approach to simple integration• A process and some code that you can use right away!• How to decide if this approach is for you - this isn't the only way (and isn't always the best way)• Ideas about extending the solution to meet your specific needs

Thanks for joining us!

Page 3: Publishing Data to REST APIs with Lightning Process Builder

REST Web Services and APIs

What is an API?• Application Programming Interface• Defines the interface between one component and second component that provides services for it

What is REST?• Representational State Transfer• An architectural style that can be applied to APIs (most commonly on the web); a set of conventions or

rules• Emphasis on lightweight payloads and predictable behaviors

REST API examples• Slack webhooks• Google Maps Geocoding API• Salesforce REST API

A (Very) High Level Introduction

Page 4: Publishing Data to REST APIs with Lightning Process Builder

Calling Apex from Process Builder

Process Builder - have you used it?

You can call Apex methods from your process!• Powerful way to create mostly-declarative solutions• A chance for collaboration between admins and devs

• Use the "Apex" action type in Process Builder, select the method to call, and specify the values to pass into the method

There are rules for your Apex class and method. It must:• Method should be annotated with @InvocableMethod

• Only accept one List as its input parameter (though it can be custom type - use @InvocableVariable on members of user-defined types)

• Be static and public/global

The Basics

Page 5: Publishing Data to REST APIs with Lightning Process Builder

Slack IntegrationDemo

Slack is a popular communication tool used by companies, project teams, other organizations

Possible use case: Notify your team when a hot lead lands in Salesforce

Let's run through a quick demo of this simple Salesforce to Slack integration

Page 6: Publishing Data to REST APIs with Lightning Process Builder

Slack Integration

Slack configuration• Define a webhook and specify which channel messages should appear in• Take note of the webhook address

Responsibilities of our Process Builder process• Know when to send a notification (criteria)• Specify what data should be included in the notification• Specify the address of the Slack webhook to which the notification should be sent

Configuration

Page 7: Publishing Data to REST APIs with Lightning Process Builder

Slack IntegrationProcess Builder

Let's walk through our process!

Page 8: Publishing Data to REST APIs with Lightning Process Builder

Slack Integration

Not coupled to any particular object or webhook address• Labels, values, and address passed in by process• Same class can be used with any object that supports Process Builder• Make sure to create a Remote Site in Salesforce for https://hooks.slack.com

Very simple implementation with basic capabilities• Data structure is flat - not hierarchical• Number of fields you can include in notification is limited• Optional record link can be included so user can get more details• Works great for most quick notifications

Apex Class

Page 9: Publishing Data to REST APIs with Lightning Process Builder

Slack IntegrationApex Class

Let's dive into the code!

Page 10: Publishing Data to REST APIs with Lightning Process Builder

Extending the Solution

Is this pattern right for you?• Great for productivity features - not the best solution for reliable messaging, guaranteed delivery, business

critical scenarios

Payload format• Slack's webhook uses a JSON (JavaScript Object Notation) payload, but your REST endpoint may use a

different format• We're using a very flat data structure - maybe your target requires a hierarchical structure

Authentication• Slack's webhook REST endpoint doesn't implement any authentication, but your target endpoint likely does• Consider using Named Credentials (Spring '15 feature) to make your code simpler (more on this later)

Let us know how you extend the solution (or let us know if we can help!)• Session Chatter feed, Twitter, Email

Meet your specific needs

Page 11: Publishing Data to REST APIs with Lightning Process Builder

Salesforce-to-Salesforce IntegrationDemo

Possible use case: Publish new accounts from one org to another

Let's run through a quick demo of this basic Salesforce to Salesforce integration

Page 12: Publishing Data to REST APIs with Lightning Process Builder

Named Credentials

What are they?• A configuration item that allows you to define the credentials that should be used when making a callout to

a particular external service, either per user or for all users

Why are they useful?• Simplifies your Apex code by managing non-trivial authentication flows for you, like OAuth 2.0• Keeps the endpoint address in one place, outside of your process and Apex code

What else is needed to make this work in our use case?• A Connected App defined in the target org to grant access• An Auth Provider defined in the source org to be used by the Named Credential for authentication in the

target org

An Introduction

Page 13: Publishing Data to REST APIs with Lightning Process Builder

Salesforce-to-Salesforce Integration

Responsibilities of our Process Builder process• Similar to our first use case• Know when to send a notification (criteria)• Specify what data should be included in the notification• Specify the Salesforce API endpoint to which the notification should be sent (and the credentials to be used)

• Used Named Credentials for this

Configuration

Page 14: Publishing Data to REST APIs with Lightning Process Builder

Salesforce-to-Salesforce IntegrationProcess Builder

Let's walk through our process!

Page 15: Publishing Data to REST APIs with Lightning Process Builder

Salesforce-to-Salesforce Integration

Not coupled to any particular object or org• Labels, values, named credential passed in by process• Same class can be used with any object that supports Process Builder

Not appropriate in scenarios where data is business critical and we need guaranteed delivery• This approach is best for informational integrations and data that isn't business critical (productivity)

Apex Class

Page 16: Publishing Data to REST APIs with Lightning Process Builder

Salesforce-to-Salesforce IntegrationApex Class

Let's dive into the code and related authentication config!

Page 17: Publishing Data to REST APIs with Lightning Process Builder

ResourcesGet the Process, Code, and Step-by-Step Instructions• Available at GitHub: https://github.com/scott-cloudgiants/df16-procbuilder-restapi

Salesforce Documentation• Lightning Process Builder

• https://help.salesforce.com/HTViewHelpDoc?id=process_overview.htm

• InvocableMethod Annotation• https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm

• Apex Web Services and Callouts• https://developer.salesforce.com/page/Apex_Web_Services_and_Callouts

• Named Credentials as Callout Endpoints• https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_named_credentials.htm

Blog Posts• Slack and Salesforce Integration by Christophe Coenraets - the inspiration for our approach!

• http://coenraets.org/blog/2016/01/slack-salesforce-integration/

• Salesforce to Salesforce integration using Named Credentials in 5 lines by Jitendra Zaa - more inspiration!• http://www.jitendrazaa.com/blog/salesforce/salesforce-to-salesforce-integration-using-named-credentials-in-just-5-lines-of-code/

Page 18: Publishing Data to REST APIs with Lightning Process Builder

Q&AAsk questions!

Page 19: Publishing Data to REST APIs with Lightning Process Builder

Thank Y uScott ColemanPrincipal Solutions Architect, Cloud Giants

[email protected]@scottsalesforce

Alex KenneySolutions Consultant, Cloud Giants

[email protected]@KenneyAlexandra