Fun with Generic SObjects
​ Evan Kennedy
​ Senior Salesforce Developer/Technical Architect
​ evan@modacto.com
Writing code to work against any Salesforce object.
Evan Kennedy
Senior Salesforce Developer/Technical Architect
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
info@modacto.com
•  Going beyond
configuration
•  Customized app
builds
•  Comfortable
adoption
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
Code Examples
Let’s just jump right in, shall we?
Example 1
Using the Schema Describe Code, Dynamic SOQL
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.
Example 2
Multi-Object Trigger Handlers
​ 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
Example 3
Apex REST Endpoint for Lists of Generic SObjects
​ 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
Example 4
Small examples: Crud Enforcement and Object Sorting
​ 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
​ Custom SObject Sorting where SOQL falls short.
​ Good for fields that contain numbers and text.
SObject Sorting
Real-World Case Studies
PromotionBuilder and Solomon365
​ 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
The Good and the Bad
Pros and Common Pitfalls of using Generic SObjects
​ 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
It’s a Wrap!
​ Pros and Cons of using generic SObjects in Apex code.
​ Some awesome code examples
​ Some Real-world applications
What did we cover?
Wrap-Up
Any Questions?
​ Feel free to ask me anything (within reason)
Thank you

Writing Code to Work Against any Salesforce Object

  • 1.
    Fun with GenericSObjects ​ Evan Kennedy ​ Senior Salesforce Developer/Technical Architect ​ evan@modacto.com Writing code to work against any Salesforce object.
  • 2.
    Evan Kennedy Senior SalesforceDeveloper/Technical Architect
  • 3.
    About Modacto ​ Implementation andoptimization 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 info@modacto.com •  Going beyond configuration •  Customized app builds •  Comfortable adoption
  • 4.
    Overview of Topic ​ Weall 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
  • 5.
    Code Examples Let’s justjump right in, shall we?
  • 6.
    Example 1 Using theSchema Describe Code, Dynamic SOQL
  • 7.
    Schema Describe Codeand 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.
  • 8.
  • 9.
    ​ In this examplewe’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
  • 10.
    Example 3 Apex RESTEndpoint for Lists of Generic SObjects
  • 11.
    ​ Useful when needingto implement additional endpoint logic across many objects. ​ Good for Salesforce to Salesforce types of custom integrations. Apex REST Endpoint for Generic SObjects
  • 12.
    Example 4 Small examples:Crud Enforcement and Object Sorting
  • 13.
    ​ Required for AppExchangesecurity review ​ Good way to enforce profile/permission set object and field-level security. ​ Existing Package for this: https://github.com/ZergyPoo/SFDCCrudEnforcementUtils Crud Enforcement
  • 14.
    ​ Custom SObject Sortingwhere SOQL falls short. ​ Good for fields that contain numbers and text. SObject Sorting
  • 15.
  • 16.
    ​ 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
  • 17.
    The Good andthe Bad Pros and Common Pitfalls of using Generic SObjects
  • 18.
    ​ Pros – Whatis 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
  • 19.
  • 20.
    ​ Pros and Consof using generic SObjects in Apex code. ​ Some awesome code examples ​ Some Real-world applications What did we cover? Wrap-Up
  • 21.
    Any Questions? ​ Feel freeto ask me anything (within reason)
  • 22.