1

I have the following trigger logic built in Contact object during Create event.

If Mobile Phone is given and Home Phone is not given while creating a contact, then set the Mobile as Home Phone Number.

If I do an insert via Data Loader for about ~2000 Contact records will it cause any DML exception ?

What is the best practice for this ?

Do note that apart from the above logic there is no trigger logic built on contact

  • 6
    That all depends on how your trigger is implemented. There shouldn't be any DML exception but if the trigger is implemented poorly then there could well be. Adding your code will help people answer your question. – BarCotter Oct 23 '14 at 13:40
  • 4
    The logic you've described could easily be performed in a before trigger and shouldn't require any additional DML. It's also worth noting that simple logic such as you've described can be easily accomplished using a Workflow Rule and a Field Update, rendering your trigger unnecessary. – Alex Tennant Oct 23 '14 at 13:55
5

A few general best practice for triggers that applies generally

  1. One Trigger per object
  2. Keep SOQL queries outside of loops by leveraging collections (Lists, Sets, Maps)
  3. Keep DML outside of loops by leveraging collections (Lists, Sets, Maps) and performing DML on a collection rather than individual records
  4. Keep logic outside of the trigger itself. Keep your logic in helper classes that helps in making your code portable and reusable.

Your particular use case seems pretty simple as your logic can be achieved in a before trigger, which will require no DML calls in the trigger at all. You can change the records prior to them being committed to the database. Unless there are other requirements, its also worth noting a trigger might not even be necessary here as what you described can be achieved with a Work Flow Rule.

If you share your code that you have for the trigger so far, would be happy to help optimize it or try to find any errors you might be running into.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.