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
alynlinalynlin 

Error: Field is not writeable--for Master-Detail relationship.

Hi all.

 

I am writing an apex trigger,

 

trigger LatestEvaluationHistory on Evaluation_History__c (after delete, after insert,
 after undelete, after update) {

// this is the child object.

 

 

 

//then I want to update the parant object when the child is delete/insert/update

List<Job_Evaluation__c> dateToUpdate = new List <Job_Evaluation__c>{};

 

 

}

 

then I got the error:  Field is not writeable

 

 

Any body can help me on this? cause I am totally new in apex :)

 

 

alynlinalynlin

trigger LatestEvaluationHistory on Evaluation_History__c (after delete, after insert,
 after undelete, after update) {
 if(!test1.contextTriggerExecuting){ //check that the trigger isnt already executing
     test1.contextTriggerExecuting = true;  //set to true so it doesnt go in a recursive loop  
  Set<Id> emtoupdate = new Set<Id> ();
  
  if (Trigger.isInsert || Trigger.isUpdate ) {
   for (Evaluation_History__c c: Trigger.New) {
    emtoupdate.add(c.Job_Name__c);
   }
  }
  
  if (Trigger.isDelete) {
   for (Evaluation_History__c c: Trigger.Old) {
    emtoupdate.add(c.Job_Name__c);
   }
  }
  
  List<Evaluation_History__c> ctxsToUpdate = new List<Evaluation_History__c>{};
  boolean firstRecord = true; 
  
  for(Evaluation_History__c ctx : [Select Id, Most_Recent_Record__c, Effective_Date__c
   from Evaluation_History__c where Job_Name__c =: emtoupdate ORDER BY Effective_Date__c DESC])
   {
    if(firstRecord)
    {
     ctx.Most_Recent_Record__c = true;
     firstRecord = false; 
    }
    else
     ctx.Most_Recent_Record__c = false; 
     
    ctxsToUpdate.add(ctx);
   }
  if(ctxsToUpdate != null && !ctxsToUpdate.IsEmpty())
  update ctxsToUpdate;
  
  
  List<Job_Evaluation__c> dateToUpdate = new List <Job_Evaluation__c>{};
  
  for (Job_Evaluation__c dateTo : [Select LatestEvaluationDate__c From Job_Evaluation__c
   where Name =: emtoupdate])
   {
  
  for (Evaluation_History__c dateFrom : [Select Effective_Date__c from Evaluation_History__c
   where Job_Name__c =: emtoupdate and Most_Recent_Record__c = TRUE])
   {
    dateTo.LatestEvaluationDate__c = dateFrom.Effective_Date__c;
   }
      
   }

}

}

 

// this is my code.  and the last few lines got error.

Shashikant SharmaShashikant Sharma

Yes You can not update a master reference in master detail relationship. thats not allowed in salesforce , either you have to delete this record and create new one LIke

get a clone of cuurent

 

DetailObject__c objInstanceNew = objInstanceOld.clone(false);  //This will remove id from this record

objInstanceNew.MasterRefFields__c = 'NewRefID"; // Add id of new master

insert objInstanceNew;

delete objInstanceOld;

 

Do it for bulk using collections.

 

Easier way is to use lookup relationship, then you can modfy references.

alynlinalynlin

Thanks.

 

So, when writing trigger on the child object, can read-only its parent object?

 

And, may I ask, if I am writing a trigger on the parent object. Can I read or write it's child object?

 

 

Shashikant SharmaShashikant Sharma

No read only is only the  reference field on detail (child obj) , if relationship is master detail,

 

Like if you have 

Object1

Object2 

Object2 has a Field referenceToObject1 Master detail relationship field

 

then referenceToObject__c field can only be set in an insert call. It can not be modified later. If you write any statement like

 

obj2.referenceToObject1 = 'newID';

it will give error Field is not writeable--for Master-Detail relationship if obj2.id != null bcoz obj.id != null means that object has been inserted earlier thats why it has id.

 

Other than Master Detail reference You can always change other field values.

Jaap BranderhorstJaap Branderhorst

If you allow reparenting on the master-detail relationship the id is writeable.