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
Preetham ThummalaPreetham Thummala 

trigger.new

Hi
Can we perform Read/write on Trigger.new in before insert?
thanks
 
DmonikaDmonika
Tigger.new will return a list of new version records.With help of those values we can do validation on the same record.
Amit Chaudhary 8Amit Chaudhary 8
For example, in this simple trigger, Trigger.new is a list of sObjects and can be iterated over in a for loop, or 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 : Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert andupdate triggers, and the records can only be modified in before triggers.

 
Trigger.old : Returns a list of the old versions of the sObject records. Note that this sObject list is only available in update anddelete triggers.


1) isExecuting Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or anexecuteanonymous() API call.

2) isInsert Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or theAPI.

3) isUpdate Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or theAPI.

4) isDelete Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or theAPI.

5) isBefore Returns true if this trigger was fired before any record was saved.

6) isAfter Returns true if this trigger was fired after all records were saved.

7) isUndelete Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)

8) new Returns a list of the new versions of the sObject records.Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.

9) newMap A map of IDs to the new versions of the sObject records. Note that this map is only available in before update, after insert, and after update triggers.

10) old Returns a list of the old versions of the sObject records.Note that this sObject list is only available in update and delete triggers.

11) oldMap A map of IDs to the old versions of the sObject records.Note that this map is only available in update and delete triggers.

12) size The total number of records in a trigger invocation, both old and new.



Please check below blog for more information:-
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
https://help.salesforce.com/apex/HTViewSolution?id=000003789&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000003789&language=en_US)
Please let us know if this will help you. 


Thanks,
Amit Chaudhary
Thirumurugan PThirumurugan P
Thank for good info