You need to sign in to do that
Don't have an account?
Mike @ 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; } }
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
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
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
could you justsay wat is ur req.I can help you out with the code
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
I think you should change the properties on 'before' triggers, not 'after'
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
Worked Great! Thanks for the help everyone :)
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.