+ Start a Discussion
DhairyaDhairya 

Trigger.new

I am new to apex development. I wrote few triggers without using trigger.new variable and they worked fine.

 

Can anyone explain what is the importance of Trigger.new and its mandatory to use trigger.new?

 

Thanks 

Best Answer chosen by Admin (Salesforce Developers) 
Imran MohammedImran Mohammed

Hi,

 

The trigger you wrote definitely has a performance issue, even though if it is working for you.

Generally in Triggers, only those records get fired which underwent a DMl operation. 

Your query will run against all accounts rather than just the fired one.

 

Your trigger should be like this,

trigger WorldTrigger on Account (after insert, after update) {
    
    list<account> accs = new list<account>();

    
    for(account a:Trigger.new )
    {

 if(a.hello__c == null)

 {
         a.Hello__c='World';

 accs.add(a);

 }
        
    }
    if(accs.size() > 0)

    {
     update accs;

    }
}

 

Let  me know if you face any issues.

All Answers

Imran MohammedImran Mohammed

Trigger.new holds all new records of a object that got fired due to an event(after insert, after update etc).

Yes you should use Trigger.new to get the latest set of records that were inserted, updated.

Did you perform any DML operation on the same object the trigger was written on, as you said thetriggers you wrote work fine?

 

anil 007anil 007

HIII,

 

triggers.new  are implicitly defined in all triggers, and provide access
to the records that caused the trigger to fire. it  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.

 

for more details go through  Apex Code Developer's Guide 

 

 

DhairyaDhairya

Hi Mr. Imran,

 

    Thanks for your prompt reply and explanation.

 

    Below is my Trigger code and i assume it works fine. 

 

trigger WorldTrigger on Account (after insert, after update) {
    
    list<account> accs = [select id, hello__c from account where hello__c=null ];
    
    for(account a:accs )
    {
        a.Hello__c='World';
        
    }
    
    update(accs);
}

 

 

Pls. let me know, is it right way to write?

 

Thanks,

Dhairya

DhairyaDhairya

Thanks Anil,

 

               Appreciate your prompt and detail response.

 

Thanks,

Dhairya

Imran MohammedImran Mohammed

Hi,

 

The trigger you wrote definitely has a performance issue, even though if it is working for you.

Generally in Triggers, only those records get fired which underwent a DMl operation. 

Your query will run against all accounts rather than just the fired one.

 

Your trigger should be like this,

trigger WorldTrigger on Account (after insert, after update) {
    
    list<account> accs = new list<account>();

    
    for(account a:Trigger.new )
    {

 if(a.hello__c == null)

 {
         a.Hello__c='World';

 accs.add(a);

 }
        
    }
    if(accs.size() > 0)

    {
     update accs;

    }
}

 

Let  me know if you face any issues.

This was selected as the best answer
DhairyaDhairya

Thank you Imran. I got it. 

 

Thank you very much.

 

Regards,

Dhairya