Program touch-tone instructions for my fingers

I use SObject as a key when I want to use a composite key. For example, say I want to find duplicates based on first name, last name, and email. I might loop through a list of contacts, and create a contact that matches the keys I'm using:

Incoming.put(new Contact(FirstName= record.FirstName, LastName= record.LastName, Email= record.Email), record);

Then, when I query the database on those three fields, I can quickly look up the new records, again by constructing a key from an SObject.

This is more reliable than concatenating strings and hoping that it is unique enough that it won't grab false positives.


Example:

trigger matchDupes on Lead (before insert, before update) {
    Map<Lead, Lead> leads = new Map<Lead, Lead>();
    Set<String> company = new Set<String>(), email = new Set<String>();

    // Match duplicates in this transaction
    for(Lead record: Trigger.new) {
        Lead key = new Lead(Company=record.Company, Email=record.Email);
        if(leads.containsKey(key)) {
            record.addError('This lead matches a duplicate lead.');
            continue;
        }
        company.add(record.company);
        email.add(record.email);
        leads.put(key, record);
    }
    // Match duplicates in the database
    for(Lead record: [SELECT Email, Company from Lead WHERE Company in :company and Email IN :email]) {
        Lead key = new Lead(Company=record.Company, Email=record.Email),
             dupLead = leads.remove(key);
        if(dupLead != null) {
           dupLead.addError('This lead matches a duplicate lead.');
        }
    }
}

Note that I could use an arbitrary number of fields (in most cases). There's several considerations, such as case sensitivity, not included here. I'd recommend always trying to convert to a common case when putting into/out of the map, but generally speaking this technique works well.


Sfdcfox explains a great reason to use this, but at a more abstract level the reason it's supported it because maps can have any type as a key.

In a model where custom classes, or any other type, can be used as keys there seems little reason to make a special case for forbidding SObjects.

Using an apex class as a map key has equally odd behavior (without implementing the equals and hashCode methods at least), so by this logic making SObject special seems inexplicable.


In principle, there are two possible approaches. One is based on the Hasse derivatives (also called hyperdifferentiations). See http://math.fontein.de/2009/08/12/the-hasse-derivative/ for elementary definitions and properties, and the paper

P. Vojta, Jets via Hasse-Schmidt derivations, ArXiv: math/0407113,

for the use of Hasse derivatives in algebraic geometry.

On the analysis level, there are also the Carlitz derivatives, special difference operators working efficiently just on functions annihilated by usual derivatives, with a rich theory of "differential equations", Fourier series, special functions etc. See

A. N. Kochubei, Analysis in Positive Characteristic, Cambridge University Press, 2009.

However there is no geometry around this approach so far.

Tags:

Code Golf