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:
trigger TINCheck on Account (before insert) {
dupeCheck.checkforDupes(trigger.New);
}
Class:
//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){
namesFromTrigger.add(acct.LLC_BI__lookupKey__c);
}
//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?
}
}
}
}
}