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
SFDC DevlSFDC Devl 

If one record in batch fails entire batch fails- apex triggers

Hello,

I have an Apex trigger (after insert) on Opportunity which updates the Contact on that Opportunity upon insert. While inserting thousands of Opportunities via Data loader, the 199 Opportunities are inserted but 199 Contacts are not updated for the whole batch if one opportunity insert fails in a batch of 200 records.

Do you know of a way where trigger does a partial update of the batch with successful inserts.
I tried using try and catch, Database.SaveResult[] result = Database.update(contactList, false); but no luck

Can anyone suggest how to resolve this? 
Shashikant SharmaShashikant Sharma
Hi,

Your code for Contact update is correct but issue is in opportunity insert itself so data loader opportunity insert is failed. You could reduce the size of Batch in DataLoader to 1 to resolve it.

Thanks
 
SFDC DevlSFDC Devl
Hi Shashikant,

Thanks for your response! We are already inserting with batch size 1 but we have thousands to load each time and its time consuming so I was wondering if there is a way to improve this one.

Thanks
SFDC DevlSFDC Devl
I was thinking to schedule a batch job to update the Contacts after all the opportunities are inserted. For our scenario, nightly update is enough than the real time updates. Lets see how the batch works! Mean while if any of you have better ideas, please feel free to post.