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
kcnmaheshkcnmahesh 

How to get current record values when we are working with triggers

I created one trigger on an object.

The trigger executes when we do any operations on an object.

I need to get the current record values.

How can i get the current record values or ID.....?

sanjdevsanjdev

Hi 

 

You can get the current record by Trigger.new context variable.

 

For Example:

 

Trigger test on Account (after insert) {
for (Account a : Trigger.new) {
// Iterate over each sObject records that were inserted

}

// 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];
}

 

You can get the current record Id by below:

 

Id id = Trigger,new[].Id;

 

If this helps you. Mark it as resolved

 

Cheers

Sanj