I have a VF page that inserts two records in a single page:

  1. Insert recipient__c object with required fields: school__c
  2. Insert scholarship_award__c object with required fields recipient__c, year__c, award__c, and school__c

Issue here is that when I set recipient__c to the Id of the previously inserted recipient__c record, it doesn't seem to work in referencing the newly inserted record.

I've tried switching around the insert statement so that I defined the variable first, however to no avail. It simply asks for required other fields which are inputted by the user.

public class c2{
    public scholarship_award__c sch {get;set;}
    public recipient__c rec {get;set;}
    public c2(){
        sch = new scholarship_award__c();
        rec = new recipient__c();
    public void saveScholarship(){
        insert rec;
        rec = new recipient__c();
        insert sch;
        sch = new scholarship_award__c(Recipient__c=rec.Id);

VF page

<apex:page showHeader="False" applyHtmlTag="true" applyBodyTag="false" controller="c2" lightningStyleSheets="True">
   <apex:slds >
    <body class="slds-scope">
    <apex:form >
        <apex:pageBlock title="Academic Year 2020-2021">
        <apex:pageBlockSection columns="2" title="Add Scholarships">
            <apex:inputField value="{!rec.Name}"/>
            <apex:inputField value="{!rec.Last_Name__c}"/>
            <apex:inputField value="{!rec.Preferred_Name__c}"/>
            <apex:inputField value="{!sch.School__c}"/>
            <apex:inputField value="{!sch.Year__c}"/>
            <apex:inputField value="{!sch.Award__c}"/>
            <apex:inputField value="{!rec.Email__c}"/>
            <apex:inputField value="{!rec.Specialty__c}"/>
            <apex:inputField value="{!rec.school__c}"/>
            <div align="center">
            <apex:inputField value="{!rec.Biography__c}"/>           
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton action="{!saveScholarship}" value="Submit"/>

1 Answer 1


You are resetting your rec variable after insert, which destroys the reference you need to the inserted object's Id:

    insert rec;
    rec = new recipient__c();

rec.Id is now null, because you erased the sObject that was in that variable.

    insert sch;
    sch = new scholarship_award__c(Recipient__c=rec.Id);

So you can't connect up the relationship here.

But further, you're populating the relationship on the new Scholarship_Award__c instance you're creating here, not the one you just inserted. You need to do an assignment before you insert the scholarship award.

So, two changes. Move the line

    rec = new recipient__c();

to the end of your method, and write an assignment statement:

 sch.Recipient__c = rec.Id;

after you insert the Recipient, and before you insert the Scholarship Award.

  • Wow thanks! I am actually an consultant and a beginner at VF/Apex so super helpful. Sep 23, 2020 at 14:55

You must log in to answer this question.

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