Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers 2

up vote 7 down vote accepted

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

share|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 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 at 21:54
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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