Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What's new in ApexLib?

What's new in ApexLib?

Chris Peterson

June 04, 2014
Tweet

More Decks by Chris Peterson

Other Decks in Programming

Transcript

  1. What’s new in ApexLib
    Chris “@ca_peterson” Peterson
    June 4th, 2014

    View Slide

  2. How do we query?
    It all starts off so simple:
    Contact notBob =
    [SELECT Name, Id FROM Contact WHERE Name != ‘Bob’ LIMIT 1];

    View Slide

  3. How do we query?
    But it can get ugly fast:
    //ewww
    String soql = ‘SELECT ‘;
    for(Integer i = 0; i< fieldList.size(); i++){
    soql += fieldList + ‘, ‘;
    }
    soql = soql.substring(0,soql.length()-2);
    soql += conditions != null && conditions.trim().size() > 0 ? ‘ WHERE ‘ +
    soqlConditions : ‘’;
    soql += limit != null && limit.trim().size() > 0 ? ‘ LIMIT ‘+limit;
    List nonBobs = Database.query(soql);

    View Slide

  4. Introducing QueryFactory
    ● Object-oriented. No string building.
    ● Enables flexible selector layers.
    ● Idempotent operations (where sane).
    ● Field-set friendly.
    ● Package-friendly (implies namespaces)

    View Slide

  5. What’s so bad about string building?
    ● Did you see that earlier slide?
    ● Fails late, well after when errors were introduced.
    ● Stream-like: you can’t (easily) amend a previous clause.

    View Slide

  6. Why not apex-commons SOQLBuilder?
    Initially planned to, but…
    ● Massive, verbose.
    ● No updates in quite a while.
    ● Not idempotent, reduces encapsulation.

    View Slide

  7. Results in:
    SELECT Name FROM Contact Limit 5
    In it’s simplest form:
    new fflib_QueryFactory(Contact.SObjectType)
    .selectField(‘name’)
    .setLimit(5)
    .toSOQL();
    So how do I use it?

    View Slide

  8. fflib_QueryFactory qf = new fflib_QueryFactory(Contact.SObjectType)
    .selectField(Contact.SObjectType.fields.name)
    .selectField(‘name’) //ignored: name already selected
    .selectFieldSet(
    Schema.SObjectType.Contact.fieldSets.MyFieldSet
    )
    .setLimit(5);
    qf.setCondition(‘Name != “Bob”’);
    qf.setLimit(10); //overrides the earlier limit
    Results in:
    SELECT Name, FROM Contact WHERE Name != “Bob”
    LIMIT 10
    With a field set

    View Slide

  9. When should I use it?
    ● Field sets.
    ● Dynamic queries.
    ● Extensible selector classes.

    View Slide

  10. When should I go another route?
    ● When queries are static.
    ● When performance is more critical than code quality.
    ● When you need aggregations.
    ● Weird SOQL edge cases, like toLabel().

    View Slide

  11. Future Plans
    ● Bind variable support
    ● Object and field read permission checks
    ● Better cross-object field selects
    ● Support for aggregations
    ● Object-oriented where clauses
    (#safeharbor)

    View Slide

  12. Questions?
    Twitter: @ca_peterson
    irc.freenode.net #salesforce: ca_peterson
    Code on Github: https://github.com/financialforcedev/fflib-
    apex-common
    Slides available at https://speakerdeck.com/ca_peterson

    View Slide