0

I have a custom object where I have a related list. I created a before insert before update trigger checking if there is any value in related list or not but when I try to write a test class for it and insert values in related list it gives following error message

Field is not writeable:

Note: Activity is related list of dev plan object

 lmsilt__Activity__c act =new lmsilt__Activity__c();
    act.Name='Test_Activity';
    insert act;
    lmsilt__Development_Plan__c  devplan = new lmsilt__Development_Plan__c();
    devplan.Name='Mydev Plan';
    devplan.lmsilt__Activities__r=act;
    insert devplan;

Error is shown for this line devplan.lmsilt__Activities__r=act;

5
  • Error indicates you are trying to put the value for read only field. you can not defined value for any read only field like formula field. check the line no. on which you getting error and make sure field is not ready only.
    – Amol
    Dec 8, 2017 at 10:49
  • I am adding value to related list not a field. Let me update the question to include code in question.
    – Ishan
    Dec 8, 2017 at 11:00
  • Is the relationship here master-detail?
    – TSmith
    Dec 8, 2017 at 11:45
  • Is Act Master of DevPlan? If yes, I believe you are updating the wrong field. You should not use the relationship field for this.
    – m Peixoto
    Dec 8, 2017 at 12:25
  • No, they aren't directly related. I managed to do this by giving Dev Plan Id to Activity. Like this lmsilt__Development_Plan__c devplan = new lmsilt__Development_Plan__c(); devplan.Name='Mydev Plan'; insert devplan; lmsilt__Activity__c act =new lmsilt__Activity__c(); act.Name='Test_Activity'; act.lmsilt__Development_Plan__c=devplan.Id; insert act;
    – Ishan
    Dec 11, 2017 at 8:31

1 Answer 1

1

As you said, the problem is on this line:

devplan.lmsilt__Activities__r=act;

Try changing __r to __c and act to it's Id. For example:

devplan.lmsilt__Activities__c = act.Id;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .