2

I understand that trigger.new contains the list of my records with new values of the records. My questions are:

  1. If multiple users work on the same single object and they do an update should I use Trigger.new or trigger.new[0]?
  2. 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');
            }
        }
  1. Is it ok to use trigger.new[0] in the above case, as one/more users will convert only one lead at time?
  2. When is ok to use trigger.new[0] and not ok to use trigger.new[0]? I know if you are working in 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?
1
  • 3
    You should pretty much never use trigger.new[0], it should never be the correct pattern.
    – Adrian Larson
    Commented Jul 10, 2020 at 15:06

2 Answers 2

6

That's not how salesforce works. A trigger will never contain records updated by different users because those will be dealt with in a separate transaction. When using Trigger.new[0] you are only taking the first record updated in this transaction and ignoring any others. This is an anti-pattern because you don't know what's the context of the operation, for example it might be a mass update and then your trigger will only work for one record of all being updated.

While in theory, you could take only one record for your case of converting leads, it is safer to put it inside a loop and handle all other records which could show up instead of waiting for when it fails in the future.

Please read those: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_transaction.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_bestpract.htm

2
  • Thank you. I have read the links. I have some more doubts, now what happens if 2 users work on the same record and lets say at the same exact time update the same field of this record. Which is the value you will see(Bit hypothetical, I know)? Now, I login to my salesforce and login again using incognito mode and work in both normal and incognito mode are they considered different tranactions?
    – nSv23
    Commented Jul 10, 2020 at 22:28
  • 2
    @nSv23 Specific answer to your hypothetical question - the platform does its best to prevent data loss, including Collision Detection (two users modifying the same record). The platform prevents the second transaction from being successful and the user sees an error message. More details: Collision Detection
    – Mark Pond
    Commented Jul 10, 2020 at 23:03
2

Each user will have have their own and separate transaction , even though they update same record at the same time. Transaction in Apex

We use Trigger.New for bulkification and to avoid governor limits during

  1. If the request is coming for a single record , then trigger.New[0] mayn't throw an error and still work.

  2. However if there is an operation on bulk of records trigger.New[0] will represent the only the first record in the bulk of records.

Combining both, it's better and safer to use trigger.new which will support a single record as well as bulk records.

2
  • Thank you. I have read the links. I have some more doubts, now what happens if 2 users work on the same record and lets say at the same exact time update the same field of this record. Which is the value you will see(Bit hypothetical, I know)? Now, I login to my salesforce and login again using incognito mode and work in both normal and incognito mode are they considered different tranactions?
    – nSv23
    Commented Jul 10, 2020 at 22:28
  • 1
    When a user or an operation tries to update a record , it places a lock so no other user or other operation can update that record, unless lock is released - Read details here- help.salesforce.com/… To your last question- Yes , those are considered 2 different transactions. Commented Jul 11, 2020 at 5:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .