rigger Scenario 4 on before insert and before update with trigger.new

 The following trigger describes about when the leads are inserted into the data base it would add Doctor prefixed for all lead names. This is applicable for both inserting and updating the lead records.

Trigger

trigger PrefixDoctor on Lead (before insert,before update)

{

List<Lead> leadList = trigger.new;

for(Lead l: leadList)

{

l.firstname = 'Dr. '+ l.firstname;

}

}

How to see output

Before Insert:

Go to leads tab and click on “New” button.


Fill first name and mandatory fields and click “Save” button.

“Dr. “ added to before First Name.

Before Update:

Click on “Edit” button in already saved record in leads.


Change First Name and click on "Save" button

“Dr. “ added to before First Name.
Test Class
@isTest
public class PrefixDoctorTest {
    Public static testMethod void PreDoctor(){
        Lead l=new Lead();
        l.LastName='Ramu';
        l.company='Zuventas';
        l.Status='Open - Not Contacted';
        insert l;
    }
}
100% Code coverage :