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
nikkeynikkey 

Trigger syntax

Hi Folks ,

can any one help me 

 

What is the difference in this syntax below :

 

list<Student__c> newstud = trigger.new;
for(Student__c st: trigger.new)
if(trigger.new[].fee__c>500)

 

Thanks in Advance 

 

Best Answer chosen by nikkey
gautam_singhgautam_singh

Hi,


1. list<Student__c> newstud = trigger.new;

This will create a List which is collection in Salesforce of Students Custom Object Records , All of which are coming through new versions.

2. for(Student__c st: trigger.new)

This iterates over the Student sObject and takes those inside the loop which are coming as a new version. This is only declaration , Now you can enter logic after this in braces {}.  You are not allowed give DML statements inside the loop.

This is creating a looping variable over which you can iterate. It can be taken equivalent to the 1. The differnce is you cannot check any more conditions when you are inserting values in newstud there.



3. if(trigger.new[].fee__c>500)

This check the value of the Fee Field in the records which come through trigger.new.



Check Trigger.new Docs


Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You

All Answers

gautam_singhgautam_singh

Hi,


1. list<Student__c> newstud = trigger.new;

This will create a List which is collection in Salesforce of Students Custom Object Records , All of which are coming through new versions.

2. for(Student__c st: trigger.new)

This iterates over the Student sObject and takes those inside the loop which are coming as a new version. This is only declaration , Now you can enter logic after this in braces {}.  You are not allowed give DML statements inside the loop.

This is creating a looping variable over which you can iterate. It can be taken equivalent to the 1. The differnce is you cannot check any more conditions when you are inserting values in newstud there.



3. if(trigger.new[].fee__c>500)

This check the value of the Fee Field in the records which come through trigger.new.



Check Trigger.new Docs


Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You

This was selected as the best answer
liron169liron169

I think this:

if(trigger.new[].fee__c>500)

 

should give you compile error.

 

trigger.new is a list. so you should write which element in the list you approaching.

e.g.:

 

if(trigger.new[1].fee__c>500)