+1 vote
35 views
by (163k points)

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">
<head>
   <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}"/>
        </apex:pageBlockSection>      
            <div align="center">
            <apex:inputField value="{!rec.Biography__c}"/>           
            </div>
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton action="{!saveScholarship}" value="Submit"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
    </body>
   </apex:slds>
</head> 
</apex:page>

1 Answer

Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
...