+ Start a Discussion
MellowYellowMellowYellow 

Trigger to populate fields on a custom object, only executes on new records

I'm trying to find a way to have several fields populate automatically when a new record is entered.  Most of the fields values will come from SOQL Selects.

The trigger should only execute the first time the record is created.  The records will be created from the standard 'New' button, but a custom button can also be used.

 

Thanks for any examples or links

Best Answer chosen by Admin (Salesforce Developers) 
Jake GmerekJake Gmerek

You can us the isInsert trigger context variable(http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm) and then update the fields that you need.

 

if(Trigger.isInsert && Triggger.isBefore){

 

foreach (myobj__c mine: trigger.new){

  mine.field1_c = "new value"

}  

}

 

That is the basic idea, but you will have to gather the data that you want to put on the object before this possibly and make sure you bulkify throughout.  Let me know if you have specific questions.