I'm working on custom workflow field update I have taken the value to update as input field and by using the map<> method I'm getting the api name of the field and storing it in a record Now I need to take dynamically the field to update and the value to update from the records of the custom object in which I have save them for eg Suppose the Field_to_Update__c is "department" & Value_to_Update__c is "dept" I've save them in field as


In the above line objRuleFieldUpdate is instance of junction object Field_Udpate__r is the custom object. I'm taking the relation to access its field. The trigger is saved without error Now when I try to create a record in contact matching the workflow criteria it give the error as below on the above line

Apex trigger ContactFieldUpdate caused an unexpected exception, contact your administrator: ContactFieldUpdate: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.ContactFieldUpdate

how can I achieve this functionality of taking the field to update dynamically and value to update dynamically and then assign the value to the field.


I agree I've check the code & that's not working if the new record is created. But I have already created the records i'm fetching the records from the database. I'm taking the records that meet the criteria in the list & then I'm updating the field

List<Object1__c> lstOfObject = [ SELECT  Id, Object__c, Rule_Evaluation_Criteria__c, Status__c,
                                      (SELECT Id, Object2__r.Name, Object2__r.Object__c, Object2__r.Field__c,
                                       Object2__r.Operator__c, Object2__r.Value__c FROM Object1_Object2_JO__r),
                                      (SELECT Id, Object3__r.Rule_Name__c, Object3__r.Object_Name__c,
                                       Object3__r.Field_to_Update__c, Object3__r.Value_to_Update__c 
                                       FROM Object1_Object2_JO__r)
                                      FROM Object1__c
                                      WHERE Object__c = 'Contact' AND Rule_Evaluation_Criteria__c = 'Record is    
                                      Created' AND Status__c = 'Active'

This is the query that I'm using to get the records using the junction objects I've created a code if else condition to match the criteria that is entered

Once the criteria is met a field will be update so this is the code I'm using to update the field

for(Object1_Object3_JO__c objObject1Object3JO : objObject1.objObject1Object3JO __r)

                        //String fieldtoupdate = String.valueOf(a.get(objRuleFieldUpdate.Field_Udpate__r.Field_to_Update__c));
                        //Schema.DescribeFieldResult field1=mapFields.get(objRuleFieldUpdate.Field_Udpate__r.Field_to_Update__c).getDescribe();
                        //a.get(field1.getName()) = '';

                        //Schema.DescribeFieldResult field1 = mapFields.get(objRuleFieldUpdate.Field_Update__r.Field_to_Update__c).getDescribe();
                        //a.Field_to_Update__c = objRuleFieldUpdate.Value_to_Update__c;

isInCriteria is the flag that will get true once the criteria is met


This variable stores the name of the field to update


This variable stores the value to assign to that field


rYou can not update fields in after update trigger please use before update. Your question is duplicate of this link. System.FinalException: Record is read-only: Trigger.updateCompetitors: line 24


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.