+ Start a Discussion
KeithlarsKeithlars 

Trigger error that record is read only

I'm trying to copy the values from one multiselect picklist to another on the lead object and I get and error that the record is read only, yet I can update the record on the form. What am I doing wrong?

Record is read-only: Trigger.Area_of_Interest: line 21, column 9 which is this line:
   a.area_of_interest__c = entries.get(a.id).area_of_interest_landing__c;

trigger Area_of_Interest on Lead (after update) {

    // Set up list to hold unique IDs of all new or updated leads
    Set<Id> leadIds = New Set<Id>();
   
    // Set up list to hold unique area of interests
    Set<String> interests = new Set<String>();
   
    // Iterate through array of new or updated records
    for (Lead a : trigger.new){
        if (a.area_of_interest_landing__c != '')
        leadIds.add(a.id);
    }
 
    // Map the area of interest field values to ID
    Map<ID, Lead> entries = new Map<Id, Lead>(
        [select area_of_interest_landing__c, area_of_interest__c from Lead where ID in : leadIds]); 
   
   
     for (Lead a : trigger.new){
        a.area_of_interest__c = entries.get(a.id).area_of_interest_landing__c;
        update a;
     } 
 }
werewolfwerewolf
My guess would be it's because you're doing it in an after trigger.  Try it in a before update trigger instead perhaps
JimRaeJimRae
I would agree with werewolf, use a before trigger instead.  Also, note that you wouldn't need the update statement in that scenario, since the changes to the records would be in process when the trigger fired.
 
 
Code:
trigger Area_of_Interest on Lead (before update) {

    // Iterate through array of new or updated records
    for (Lead a : trigger.new){ 
        if (a.area_of_interest_landing__c != '')
           a.area_of_interest__c = a.area_of_interest_landing__c;

    }
  
 }

 
KeithlarsKeithlars
No that didn't change anything.
JimRaeJimRae
Did you verify that the user inserting the lead has write access to the field?