You need to sign in to do that
Don't have an account?
OneNewbie
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;
}
}
}
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;
}
}
}
Any ideas?