You need to sign in to do that
Don't have an account?

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
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
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
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
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
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm.
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