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
renu anamalla 9renu anamalla 9 

After insert and after update differnce n give simple scenario

After insert and after update  differnce n give simple scenario
SFDC GuestSFDC Guest
Hi renu,

After insert: Trigger will be fired after inserting a record.
After insert
-->  is used to insert related object, not the same object.
--> to send notification email.

After Update: 
Trigger will be fired after updating a record.
After update
-->  is used to update related object, not the same object.
--> to send notification email.

If we want to update a record of same object, we cannot use After trigger, because it causes read only error. This is because, after inserting or updating, we cannot update a record. Update should be made in before event for the same object.

Here is the example for automatically creating Opportunity after creating/updating an Account record.

trigger addRelatedRecord on Account (after insert, after update)
{
    List<Opportunity> oppList = new List<Opportunity>();

    for ( Account a: [SELECT Id, Name From Account

           where id IN : Trigger.new And Id Not In (Select AccountId From Opportunity)])

    {

    oppList.add(new Opportunity(Name=a.Name + 'Opportunity', StageName='Prospecting', CloseDate= System.today().addMonths(1), AccountId=a.Id));
    }

    if(oppList.size()>0)
    {
    insert oppList;
    }
}

Please mark it as a best answer if it helps you.

Thank You,
Sohel Mohd