function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
OneNewbieOneNewbie 

Trigger batch update problems

Hopefully I can get some help here. I created this trigger to update the account name under contact. This code runs ok when I do single record insert and update. It also works ok when I do batch inserts. However, it will not do updates when I run batch updates. Am I missing something? Why would it run ok under batch insert but not batch update? Thanks for your help.

trigger updateContact on Contact (before insert, before update) {
    Set<String> acclist = new Set<String>();
    Map<String, Account> accountlist = new Map<String, Account>();

    for (Contact c : Trigger.new)
    {
        if (c.lookupAccountName__c != null)
            acclist.add(c.lookupAccountName__c);
    }   

    if (acclist.size() > 0)
    {               
        for (Account a2 : [Select Id, Name from Account where Name in :acclist]){
            if (a2 !=null)
                accountlist.put(a2.Name, a2);
            else
                accountlist.put(a2.Name, null);
        }
    }   

    for (Contact c : Trigger.new)
    {
        if (c.lookupAccountName__c != null) {
            if (accountlist.get(c.lookupAccountName__c) != null)
                c.AccountId = accountlist.get(c.lookupAccountName__c).Id;
        }
    }   
}
WhyserWhyser
I notice that there are no update commands in your trigger
 
update accountlist; // Do maps work with DML statements? I've only worked with lists or single objects when calling a DML statement
update c;
 
how in your trigger do your records get updated?
 
OneNewbieOneNewbie
why does it work without "update c" when I do a single record but it will not work for batch updates? Also, when I do single inserts or batch inserts the trigger does what is supposed to?

Any ideas?