function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Harsha SekharHarsha Sekhar 

when trigger.new returns more than one record

Hi Guys,

Is it possible that Trigger.new(returns list<Sobject>) returns more one record. If its possible can someone explain when it will.

Thanks
Harsha
Anant KamatAnant Kamat
All triggers are bulk triggers by default, and can process upto 200 records at a time.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Harsha,

Greetings to you!

Trigger.new returns a list of the new versions of the sObject records.
This sObject list is only available in insert, update, and undelete triggers, and the records can only be modified in before triggers.

For example, in this simple trigger, Trigger.new is a list of sObjects and can be iterated over in a for loop. It can also be used as a bind variable in the IN clause of a SOQL query.
 
Trigger simpleTrigger on Account (after insert) {
    for (Account a : Trigger.new) {
        // Iterate over each sObject
    }

    // This single query finds every contact that is associated with any of the
    // triggering accounts. Note that although Trigger.new is a collection of  
    // records, when used as a bind variable in a SOQL query, Apex automatically
    // transforms the list of records into a list of corresponding Ids.
    Contact[] cons = [SELECT LastName FROM Contact
                      WHERE AccountId IN :Trigger.new];
}

Trigger.new contains the records that caused the trigger to fire. Triggers can fire when one record is inserted, or when many records are inserted in bulk via the API or Apex. Therefore, context variables, such as Trigger.New, can contain only one record or multiple records. You can iterate over Trigger.New to get each individual sObject.
 
// Trigger.new will contain one record
insert new Account();

// Trigger.new will contain three records
insert new List<Account>{ new Account(), new Account(), new Account() };

Please refer to the below links which might help you further with the above requirement.

https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers/apex_triggers_intro

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

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas