I understand that trigger.new
contains the list of my records with new values of the records. My questions are:
- If multiple users work on the same single object and they do an update should I use Trigger.new or trigger.new[0]?
- If multiple users work on different records of the same object(say lead) and they do an update should I use Trigger.new or trigger.new[0]?
As an exmple when a Lead is being converted, I need to check that a contact with the same email does not exists (I know I can do in duplication rules, but I am doing it in trigger). Now what I did was:
if(!trigger.new[0].status.equalsIgnoreCase('Qualified')) {
List<Contact> existingContacts = [
SELECT Email
FROM Contact
WHERE Email =: trigger.new[0].Email
LIMIT 1
];
if(!existingContacts.isEmpty()) {
trigger.new[0].addError('A contact already exists');
}
}
- Is it ok to use
trigger.new[0]
in the above case, as one/more users will convert only one lead at time? - When is ok to use
trigger.new[0]
and not ok to usetrigger.new[0]
? I know if you are working in mass records like mass update or mass delete etc., obviously we have to usetrigger.new
and nottrigger.new[0]
. But any other scenarios?
trigger.new[0]
, it should never be the correct pattern.