22
Fun with Generic SObjects Evan Kennedy Senior Salesforce Developer/Technical Architect [email protected] Writing code to work against any Salesforce object.

Writing Code to Work Against any Salesforce Object

Embed Size (px)

Citation preview

Page 1: Writing Code to Work Against any Salesforce Object

Fun with Generic SObjects

 Evan Kennedy  Senior Salesforce Developer/Technical Architect  [email protected]

Writing code to work against any Salesforce object.

Page 2: Writing Code to Work Against any Salesforce Object

Evan Kennedy Senior Salesforce Developer/Technical Architect

Page 3: Writing Code to Work Against any Salesforce Object

About Modacto  Implementation and optimization of the Salesforce platform for growth-minded organizations.

•  Improvements

•  Meaningful business value

•  Effective support of Salesforce ecosystem

•  Cleansing, migration, integration

•  Reporting and dashboards

•  Lightning connect

•  Wave

 Ready to go Modacto?

 800 Washington Ave N Suite 907 Minneapolis, MN 55401

 612.200.0846 [email protected]

•  Going beyond configuration

•  Customized app builds

•  Comfortable adoption

Page 4: Writing Code to Work Against any Salesforce Object

Overview of Topic  We all like to write re-usable code, one of the ways to do this is to use Generic SObjects in our Apex classes.

  Some code examples •  Schema Describe Code

•  Multi-Object Trigger Handlers

•  Apex REST endpoint for generic SObjects

•  Crud Enforcement

  A couple of real-world case studies

  Pros and common pitfalls of generic SObjects

Page 5: Writing Code to Work Against any Salesforce Object

Code Examples Let’s just jump right in, shall we?

Page 6: Writing Code to Work Against any Salesforce Object

Example 1 Using the Schema Describe Code, Dynamic SOQL

Page 7: Writing Code to Work Against any Salesforce Object

Schema Describe Code and Dynamic SOQL  This example will cover: •  Using the Schema describe code to get object and field information in Apex.

•  Using this information to construct dynamic SOQL queries.

Page 8: Writing Code to Work Against any Salesforce Object

Example 2 Multi-Object Trigger Handlers

Page 9: Writing Code to Work Against any Salesforce Object

 In this example we’re going to look at:

•  Writing simple trigger handler code to handle multiple types of objects

•  Retrieving field data from a generic SObject.

Multi-Object Trigger Handlers

Page 10: Writing Code to Work Against any Salesforce Object

Example 3 Apex REST Endpoint for Lists of Generic SObjects

Page 11: Writing Code to Work Against any Salesforce Object

 Useful when needing to implement additional endpoint logic across many objects.

 Good for Salesforce to Salesforce types of custom integrations.

Apex REST Endpoint for Generic SObjects

Page 12: Writing Code to Work Against any Salesforce Object

Example 4 Small examples: Crud Enforcement and Object Sorting

Page 13: Writing Code to Work Against any Salesforce Object

 Required for AppExchange security review

 Good way to enforce profile/permission set object and field-level security.

 Existing Package for this: https://github.com/ZergyPoo/SFDCCrudEnforcementUtils

Crud Enforcement

Page 14: Writing Code to Work Against any Salesforce Object

 Custom SObject Sorting where SOQL falls short.

 Good for fields that contain numbers and text.

SObject Sorting

Page 15: Writing Code to Work Against any Salesforce Object

Real-World Case Studies PromotionBuilder and Solomon365

Page 16: Writing Code to Work Against any Salesforce Object

 PromotionBuilder

•  Gamification app

•  Allows users to setup custom criteria against any of their objects

•  Runs the same trigger code against the objects to check for gamification criteria.

 Solomon365

•  Hub and Spoke model

•  Data syncing back from the spokes to the hub organization

•  Needed custom Apex REST endpoints to handle multiple types of objects with complex logic without having to duplicate code.

Cool Real-World Examples

Page 17: Writing Code to Work Against any Salesforce Object

The Good and the Bad Pros and Common Pitfalls of using Generic SObjects

Page 18: Writing Code to Work Against any Salesforce Object

 Pros – What is great about using generic SObjects

•  Lots of re-usable code

•  Covering common functionality without have to copy and paste all the time

•  Great for apps, lets your clients run your code against their own custom objects

•  Dynamic SOQL

 Cons – The ugly

•  Much more prone to run-time error

•  Can be more confusing to read the code

•  Object instantiation can get a little wonky

Pros and Cons

Page 19: Writing Code to Work Against any Salesforce Object

It’s a Wrap!

Page 20: Writing Code to Work Against any Salesforce Object

 Pros and Cons of using generic SObjects in Apex code.

 Some awesome code examples

 Some Real-world applications

What did we cover? Wrap-Up

Page 21: Writing Code to Work Against any Salesforce Object

Any Questions?

 Feel free to ask me anything (within reason)

Page 22: Writing Code to Work Against any Salesforce Object

Thank you