[SalesForce] Upsert from a trigger?

I want to update records before insert if there is a match on a lookupkey rather than inserting a new record. I have viewed the related question but do not understand how to get the trigger list to update rather than insert a new record? I appreciate everyone taking the time to review, and help a new coder out.


trigger TINCheck on Account (before insert) {


//A Utility class to process Account records 
public class dupeCheck {

    //the AccountTINTrigger list is getting passed in by our trigger; this should match trigger.new
    public static void checkforDupes(List<Account> itemsFromTrigger){

        //list variable for our query to use as a limit/bind variable
        List<String> namesFromTrigger = new List<String>();
        //populate that list variable
        for(Account acct: itemsFromTrigger){

        //query for existing items, but only if they have the same TIN (WHERE cond)
        List<Account> existingRelationship = ([SELECT id, LLC_BI__lookupKey__c  FROM Account 
            WHERE LLC_BI__lookupKey__c in :namesFromTrigger]);

        List<account> Recordstoupdate = new List <Account>();
        //check to see if there are matches
        if(existingRelationship.size() > 0){
            //this means we have some dupes and we need to identify which records are dupes
            //loop through Accounts

            for (Account currentRelationship: existingRelationship){
                for(Account newRelationship: itemsFromTrigger){
                    if(currentRelationship.LLC_BI__lookupKey__c == newRelationship.LLC_BI__lookupKey__c) {
                        newRelationship.Id = currentRelationship.Id;}
//What else is needed here to update the itemsFromTrigger so that records are upserted rather than inserted?

Best Answer

I agree with the duplicate vote here...but I won't badge hammer just yet. The correct solution here is to call Database.delete(duplicateIds). The steps should be:

  1. Identify duplicates
  2. Update existing records
  3. Delete inserted records

Something like:

static Map<String, Account> getExistingDuplicates(List<Account> records)
    Set<String> keys = new Set<String>();
    for (Account record : record) keys.add(record. LLC_BI__lookupKey__c);

    Map<String, Account> existing = new Map<String, Account>();
    for (Account record : [
        SELECT LLC_BI__lookupKey__c FROM Account WHERE LLC_BI__lookupKey__c IN :keys
    ]) existing.put(record.LLC_BI__lookupKey__c, record);
    return existing;
public static void deduplicate(List<Account> records)
    List<Id> duplicateIds = new List<Id>();
    Map<String, Account> existing = getExistingDuplicates(records);
    for (Account record : records)
        String key = record.LLC_BI__lookupKey__c;
        if (existing.containsKey(key))
            Account clone = record.clone();
            clone.Id = existing.get(key).Id;
            existing.put(key, clone);
    update existing.values();
    // error handling and transaction control recommended
Related Topic