0

I have an <apex:form> that gathers user input for various fields on the recipient__c object and a custom controller that inserts the new record. Further in the controller I have another object scholarship_award__c where I set the value of child fields of recipient__c to be the same value that the user inputs.

When I save the record in the VF page, it only inserts the child fields I set and not the rest of the user inputted values.

Controller

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(recipient__c=rec.Id);
rec = new Recipient__c(School__c=sch.School__c);
}

public void saveScholarship(){
    rec = new recipient__c(School__c=sch.School__c);
    insert rec;
    sch.Recipient__c = rec.Id;  
    insert sch;
    sch = new scholarship_award__c(recipient__c=rec.Id);
}

}

VF Page

<apex:page controller="regularController" extensions="testController" lightningStylesheets="true">
<apex:form>
<apex:pageBlock >
    <!--Recipient form-->
<apex:pageBlockSection title="Add recipients" columns="2">
    <apex:inputField value="{!rec.Name}"/>
    <apex:inputField value="{!rec.Last_Name__c}"/>
    <apex:inputField value="{!rec.Preferred_Name__c}"/>
    <apex:inputField value="{!rec.Email__c}"/>
    <apex:inputField value="{!sch.Award__c}"/>
    <apex:inputField value="{!sch.School__c}"/>
    <apex:inputField value="{!rec.School__c}" rendered="false"/>
    <apex:inputField value="{!sch.Year__c}"/>
    <apex:inputField value="{!rec.Specialty__c}"/>
</apex:pageBlockSection>
    <apex:pageBlockSection title="Biography">
    <apex:outputLabel >Upload or enter student biography/CV</apex:outputLabel>
    <apex:inputFile id="myFile" value="{!myDoc.Body}" fileName="{!myDoc.Name}">
    </apex:inputFile>
        <apex:inputTextarea value="{!rec.Biography__c}" rows="3" cols="100"/>
</apex:pageBlockSection>
    
<!--Record list-->     

    
<!--Submit all records in record list-->
<apex:pageBlockButtons location="bottom">
    <apex:commandButton action="{!saveScholarship}" value="Submit"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>    

</apex:page>

Where only

    <apex:inputField value="{!rec.School__c}" rendered="false"/>
    <apex:inputField value="{!sch.Year__c}"/>
    <apex:inputField value="{!sch.Award__c}"/>

Are saving in the record in the related list and none of the other recipient fields are populated with the user inputted info.

Why might this be?

1 Answer 1

2

It looks like you are overriding the user inputted values by creating a new instance.

public void saveScholarship(){
    rec = new recipient__c(School__c=sch.School__c); // This is a  problem 
    insert rec;
    sch.Recipient__c = rec.Id;  
    insert sch;
    sch = new scholarship_award__c(recipient__c=rec.Id);
}

If you want to keep the value but also provide the value for School__c, you need a minor changes

public void saveScholarship(){
    rec.School__c=sch.School__c; 
    insert rec;
    sch.Recipient__c = rec.Id;  
    insert sch;
    sch = new scholarship_award__c(recipient__c=rec.Id);
}

Update, if you look at your constructor, you are putting rec value in sch and then sch in rec which would be null. Either you need to query some data an populate them or instantiate them to prevent a null pointer exception.

public regularController(){
sch = new Scholarship_Award__c();
rec = new Recipient__c();
}
2
  • Thank you but updated code is now returning Attempt to de-reference a null object error on launching VF page Commented Sep 29, 2020 at 20:05
  • Edit: Removed rec = new recipient__c declarations on my saveScholarship method and it works! Commented Sep 29, 2020 at 20:10

You must log in to answer this question.

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