Git Product home page Git Product logo

hierarchical-search's Introduction

Hierarchical Search

Allows working with an "hierarchical" SObject relationships (Lookup or Master-detail that references the same SObject type. EG: Account.Parent).

Functionality

  • Find the "root" (AKA highest parent)
  • Find ALL children
  • Works for any SObject

Install

git clone [email protected]:ChuckJonas/hierarchical-search.git
sfdx force:source:convert -d dist
sfdx force:mdapi:deploy -d dist -u your-user

Usage

Find the root of a record(s)

RootSearch r = new RootSearch(Account.getSObjectType(), Account.ParentId);
Map<Id, Id> roots = r.search(new Set<Id>{ child.Id });
System.debug('The Root of this account is ' + roots.get(child.Id));

Find the children of a record(s)

DescendantSearch r = new DescendantSearch(Account.getSObjectType(), Account.ParentId);
Map<Id, List<Id>> children = r.search(new Set<Id>{ parent.Id });
System.debug('This Account has ' + children.get(parent.Id).size() + ' children');

"Ultimate Parent"

A common use case for this type of thing is to create an "Ultimate Parent Lookup" on an Account:

  • Realtime (trigger) (TODO: Add Example)
  • near-realtime (trigger + @future) (TODO: Add Example)
  • Scheduled Batch Job

Depending on your requirements, you might use a combination of these approaches.

Performance

This code has been optimized to limit the number of SOQL queries and is capable of traversing "deep" trees. It for a depth of N, it should return in CEIL(N/6) SOQL queries.

License

MIT

hierarchical-search's People

Contributors

chuckjonas avatar nchursin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.