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

any clue for this exception

Hi Friends

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger MyContactTrigger caused an unexpected exception, contact your administrator: MyContactTrigger: execution of AfterUpdate caused by: System.Exception: Record is read-only:

while Contact has full rites ,I m not able to update Email Field through this code

trigger MyContactTrigger on Contact (after insert , after update) {
   if((Trigger.isInsert) || (Trigger.isUpdate))
     set <Id> OwnerId = new set <Id>();
    for (Contact c
           User[] user = [Select Email from User where Id in: OwnerId];   
           string Email = user[0].Email;
           //  c.Description = Email; 
           if(c.Email== null)
           c.Email = '';
           c.Email = Email;
           //Update c;
You can't change the values in after triggers. You need to put this into the before trigger events.

You shouldn't get that error if you change this line:
trigger MyContactTrigger on Contact (after insert , after update)

to this:
trigger MyContactTrigger on Contact (before insert , before update)

Also you have placed a query inside the loop of your array which means you will get caught by the governor limit on SOQL statements if your trigger ever sees 21 or more contacts (i.e. in an import wizard or API operation)

You should create a map of Id => List<Contact> where the Id is the ownerId and then use the keyset of this map in a single query to get all the user records for all the contact owners in the array using the "IN" operator:

select email from user where Id IN :ownerMap.keySet()

Iterate over that result and for each dip into your map to get the set of contacts, iterate through that and set the email appropriately.