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
Mike @ BlackTabMike @ BlackTab 

Apex Trigger Error: Record is Read-Only

I'm getting this error when I try to update a contact record:

 

Review all error messages below to correct your data.
Apex trigger ContactStageTrigger caused an unexpected exception, contact your administrator: ContactStageTrigger: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.ContactStageTrigger: line 6, column 1

 

Here is my Code:

 

trigger ContactStageTrigger on Contact (after insert, after update) {

    Contact myContact = trigger.new[0];
    Account myAccount = [SELECT OwnerId FROM Account WHERE Id = :myContact.AccountId];
    if(myContact.Status__c == 'Prospect'){
        myContact.OwnerId = myAccount.OwnerId;
        update myContact;
    }
 }

 


Best Answer chosen by Admin (Salesforce Developers) 
trictric

Hi ,

 

Try below given code.It should work if it soesn;t it should bring you very close to what you are trying to accomplish.

Since you will be using  before update as well.You will need to check for values in the trigger,old and trigger.new context variable .if the values differe in old and new variable only then your update should actually happen.  

 

trigger ContactStageTrigger on Contact (before insert, before update) {

set<id> soap=new set<id>();

for(contact c:trigger.new)
{

soap.add(c.accountid);

}

map<id,account>must=new map<id,account>([select id,ownerid from account where id in:soap]);

for(contact g:trigger.new)
{
if(g.status__c=='Prospect')
{

g.ownerid=must.get(g.accountid).ownerid;

System.debug('The value in thge g.ownerid is'+g.ownerid);

}
}

}

 

Thanks,

Tric

All Answers

steve456steve456

You cannot do this way

 

You writing the trigger on Contact 

 

and you are updating the contact

 

use before insert,before update rather than after insert ,after update

Mike @ BlackTabMike @ BlackTab

I changed it to "before insert, before update" and now i'm getting a different error:

 

Apex trigger ContactStageTrigger caused an unexpected exception, contact your administrator: ContactStageTrigger: execution of BeforeUpdate caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.ContactStageTrigger: line 7, column 1

steve456steve456

could you justsay wat is ur req.I can help you out with the code

Mike @ BlackTabMike @ BlackTab

Basically this is what i'm trying to do:

 

If Status__c changes from "Suspect" to "Prospect" Then the Contact's OwnerId = the Account's OwnerId

SeAlVa_VFTabulatorSeAlVa_VFTabulator

I think you should change the properties on 'before' triggers, not 'after'

trictric

Hi ,

 

Try below given code.It should work if it soesn;t it should bring you very close to what you are trying to accomplish.

Since you will be using  before update as well.You will need to check for values in the trigger,old and trigger.new context variable .if the values differe in old and new variable only then your update should actually happen.  

 

trigger ContactStageTrigger on Contact (before insert, before update) {

set<id> soap=new set<id>();

for(contact c:trigger.new)
{

soap.add(c.accountid);

}

map<id,account>must=new map<id,account>([select id,ownerid from account where id in:soap]);

for(contact g:trigger.new)
{
if(g.status__c=='Prospect')
{

g.ownerid=must.get(g.accountid).ownerid;

System.debug('The value in thge g.ownerid is'+g.ownerid);

}
}

}

 

Thanks,

Tric

This was selected as the best answer
Mike @ BlackTabMike @ BlackTab

Worked Great! Thanks for the help everyone :)

Pranay DhapodkarPranay Dhapodkar
can anyone tell me why that read only error shows?
manu manu 23manu manu 23
Hi pranay,
The read only error is due to after update/ after insert event. Generally after triggers are used to update/insert other records but not the same record. 
I see that code was written on the contact object and( it's after trigger). In after triggers,  the trigger code gets executed only when the record is updated/inserted with certain values. 
in the above code, 
he was trying to update the record again, wchich was already updated in the temporary database.

It would be appropriate if it was before update. 
NOTE: If you have to update the same object record (the trigger written on) then use before triggers.