Code Monkey home page Code Monkey logo

query.apex's Introduction

Query.apex

Query.apex provides a flexible and dynamic way of building a SOQL query on the Salesforce platform.

Features

  • Allows both functional programming and object oriented programming paradigm

  • Manages object fields and provides Field Level Security checking

  • Supports complex queries including parent/child relationships, and nested conditions

Examples

Get all accounts

This will return a list of all Accounts from the database.

By default it will select all the fields in Account object which the user has the access privilege on it.


List<Account> accounts = new Query('Account').run();

Get an account based on its Id

This will query the Accounts with a specific Id, and return only one SObject as a result.


Account account =
    (Account)new Query('Account').
    byIds('001O000001HMWZVIA5').
    fetch(0);

Get an account based on a foreign key

This will query the Contacts given the AccountId foreign key


List<Contact> contacts =
    new Query('Contact').
    lookupIds('AccountId',
    '001O000001HMWZVIA5').run();

Select parent fields

This will select all the fields from the parent object Account.


List<Contact> contacts = new Query('Contact').selectParentFields('Account').run();

Query with simple conditions

This will query all the accounts whose 'FirstName' is 'Sam' and 'LastName' is 'Tarly'.

By default, all the conditions are joined by 'AND' operator.


List<Account> accounts =
    new Query('Account').
    addConditionEq('FirstName', 'Sam').
    addConditionEq('LastName', 'Tarly').
    run();

Query with complex conditions

For more complicated conditions, we can use the method 'conditionXX' to create a condition variable, and join these conditions with 'doOr' method or 'doAnd' method to join these condition variables.


List<Account> accounts =
    new Query('Account').
    addCondition(
        Query.doAnd(
            Query.doOr(
                Query.conditionEq('FirstName', 'Sam'),
                Query.conditionEq('Id', '001O000001HMWZVIA5')
            ),
            Query.conditionLe('NumberOfEmployees', 15)
        )
    ).
    run();

query.apex's People

Contributors

henryrlee avatar herakles86 avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.