I created one trigger (after insert and after update) for my custom object. If I'm inserting single record the trigger is firing, but when I'm inserting bulk records using DataLoader the trigger isn't firing. Can someone help me?
1 Answer
This trigger is being fired, I suggest, and that the issue is that you assume there's only one Contact in the "searchType" array. This won't necessarily be the case.
To help here I recommend getting the "searchType" details into a map indexed by Contact ID. This is easily done using:
searchType = [SELECT ...];
Map<Id, Contact> contactsById = new Map<Id, Contact>(searchType);
Now instead of using searchType[0]
in this code you should simply do:
for (UNITA_Asks_History__c newAsk : Trigger.new) {
Contact contact = contactsById.get(newAsk.UNITA_Contact_Id__c);
Now replace searchType[0]
with contact
through the rest of the trigger. That way for each asks history entry you look to update the contact related to that history entry.
Of course you may find that multiple history entries relate to the same contact in the same trigger execution, so you should make sure that a given contact is only added for update once (track the IDs of the contacts that you add to the update list, con
and don't add the same one twice otherwise you'll get a DML error).
NB: This code should be moved out of the trigger into a supporting "trigger handler" class which is then invoked from the trigger. That handler should be given the trigger context data it needs and should not directly reference the trigger context variables if you want to be able to test it without having to actually perform a DML operation.
System.debug()
are almost always a bad practice that makes your debugging efforts more difficult.