+1 vote
13 views
by (163k points)

What I have in my custom controller is an assignment when a user inputs a value for a certain field, the field for its parent object will update as well.

For example.

User inputs the school for a scholarship recipient through <apex:inputField>. Both scholarship and recipient have required fields for the school__c field, so I need to update the school field on the scholarship record to be dynamically the same as the school that the user inputted for recipient__c

What I have attempted:

    public with sharing class regularController {
    
    public Scholarship_Award__c sch {get;set;}
    public Recipient__c rec {get;set;}
    
    //Constructor
    public regularController(){
    sch = new Scholarship_Award__c();
    rec = new Recipient__c();
    }
    
    public void saveScholarship(){
        insert rec;
        sch.Recipient__c = rec.Id;
        sch.School__c = rec.School__r;
        insert sch;
        sch = new scholarship_award__c(Recipient__c=rec.Id);
        rec = new recipient__c(School__c=sch.School__r);
    }
}

Which returns the error:

A non foreign key field cannot be referenced in a path expression: School__c

on line 18 which is the line of code that sets the school__c field on the recipient object to the value inputted in scholarship_award__c:

        rec = new recipient__c(School__c=sch.School__c.Id);

1 Answer

+1 vote
by (163k points)
 
Best answer

It's just __c. No need to go through the relationship:

rec = new recipient__c(School__c=sch.School__c);

If you actually needed a value from the related object, you'd use __r instead:

rec = new recipient__c(Name=sch.School__r.Name);
Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
...