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
Gaurav  PuranikGaurav Puranik 

Update the field on the same object for after Insert event Trigger

Hi all,

There is one object let say Account. In Account I have 3 fields, Amount1, Amount2,Amount3. Now when I enter value in Amount1 at the time of creation of account record. Account2 field value get updated by (Amount1+10), that I am doing by before Insert event.

Now I want to update Amount3 field of same record let say by Amount2+40, so for this case I have to go by after insert event. I am doing the same by after insert event but it is throughing an error 'Feild is read only'. 
Best Answer chosen by Gaurav Puranik
Arunkumar RArunkumar R
Hi Gaurav,
   
     You cannot change Trigger.new value on After event. Don't go for after insert, you can achieve in before insert trigger. 

If user won't change amount 2 and amount 3 field values, then go for formula field. Please find the below sample trigger code,
 
trigger AccountTrigger on Account(before insert)
{
     for(Account currAcc: Trigger.new)
	 {
		if(currAcc.Amount1__c != null)
		{
			currAcc.Amount2__c = currAcc.Amount1__c + 10;
			currAcc.Amount3__c = currAcc.Amount2__c + 40;
		}		
	 }
	 
}

 

All Answers

Arunkumar RArunkumar R
Hi Gaurav,
   
     You cannot change Trigger.new value on After event. Don't go for after insert, you can achieve in before insert trigger. 

If user won't change amount 2 and amount 3 field values, then go for formula field. Please find the below sample trigger code,
 
trigger AccountTrigger on Account(before insert)
{
     for(Account currAcc: Trigger.new)
	 {
		if(currAcc.Amount1__c != null)
		{
			currAcc.Amount2__c = currAcc.Amount1__c + 10;
			currAcc.Amount3__c = currAcc.Amount2__c + 40;
		}		
	 }
	 
}

 
This was selected as the best answer
cvuyyurucvuyyuru
Why can't you do it in the same Before Insert trigger.

amount2 = amount1+10;
amount3 = amount2+10;
 
Aravind RAravind R
Hi,

You can achieve this by two ways:

Create formula fields for amount2 and amount3(Preferred) or

Set Amount3 = Amount2+40 is nothing but 
(Amount1+10)+40