How to perform DML operation in after insert trigger

Feedback

Have you ever been into a situation where you want to update the same object on which after insert trigger have been written on, In a simple lay man language let’s consider a situation that you have to written a trigger on after insert event and that trigger would update some fields of some other objects and this trigger would also need to update the same record after creation of that record.Let’s take a general code example and see what happened when we try to perform above following operation.

Perform DML operation on the same object on which trigger is written on

Let see a simple code example in which a trigger is working on account object in after insert event, so when a new record gets created the trigger would fire at the same time we want to update the record which recently gets created, see the below code.

trigger UpdateSameObject on Account (After insert) {

	Account aa=new Account();

	for(Account a:trigger.new)
	{
		a.Website='ohotech.com';
	}
}

The above code could work perfectly in before insert trigger but we want to written trigger in after insert event because we are required ID of the newly created record somewhere in the code and at the same time we also want to update the newly created record , so we need to write the trigger in after insert event to have ID of newly created record, if we execute above code by creating a new account record it would give us an error, see below image of the error.

error in after insert event

So to make sure we achieve both the objectives in after insert event we get the ID of the newly created record and also update the object on which trigger is working so to update the object we are going to use newly object ID and using that ID we would create a instance of object and associate it with the newly created ID, Let see in below code.

trigger UpdateSameObject on Account (After insert) {

	list<Account> accList=new Account();
	for(Account a:trigger.new)
	{
		Account aa=new Account(ID=a.id);
		aa.Website='ohotech.com';
		accList.add(aa);
	}
	update accList;
}

As you can see in the above code i create a new object instance aa and associate that object with ID of the newly created record so the new created object instance is now pointing to the that object on which we have written the trigger and added the new object instance to the list and update it but when we try to create record the trigger would get stuck in recursion so now we have to resolve the problem of trigger recursion to do that we have to use a Boolean variable which make sure that trigger would run only one time, so i have to little expand my previous version of trigger.

trigger UpdateSameObject on Account (After insert) {

	list<Account> accList=new list<Account>();
	public static boolean isExecuted = True;

    if(trigger.isInsert){
		for(Account a:trigger.new)
		{
			Account aa=new Account(ID=a.id);
			aa.Website='ohotech.com';
			accList.add(aa);
		}
    }

		if(isExecuted) //To check whether the trigger is not get stuck in recursion if we are updating the same object
		{
		 isExecuted=False;
		 system.debug('Inside update');
		 update accList;
		}

}

That’s it now your code work perfect so in this whole tutorial we have learn three things

1.) How to get ID of the object and update the same object in after insert trigger.
2.) How to remove recursion in trigger.

I hope you like my this post, if you have any query related to this post please leave comment below in comment section and don’t forget to subscribe to my blog and social networking pages, you can also go through the related post from the below links.

Related Articles :

Speak Your Mind

*