You need to sign in to do that
Don't have an account?
venkat bojja
Execution of AfterUpdate caused by: System.FinalException: Record is read-only
Hi Team ,
I have wrote a trigger. When ever the record is updated I need update the pick list field which is Car_Type__c. But I'm gettng below error. Please let me know the why the error message is getting and solution for it.
Error Message : execution of AfterUpdate caused by: System.FinalException: Record is read-only
Triggger :
trigger DiscountonCarprice on V_Customers__c (after insert,after update) {
if(Trigger.isInsert && Trigger.isAfter ){
DiscountonCarpriceController.Discount(trigger.new);
}
else if(Trigger.isupdate && Trigger.isAfter ){
system.debug('Im in else block');
DiscountonCarpriceController.DiscountAfterUpdate(trigger.new,trigger.oldmap);
}
}
=================================
Apex Class :
public class DiscountonCarpriceController {
Public static void discount(list<V_Customers__c> vclist){
set<id> vcId = new set<id>();
list<V_Customers__c> updateVc = new list<V_Customers__c>();
for(V_Customers__c vcCust:vclist){
vcId.add(vcCust.id);
}
list<V_Customers__c> updateVcList = [select id,name,Country__c,Discount__c from V_Customers__c where id =: vcId];
for(V_Customers__c vc: updateVcList){
if(vc.Country__c=='INDIA'){
vc.Discount__c = 20;
//updateVc.add(vc);
}
else if(vc.Country__c=='UK'){
vc.Discount__c = 15;
// updateVc.add(vc);
}
else if(vc.Country__c=='USA'){
vc.Discount__c = 10;
// updateVc.add(vc);
}
}
update updateVcList;
}
Public static void DiscountAfterUpdate(list<V_Customers__c> vclist,map<id,V_Customers__c> vcoldMap){
set<id> vcId = new set<id>();
for(V_Customers__c vcCust:vclist){
vcId.add(vcCust.id);
}
system.debug('vclist****'+vclist);
system.debug('vcoldMap****'+vcoldMap);
list<V_Customers__c> vclist1 = new list<V_Customers__c>();
list<V_Customers__c> vclist2 = [select id,name,Country__c,Discount__c,Car_Type__c from V_Customers__c where Id IN :vcId];
for(V_Customers__c vc:vclist){
if(vcoldMap.get(vc.id).id == vc.id ){
vc.Car_Type__c = 'Hatchback';
vclist1.add(vc);
system.debug('Discount Amount***'+ vc.Discount__c);
}
}
system.debug('vclist1***'+ vclist1);
update vclist1;
system.debug('vclist1***'+ vclist1);
}
}
=============================
Error Screenshot:
FYI:
I have wrote a trigger. When ever the record is updated I need update the pick list field which is Car_Type__c. But I'm gettng below error. Please let me know the why the error message is getting and solution for it.
Error Message : execution of AfterUpdate caused by: System.FinalException: Record is read-only
Triggger :
trigger DiscountonCarprice on V_Customers__c (after insert,after update) {
if(Trigger.isInsert && Trigger.isAfter ){
DiscountonCarpriceController.Discount(trigger.new);
}
else if(Trigger.isupdate && Trigger.isAfter ){
system.debug('Im in else block');
DiscountonCarpriceController.DiscountAfterUpdate(trigger.new,trigger.oldmap);
}
}
=================================
Apex Class :
public class DiscountonCarpriceController {
Public static void discount(list<V_Customers__c> vclist){
set<id> vcId = new set<id>();
list<V_Customers__c> updateVc = new list<V_Customers__c>();
for(V_Customers__c vcCust:vclist){
vcId.add(vcCust.id);
}
list<V_Customers__c> updateVcList = [select id,name,Country__c,Discount__c from V_Customers__c where id =: vcId];
for(V_Customers__c vc: updateVcList){
if(vc.Country__c=='INDIA'){
vc.Discount__c = 20;
//updateVc.add(vc);
}
else if(vc.Country__c=='UK'){
vc.Discount__c = 15;
// updateVc.add(vc);
}
else if(vc.Country__c=='USA'){
vc.Discount__c = 10;
// updateVc.add(vc);
}
}
update updateVcList;
}
Public static void DiscountAfterUpdate(list<V_Customers__c> vclist,map<id,V_Customers__c> vcoldMap){
set<id> vcId = new set<id>();
for(V_Customers__c vcCust:vclist){
vcId.add(vcCust.id);
}
system.debug('vclist****'+vclist);
system.debug('vcoldMap****'+vcoldMap);
list<V_Customers__c> vclist1 = new list<V_Customers__c>();
list<V_Customers__c> vclist2 = [select id,name,Country__c,Discount__c,Car_Type__c from V_Customers__c where Id IN :vcId];
for(V_Customers__c vc:vclist){
if(vcoldMap.get(vc.id).id == vc.id ){
vc.Car_Type__c = 'Hatchback';
vclist1.add(vc);
system.debug('Discount Amount***'+ vc.Discount__c);
}
}
system.debug('vclist1***'+ vclist1);
update vclist1;
system.debug('vclist1***'+ vclist1);
}
}
=============================
Error Screenshot:
FYI:
Do it on before update not on after update.
and if you still want to do it on after update create new list and records on that list and do DML like
Example:
All Answers
Do it on before update not on after update.
and if you still want to do it on after update create new list and records on that list and do DML like
Example:
On before update it should be something like this.