I'm writing a trigger to update another object's field. I know a trigger can fire on multiple records. My question is if the records are unique? E.g. A trigger is called with 5 records. Does SF guarantee that each of the 5 records are different or could the same record be in the list twice? I want to use a Map and thought to use the object's ID as the key.

The first example [in this link] (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bulk_idioms.htm) implies a record can appear more than once as it uses a set to guarantee uniqueness.

Thanks, Scott


Yes, the records are unique. In fact, in an ApexTrigger or the context of a trigger specifically, you can use Trigger.newMap (which is populated on after insert, but not before insert) and also Trigger.oldMap in place of creating your own map.

Here's a link to the Trigger Context Variable documentation for more info on the maps and when they are available: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

| improve this answer | |
  • Thanks for the confirmation. I've been looking for documentation guaranteeing such behavior but found nothing that specific. Paranoid I am, but when messing with a company's data a healthy dose of paranoia goes a long way! :-) – Scott Feb 14 '14 at 21:48
  • No problem. It's worth to note that the records are guaranteed to be unique by id, but not necessarily any other criteria. Meaning that your trigger may contain records that have the same email addresses or other dedupe criteria. As an example, if you import 100 records with 5 distinct emails in data loader, your trigger will run over 100 records. – James Loghry Feb 14 '14 at 21:54

In a trigger, all of the records are unique. If you have the same record in a list twice and do a DML, salesforce will actually throw an error. Make sure you do not place the same record twice in a list.

| improve this answer | |

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.