0

If I use the below code then it throws an error:

Review all error messages below to correct your data. Apex trigger abc caused an unexpected exception, contact your administrator: abc : execution of AfterInsert caused by: System.FinalException: Record is read-only: Class.xyz: line 93, column 1

if((trigger.isInsert || trigger.isUpdate) && trigger.isBefore)
{
    B2C_ServiceRoleService.encryptData(trigger.new); 
}
if(trigger.isinsert || trigger.isbefore){
    B2C_ServiceRoleService.copyAccountFields(trigger.new);
}

If I use below code then it works fine.

if((trigger.isInsert || trigger.isUpdate) && trigger.isBefore)
{
    B2C_ServiceRoleService.encryptData(trigger.new); 
    B2C_ServiceRoleService.copyAccountFields(trigger.new);
}
  • Value of trigger.isinsert = true in both scenarios
  • Value of trigger.isupdate = false in both scenarios
  • Value of trigger.isbefore = true in both scenarios

I am not able to figure out why it throws an error in the first case. I encounter the error when I create new records.

8
  • 1
    What is the error? Commented Mar 14, 2016 at 14:08
  • records should be created but getting below error: Review all error messages below to correct your data. Apex trigger abc caused an unexpected exception, contact your administrator: abc : execution of AfterInsert caused by: System.FinalException: Record is read-only: Class.xyz: line 93, column 1
    – Jack vardy
    Commented Mar 14, 2016 at 14:10
  • B2C_ServiceRoleService.copyAccountFields(trigger.new); runs when isInsert is true and isBefore is false. Commented Mar 14, 2016 at 14:16
  • Possible duplicate of System.FinalException: Record is read-only: Trigger.updateCompetitors: line 24 Commented Mar 14, 2016 at 14:18
  • even if it is isinsert= true or isbefore= true it is going inside method B2C_ServiceRoleService.copyAccountFields(trigger.new);
    – Jack vardy
    Commented Mar 14, 2016 at 14:21

3 Answers 3

2

I believe that problem is here:

if(trigger.isinsert || trigger.isbefore){
    B2C_ServiceRoleService.copyAccountFields(trigger.new);
}

Your condition means that this line of code will be executed for all insert and all before events, including before delete, which does not have a Trigger.new, which is giving you error I believe

EDIT

Based on your comment, I assume that you do some modification to these records(Trigger.new) in your method(copyAccountFields()), which is not allowed in an after trigger, which will stil be executed, because after insert does return true for insert OR before

3
  • But i am not doing any delete of records.I am trying to create a new records then these errors are coming.
    – Jack vardy
    Commented Mar 14, 2016 at 14:15
  • @RobertBarnett edited my answer
    – Novarg
    Commented Mar 14, 2016 at 14:16
  • I am not deleting any records.I am just creating a new record.@Novarg
    – Jack vardy
    Commented Mar 14, 2016 at 14:24
0

As already noted, when you evaluate trigger.isInsert || trigger.isBefore it is true for the following trigger states:

  • before insert
  • before update
  • before delete
  • after insert

This is why trigger handler patterns are more tidy. You can at least move your code to an evaluation like the following so that it is more explicit which events correspond to which actions:

if (trigger.isBefore)
{
    if (trigger.isInsert)
    {
        // handle beforeInsert
    }
    if (trigger.isUpdate)
    {
        // handle beforeUpdate
    }
}
if (trigger.isAfter)
{
    if (trigger.isInsert)
    {
        // handle afterInsert
    }
    if (trigger.isUpdate)
    {
        // handle afterUpdate
    }
}
-1

It should be like below then it will work fine.We need to use 'trigger.isinsert && trigger.isbefore' in 2nd if condition

if((trigger.isInsert || trigger.isUpdate) && trigger.isBefore)
    {
        B2C_ServiceRoleService.encryptData(trigger.new); 

    }
    if(trigger.isinsert && trigger.isbefore){
        B2C_ServiceRoleService.copyAccountFields(trigger.new);
    }

And problem was trigger.isinsert was true and hence it was going inside afterinsert part and as we know on afterinsert fields are readonly so it was not able to insert data into those fields so once we put && condition we made sure it is not going to afterinsert part of trigger.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .