In Before insert you will not get the record Id and in After Insert you will get the record Id....
If you are going to do changes from trigger in same object then its good to use Before Event...But if you are going to do changes in another object where you need this object Id and then you should go for After Insert..
Example:
If Your Trigger is on Account and you want to update somefields based on phone number, then you can go for before event...
But if you want to insert Contacts based to inserted Account and want to associate them..then in this case you will need Account Id ..so you can ise after insert adn then you can insert contact where you will provide account id..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
In Before insert you will not get the record Id and in After Insert you will get the record Id....
If you are going to do changes from trigger in same object then its good to use Before Event...But if you are going to do changes in another object where you need this object Id and then you should go for After Insert..
Example:
If Your Trigger is on Account and you want to update somefields based on phone number, then you can go for before event...
But if you want to insert Contacts based to inserted Account and want to associate them..then in this case you will need Account Id ..so you can ise after insert adn then you can insert contact where you will provide account id..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
In Before insert you will not get the record Id and in After Insert you will get the record id
If you are going to do changes from trigger in same object then its good to use Before Event...But if you are going to do changes in another object where you need this object Id and then you should go for After Insert
This is the with in the object by using the before insert. trigger accountrigger on Account (before delete,before insert,before update) { list<opportunity> opptyclosedlos=[select id,name,closedate,stagename from opportunity where accountid=:trigger.newmap.keyset() and stagename='closed-lost,closed-won']; for(Account a:trigger.new){ for(opportunity opp:opptyclosedlos){ if(opp.Accountid==a.id) system.debug('>>>>>'+opp);
} } }
when ever the contact(or) opportunity object feilds are updated by using after insert,
trigger accountTestTrggr on Account (after insert,after update) { List<Account> accountsWithContacts = [select id, name,(select id,firstname, lastname,Description from Contacts) from Account where Id IN :Trigger.newMap.keySet()]; system.debug('>>>>'+accountsWithContacts ); List<Contact> contactsToUpdate = new List<Contact>(); system.debug('>>>'+contactsToUpdate); for(Account a: accountsWithContacts){ for(Contact c: a.Contacts){ c.Description=c.firstname+c.lastname; contactsToUpdate.add(c); } } update contactsToUpdate; }
In Before insert you will not get the record Id and in After Insert you will get the record Id....
If you are going to do changes from trigger in same object then its good to use Before Event...But if you are going to do changes in another object where you need this object Id and then you should go for After Insert..
Example:
If Your Trigger is on Account and you want to update somefields based on phone number, then you can go for before event...
But if you want to insert Contacts based to inserted Account and want to associate them..then in this case you will need Account Id ..so you can ise after insert adn then you can insert contact where you will provide account id..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
All Answers
In Before insert you will not get the record Id and in After Insert you will get the record Id....
If you are going to do changes from trigger in same object then its good to use Before Event...But if you are going to do changes in another object where you need this object Id and then you should go for After Insert..
Example:
If Your Trigger is on Account and you want to update somefields based on phone number, then you can go for before event...
But if you want to insert Contacts based to inserted Account and want to associate them..then in this case you will need Account Id ..so you can ise after insert adn then you can insert contact where you will provide account id..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
In Before insert you will not get the record Id and in After Insert you will get the record id
If you are going to do changes from trigger in same object then its good to use Before Event...But if you are going to do changes in another object where you need this object Id and then you should go for After Insert
This is the with in the object by using the before insert.
trigger accountrigger on Account (before delete,before insert,before update) {
list<opportunity> opptyclosedlos=[select id,name,closedate,stagename from opportunity where
accountid=:trigger.newmap.keyset() and stagename='closed-lost,closed-won'];
for(Account a:trigger.new){
for(opportunity opp:opptyclosedlos){
if(opp.Accountid==a.id)
system.debug('>>>>>'+opp);
}
}
}
when ever the contact(or) opportunity object feilds are updated by using after insert,
trigger accountTestTrggr on Account (after insert,after update) {
List<Account> accountsWithContacts = [select id, name,(select id,firstname, lastname,Description from Contacts) from Account where Id IN :Trigger.newMap.keySet()];
system.debug('>>>>'+accountsWithContacts );
List<Contact> contactsToUpdate = new List<Contact>();
system.debug('>>>'+contactsToUpdate);
for(Account a: accountsWithContacts){
for(Contact c: a.Contacts){
c.Description=c.firstname+c.lastname;
contactsToUpdate.add(c);
}
}
update contactsToUpdate;
}