+ Start a Discussion
jkcjkc 

No assigned Id to record in Before Insert trigger

Hi,
I'm in a terrible dilemma with the trigger. I was wondering why there is no id assigned on the record anymore
trigger trigger_Account on Account(before insert, before update){
  for( Account acc : Trigger.new){
    system.debug('Account id: ' + acc.id);
  }
}
this change affected some of my previous triggers
Best Answer chosen by jkc
ManojSankaranManojSankaran
Hi,

Your trigger will fire in two condition before insert and before update.

In before insert - id value wont be there....since the record is not inserted into database.
in before update - id value will be there and you will be albe to use that id for calcualtion.

Only in After Insert you will get the id of that record.


Let me know if you need more clarification. If it answers your question please mark it as answer.

Thanks
Manoj S

All Answers

ManojSankaranManojSankaran
Hi,

Your trigger will fire in two condition before insert and before update.

In before insert - id value wont be there....since the record is not inserted into database.
in before update - id value will be there and you will be albe to use that id for calcualtion.

Only in After Insert you will get the id of that record.


Let me know if you need more clarification. If it answers your question please mark it as answer.

Thanks
Manoj S
This was selected as the best answer
Uvais KomathUvais Komath

As manoj mentioned Id is assigned after commiting to database.

U wouldnt need id of the newly inserted objects unless u want to make a reerence to new objects from another objects.

For that write your logic in a class and call methods of that class from after update or after insert.

refer this to write a clean trigger handling everything properly:

http://developer.force.com/cookbook/recipe/trigger-pattern-for-tidy-streamlined-bulkified-triggers