Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

When to use trigger.new[0] and trigger.new in salesforce?

Asked by Ella Clarkson May 12, 2023 1.0K views 1 answer
Share

About this question

 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]?
  1. 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 example when a Lead is being converted, I need to check that a contact with the same email does not exist (I know I can do it in duplication rules, but I am doing it in trigger). Now what I did was:

if(!trigger.new[0].status.equalsIgnoreCase('Qualified')) { List 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?
  1. When is ok to use trigger.new[0] and not ok to use trigger.new[0]? I know if you are working on mass records like mass update or mass delete etc., obviously we have to use trigger.new and not trigger.new[0]. But any other scenarios?

Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.