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
wilbur07wilbur07 

How do I assign value to a parent-child field?

I have this code which gives an error when I try to compile my trigger:

for (Asset a : Trigger.new) {
    if (Trigger.isInsert) {
        AssetHistory__c nli0 = new AssetHistory__c();
        nli0.Name='Created';
        nli0.Asset__r.Id=a.Id;
        insert nli0;
    }
    else {

the error is Error: Compile Error: Field is not writeable: Asset__r.Id at line 8 column 9

I tried this replacement which compiles but does not assign any value to the parent field Asset__r when the trigger executes.

nli0.Asset__r=a;

and these lines all give cannot write errors, or field not in AssetHistory__c object:

nli0.Asset__rId = a.Id;
nli0.AssetId = a.Id;

My question is, how do I assign a parent Asset to my AssetHistory__c objects?

Any help is appreciated.
wilbur07wilbur07
Asset__r should be Asset__c as in Asset__c=a.id -- works fine now.