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
tommytxtommytx 

Does the new in trigger.new mean it works only on the new incoming leads?

trigger testTrigger on Lead (before insert, before update) {
    for(Lead l: trigger.new){

 

Does trigger.new mean the trigger only works on the new lead that just came in and if a bulk insert then it will work on all the new bulk leads only and none of the existing leads?

The reason I ask, is that i want to modify an incoming lead from Web2Lead form and the only way I can imagine to get access to the data being inserted as a lead from the Web2Lead is to set a trigger to grab the lead before insert, but want to find a way to mod only the incoming lead that just happened.... So when I play with "Select" I want to be sure that I will only be selecting the newly arrived lead before it has yet been inserted.  I assume if I tamper with it here i can change it before it enters the data base.

Is there a special "Select" statement that I would use to "Select" modify and then "Update" the lead as it is inserted?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Trigger.new is the "new" information that will be submitted to the database after the transaction completes. Trigger.old, in contrast, is the information that WAS in the database before the transaction started. For insert and undelete operations, there won't be any "old" information (Trigger.old will be null), and for deletions, there won't be any "new" data (Trigger.new will be null). Trigger.old and Trigger.new allow a developer to detect changes between the old values and new values during an "update" operation.

 

More to the point, as it relates to your inquery, the "before insert" part on line one is what tells the database to run this code when a record is created; "before update" indicates that it will run when an existing record is updated (e.g. a user presses "edit", makes some modifications, and then presses "save"). Trigger.new is simply the data that will be committed to the database. By modifying the values in Trigger.new, you will directly modify the data that will be committed to the database.

 

You may consider taking a short tutorial on SQL to understand CRUD (create, read, update, delete), which will help you visualize how triggers operate within salesforce.com.

All Answers

sfdcfoxsfdcfox

Trigger.new is the "new" information that will be submitted to the database after the transaction completes. Trigger.old, in contrast, is the information that WAS in the database before the transaction started. For insert and undelete operations, there won't be any "old" information (Trigger.old will be null), and for deletions, there won't be any "new" data (Trigger.new will be null). Trigger.old and Trigger.new allow a developer to detect changes between the old values and new values during an "update" operation.

 

More to the point, as it relates to your inquery, the "before insert" part on line one is what tells the database to run this code when a record is created; "before update" indicates that it will run when an existing record is updated (e.g. a user presses "edit", makes some modifications, and then presses "save"). Trigger.new is simply the data that will be committed to the database. By modifying the values in Trigger.new, you will directly modify the data that will be committed to the database.

 

You may consider taking a short tutorial on SQL to understand CRUD (create, read, update, delete), which will help you visualize how triggers operate within salesforce.com.

This was selected as the best answer
tommytxtommytx
Thanks that is very helpful... not there yet but getting there... I am familiar very much with mysql and Php... done a ton of that but never used the trigger trick... so the only thing i have left is to try to figure out how to grab the data via the trigger.new and make the changes and I am on a roll... For example actually I want to grab say lastName and change it to something else when the trigger is done... So I need to search for ways to do a select to make the changes... I will look some more and maybe come back with another question on how to select the data that is comeing in on the trigger when a new lead hits... maybe I will have another question once I play with select from the trigger.... Thanks for now..
sfdcfoxsfdcfox

There's no need to select the incoming data; it's handed to you on a silver platter. Consider the following code:

 

trigger lowercaseCompanyName on Lead (before insert) {
  for(Lead record:Trigger.new) {
    if(String.isNotBlank(record.Company)) {
      record.Company = record.Company.lowerCase();
    }
  }
}

You will only need to select data that isn't part of the DML operation (such as related records).