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 

How to use trigger.new to modify a lead on the fly before inserting into data base.

trigger quad on Lead (before insert) {
  Lead lead = Trigger.new;
  lead = [SELECT Id, lastName, firstName FROM Lead];
  System.debug('==========> First Name ' + lead.firstName);
  System.debug('==========> Last Name ' + lead.lastName);
  lead.firstName = 'Elvis';
  lead.lastName  = 'Presley';
  update lead;
}

 I know I am off in left field, but what I am trying to do is capture the lead before being inserted into the lead data base and modify some of the fields..

The code is what I am trying to get working with no luck...

What should happen is when a lead comes in via WEB2LEAD form on the web, the trigger grabs it before insertion into the data base and attempts to modify two fields so that those two fields will be modded when I look at the record after the insertion and/or update is completed.

So where did I go wrong?

 

Best Answer chosen by Admin (Salesforce Developers) 
tommytxtommytx

Whooeeee.... I figured it out on my own... I have been digging into this stuff 24x7 for 2 weeks now... and I am finally catching on.  This sucker will grab any lead coming in and change the name to Tommy Redneck without a blink of the eye..

Works perfect.  However for those who may be learning... you will note the new[0] which may be new to you..

That means take the first newly inserted lead... like when the Web2Lead normally sends.. but it will not do bulk then is when we do new with a subscript and loop thru all the new bulk leads.. but this is fine for what i am doing...

 

trigger quad on Lead (before insert) {
Lead lead = trigger.new[0];
lead.firstName = 'Tommy';
lead.lastName  = 'Redneck';
}

 Does this mean I get to give myself an attaboy.. I will try it but I am sure that won't work.....

 

trigger quad on Lead (before insert) {
    for (Lead lead : Trigger.new) {
        lead.firstName = 'Billy Bob';
        lead.firstName = 'Thornton';
    }
}

 Just in case anyone cares this is what the code would look like for bulk operations.

It will iterate thu each record one by one if more than one came in at same time..

 

All Answers

tommytxtommytx

Whooeeee.... I figured it out on my own... I have been digging into this stuff 24x7 for 2 weeks now... and I am finally catching on.  This sucker will grab any lead coming in and change the name to Tommy Redneck without a blink of the eye..

Works perfect.  However for those who may be learning... you will note the new[0] which may be new to you..

That means take the first newly inserted lead... like when the Web2Lead normally sends.. but it will not do bulk then is when we do new with a subscript and loop thru all the new bulk leads.. but this is fine for what i am doing...

 

trigger quad on Lead (before insert) {
Lead lead = trigger.new[0];
lead.firstName = 'Tommy';
lead.lastName  = 'Redneck';
}

 Does this mean I get to give myself an attaboy.. I will try it but I am sure that won't work.....

 

trigger quad on Lead (before insert) {
    for (Lead lead : Trigger.new) {
        lead.firstName = 'Billy Bob';
        lead.firstName = 'Thornton';
    }
}

 Just in case anyone cares this is what the code would look like for bulk operations.

It will iterate thu each record one by one if more than one came in at same time..

 

This was selected as the best answer
sfdcfoxsfdcfox

Just remember, not only web to lead will be affected. Imports will be affected, as well as users entering new leads, email2lead, and so on. You might want to be more selective about what you're modifying if your goal is to filter just web-to-lead leads. For example, suppose you import a file with 201 leads, leads 1 and 201 would be affected by your code, for example.