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
Rajesh NarayanaswamyRajesh Narayanaswamy 

whenever a contact is Inserted update the Email with test@test.com


whenver a contact is Inserted update the Email with test@test.com 
please help me to write a trigger for this requirment.
Best Answer chosen by Rajesh Narayanaswamy
Maharajan CMaharajan C
Hi Rajesh,

Don't use the after insert context and unnecessary update for this scenario. Please use the before context which doesnt need any dml statement also:
 
trigger TestTriggeron Contact (before insert) {
	if (Trigger.IsBefore && Trigger.isInsert) {
		for(Contact con: Trigger.new){
			con.email = 'test@test.com';  
		}
	}
}

Thanks,
Maharajan.C

All Answers

mukesh guptamukesh gupta
Hi Rajesh,

Please use below code:-
 
trigger ContactTrigger on Contact (after insert) {
    List<Contact> contList = new List<Contact>();
    if (Trigger.isInsert) {
        for(Contact con: Trigger.new){
			con.email = 'test@test.com';  
            contList.add(con);
        }
       
    }
    if(contList.size() >0)
        update contList;
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
Suraj Tripathi 47Suraj Tripathi 47
Hi,

Please follow the below code:-
 
trigger ContactTrigger on Contact (after insert) {
    List<Contact> conList = Trigger.new;
    if (Trigger.isAfter && Trigger.isInsert ) {
        for(Contact con: conList){
			con.Email = 'test@test.com';  
        }
         update contList;
    }
 }
Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi

 
CharuDuttCharuDutt
Hii Rajesh
Try Below Code
trigger TestTriggeron Contact (after insert) {
    List<Contact> contList = new List<Contact>();
    if (Trigger.IsAfter && Trigger.isInsert) {
        for(Contact con: Trigger.new){
			con.email = 'test@test.com';  
            contList.add(con);
        }
       
    }
    if(contList.size() >0)
        update contList;
}
Please Mark it As Best Answer If It Helps
Thank You!

 
Maharajan CMaharajan C
Hi Rajesh,

Don't use the after insert context and unnecessary update for this scenario. Please use the before context which doesnt need any dml statement also:
 
trigger TestTriggeron Contact (before insert) {
	if (Trigger.IsBefore && Trigger.isInsert) {
		for(Contact con: Trigger.new){
			con.email = 'test@test.com';  
		}
	}
}

Thanks,
Maharajan.C
This was selected as the best answer