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
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

Dongzhi Yang 27Dongzhi Yang 27
@Uvais Komath, I think the Id is assigned after saving to the database without commit. commiting to the database happens last after "after trigger" according to the link below
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm.
Robert WhiskerRobert Whisker
In after triggering the instance of trigger.new is in read-only mode soacc.ZTS_EU_BrickCode__c = z.ZTS_EU_BrickCode__r.The name is creating the issue. Here you are trying to update the trigge.new instance which is only possible in before trigger not in after trigger.
for(Account acc:accList){ account accObjTemp = new account(); accObjTemp = acc; String zipcode = (acc.ZTS_EU_Market__c == ZTS_EU_GlobalConstants.MARKET_VAL)?(acc.ZTS_EU_State_County_Value__c != null ? acc.ZTS_EU_State_County_Value__c : ''): (acc.ZTS_EU_Market__c==ZTS_EU_GlobalConstants.UK_MARKET_VAL ? (acc.ZTS_EU_UK_PC4_from_Postalcode__c != '' ? acc.ZTS_EU_UK_PC4_from_Postalcode__c : ''): (acc.ZTS_EU_Zip_Postal_Code__c != null ? acc.ZTS_EU_Zip_Postal_Code__c:'')); if(!String.isBlank(zipcode) && zipToBrickMap.containsKey(zipcode+';'+acc.ZTS_EU_Market__c)){ List<ZTS_GL_ZipToBrick__c> zipBrickList = zipToBrickMap.get(zipcode+';'+acc.ZTS_EU_Market__c); for(ZTS_GL_ZipToBrick__c z : zipBrickList){ accObjTemp.ZTS_EU_BrickCode__c = z.ZTS_EU_BrickCode__r.Name; lstBricks.add(accObjTemp); } } if(lstBricks.size()>0){ insert lstBricks; System.debug('Insert Successful'); } you should check at wash a car without a hose (https://washingears.com/how-to-wash-a-car-without-a-hose/) also