+ Start a Discussion
Laboyd001Laboyd001 

Create an apex trigger to add the text 'new' to name field of contact upon contact creation

Hi!
I'm new to apex coding.  I'm looking for an example of code for a trigger that will add the text 'new' to a name field once a contact object has been created.  Any suggestions are greatly appreciated.
Best Answer chosen by Laboyd001
Raj VakatiRaj Vakati
Use this trigger 
 
trigger ContactUpdateTrigger on Contact (before insert, before update) {
    for (Contact c : trigger.new) {
		c.LastName = 'new '+c.LastName ;
	}
    
}

 

All Answers

Raj VakatiRaj Vakati
Use this trigger 
 
trigger ContactUpdateTrigger on Contact (before insert, before update) {
    for (Contact c : trigger.new) {
		c.LastName = 'new '+c.LastName ;
	}
    
}

 
This was selected as the best answer
Ankit SehgalAnkit Sehgal
Create a trigger on before insert. Record Id can be fetched using Trigger.new. 
Laboyd001Laboyd001
Thanks for the example Raj.  Thanks for the explanation Ankit.  I appreciate both of your replies.