After many attempts on my part, I'm hoping someone is able to help me out with this :)
Background and Desired Result:
I have a seemingly straightforward Apex Trigger that fires as Contacts are inserted, updated, etc...
I would like this trigger to simply update a single field on the Account that the Contact is tied to.
The Issue:
The trigger works as expected when Contacts are created or edited manually through the SFDC interface. However, the trigger does not appear to fire when thousands of contacts are updated in mass (i.e. via the Import Wizard).
I have included my code below. Any help would be greatly appreciated!
My Apex Code:
Trigger setNTCOnAccountViaContact on Contact (after INSERT, after UPDATE) {
Set<ID> setAccountIDs = new Set<ID>();
for (Contact c : Trigger.new) {
setAccountIDs.add(c.AccountID);
}
String accountID = null;
for (String setElement : setAccountIDs) {
accountID = setElement;
break;
}
List<Contact> nextTargetContactObject = [SELECT Id FROM Contact
WHERE Target_Contact__c = TRUE AND
Account.Id = :accountID
ORDER BY Target_Contact_Order__c ASC
LIMIT 1];
List<Account> accounts = [SELECT Id FROM Account WHERE Id = :accountID];
if (nextTargetContactObject.size() == 1) {
for (Account account : accounts) {
account.Next_Target_Contact_ID__c = nextTargetContactObject[0].Id;
}
} else {
for (Account account : accounts) {
account.Next_Target_Contact_ID__c = NULL;
}
}
update accounts;
}
break;
? This seems like a rubber ducky problem