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
Gary Stark 3Gary Stark 3 

Visualforce page is not saving record into object after clicking save

Hi There,

I've created a visualforce page based on a custom object and an extension which enables this to be opened from the account screen. 

Whenever I click "Save", I am redirected back to the account I invoked the visualforce page, however the record I created on the visualforce doesn't appear to be saving. 

 VF page:
<apex:page StandardController="Account" extensions="accountProp" sidebar="false">
    <apex:sectionHeader title="Edit AccountProperty" />
    <apex:form >
        <apex:pageBlock title="Edit AccountProperty" id="thePageBlock" mode="edit">
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
            <apex:actionRegion >
                <apex:pageBlockSection title="Property Information" columns="1">
                    
                    <apex:pageBlockSectionItem >                       
                        <apex:outputLabel value="Property_Name__c"/>
                        <apex:outputPanel >                            
                            <apex:inputField value="{!objB.Property_Name__c}">
                                <apex:actionSupport event="onchange" rerender="thePageBlock"
                                                    status="status"/>
                            </apex:inputField>
                            <apex:actionStatus startText="applying value..." id="status"/>
                        </apex:outputPanel>
                    </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
            </apex:actionRegion>
            <apex:pageBlockSection title="SVU Channel" columns="1"
                                   rendered="{!objB.Property_Name__c == 'Sunrise ID'}">
                <apex:inputField value="{!ObjB.Account__c}" required="true"/>
                <apex:inputField value="{!objB.Sunrise_ID__c}" required="true"/>
                <apex:inputField value="{!objB.Start_Date__c}" required="true"/>
                <apex:inputField value="{!objB.Implementation_Date__c}" required="true"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="SVU Channel" columns="1"
                                   rendered="{!objB.Property_Name__c == 'SVU Site ID'}">
                <apex:inputField value="{!objB.Account__c}" required="true"/>
                <apex:inputField value="{!objB.SVU_SITE_ID__c}" required="true"/>
                <apex:inputField value="{!objB.Start_Date__c}" required="true"/>
                <apex:inputField value="{!objB.Implementation_Date__c}" required="true"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:

public with sharing class accountProperty {
    
    ApexPages.StandardController sc;
    public Account_Property__c objB {get; set;}
    
    public accountProperty(ApexPages.StandardController sc)
    {
        this.sc = sc;
        objB = new Account_Property__c();
    }
    
    public ApexPages.PageReference SaveBoth()
    {
        insert objB;
        return sc.Save();
    }

}
Best Answer chosen by Gary Stark 3
Amit Chaudhary 8Amit Chaudhary 8
I hope you can want to set same account on Account_Property__c

Update your code like below
public with sharing class accountProperty {
    
    ApexPages.StandardController sc;
    public Account_Property__c objB {get; set;}
    
    public accountProperty(ApexPages.StandardController sc)
    {
        this.sc = sc;
        objB = new Account_Property__c();
		ObjB.Account__c = sc.id;
    }
    
    public ApexPages.PageReference SaveBoth()
    {
        insert objB;
		return new PageReference('/'+objB.id)
    }

}
<apex:page StandardController="Account" extensions="accountProp" sidebar="false">
    <apex:sectionHeader title="Edit AccountProperty" />
    <apex:form >
        <apex:pageBlock title="Edit AccountProperty" id="thePageBlock" mode="edit">
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!SaveBoth}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
            <apex:actionRegion >
                <apex:pageBlockSection title="Property Information" columns="1">
                    
                    <apex:pageBlockSectionItem >                       
                        <apex:outputLabel value="Property_Name__c"/>
                        <apex:outputPanel >                            
                            <apex:inputField value="{!objB.Property_Name__c}">
                                <apex:actionSupport event="onchange" rerender="thePageBlock"
                                                    status="status"/>
                            </apex:inputField>
                            <apex:actionStatus startText="applying value..." id="status"/>
                        </apex:outputPanel>
                    </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
            </apex:actionRegion>
            <apex:pageBlockSection title="SVU Channel" columns="1"
                                   rendered="{!objB.Property_Name__c == 'Sunrise ID'}">
                <apex:inputField value="{!ObjB.Account__c}" required="true"/>
                <apex:inputField value="{!objB.Sunrise_ID__c}" required="true"/>
                <apex:inputField value="{!objB.Start_Date__c}" required="true"/>
                <apex:inputField value="{!objB.Implementation_Date__c}" required="true"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="SVU Channel" columns="1"
                                   rendered="{!objB.Property_Name__c == 'SVU Site ID'}">
                <apex:inputField value="{!objB.Account__c}" required="true"/>
                <apex:inputField value="{!objB.SVU_SITE_ID__c}" required="true"/>
                <apex:inputField value="{!objB.Start_Date__c}" required="true"/>
                <apex:inputField value="{!objB.Implementation_Date__c}" required="true"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>


Let us know if this will help you

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Add Apex:messages to see error

<apex:page StandardController="Account" extensions="accountProp" sidebar="false">
    <apex:sectionHeader title="Edit AccountProperty" />
    <apex:form >
 <apex:messages />
        <apex:pageBlock title="Edit AccountProperty" id="thePageBlock" mode="edit">
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
            <apex:actionRegion >
                <apex:pageBlockSection title="Property Information" columns="1">
                    
                    <apex:pageBlockSectionItem >                       
                        <apex:outputLabel value="Property_Name__c"/>
                        <apex:outputPanel >                            
                            <apex:inputField value="{!objB.Property_Name__c}">
                                <apex:actionSupport event="onchange" rerender="thePageBlock"
                                                    status="status"/>
                            </apex:inputField>
                            <apex:actionStatus startText="applying value..." id="status"/>
                        </apex:outputPanel>
                    </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
            </apex:actionRegion>
            <apex:pageBlockSection title="SVU Channel" columns="1"
                                   rendered="{!objB.Property_Name__c == 'Sunrise ID'}">
                <apex:inputField value="{!ObjB.Account__c}" required="true"/>
                <apex:inputField value="{!objB.Sunrise_ID__c}" required="true"/>
                <apex:inputField value="{!objB.Start_Date__c}" required="true"/>
                <apex:inputField value="{!objB.Implementation_Date__c}" required="true"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="SVU Channel" columns="1"
                                   rendered="{!objB.Property_Name__c == 'SVU Site ID'}">
                <apex:inputField value="{!objB.Account__c}" required="true"/>
                <apex:inputField value="{!objB.SVU_SITE_ID__c}" required="true"/>
                <apex:inputField value="{!objB.Start_Date__c}" required="true"/>
                <apex:inputField value="{!objB.Implementation_Date__c}" required="true"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

I hope this will help you
 
Gary Stark 3Gary Stark 3
Hi Amit, I just tried this and nothing happened. The page saves although no record is created for the relevant account. No error message, just straight back to the account as if nothing happaned. 
Akshay_DhimanAkshay_Dhiman
Hi Gary Stark 3,
You should replace action name in commandButton because in your ext controller 

<apex:pageBlockButtons>
 <apex:commandButton value="Save" action="{!SaveBoth}"/>
 <apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>

if you found this answer helpful then please mark it as best answer so it can help others.   

Thanks 
Akshay
Gary Stark 3Gary Stark 3
Still no luck. I found that when I test this with the "Preview" button in the developer console, i get the error message "Account Name: Required fields are missing: [Account Name]. Although, this doesn't make sense though, because the <apex:inputField value="{!objB.Account__c}" required="true"/> element is associated with the account master detail field and should fill the account name appropriately on save. 
 
Amit Chaudhary 8Amit Chaudhary 8
It look like you are using StandardController="Account"  but try to insert the Account_Property__c record. And in ActioButton you are using standard function Save not SaveBoth.

Look like you need to call SaveBoth function
Amit Chaudhary 8Amit Chaudhary 8
You Need to add one more text field for account name like below
<apex:page StandardController="Account" extensions="accountProp" sidebar="false">
    <apex:sectionHeader title="Edit AccountProperty" />
    <apex:form >
 <apex:messages />
        <apex:pageBlock title="Edit AccountProperty" id="thePageBlock" mode="edit">
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
            <apex:actionRegion >
                <apex:pageBlockSection title="Property Information" columns="1">
                    
                    <apex:pageBlockSectionItem >                       
                        <apex:outputLabel value="Property_Name__c"/>
                        <apex:outputPanel >                            
                            <apex:inputField value="{!objB.Property_Name__c}">
                                <apex:actionSupport event="onchange" rerender="thePageBlock"
                                                    status="status"/>
                            </apex:inputField>
                            <apex:actionStatus startText="applying value..." id="status"/>
                        </apex:outputPanel>
                    </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
            </apex:actionRegion>
            <apex:pageBlockSection title="SVU Channel" columns="1"
                                   rendered="{!objB.Property_Name__c == 'Sunrise ID'}">
                <apex:inputField value="{!ObjB.Account__c}" required="true"/>
                <apex:inputField value="{!objB.Sunrise_ID__c}" required="true"/>
                <apex:inputField value="{!objB.Start_Date__c}" required="true"/>
                <apex:inputField value="{!objB.Implementation_Date__c}" required="true"/>
                <apex:inputField value="{!Account.Name}"  />
            </apex:pageBlockSection>
            <apex:pageBlockSection title="SVU Channel" columns="1"
                                   rendered="{!objB.Property_Name__c == 'SVU Site ID'}">
                <apex:inputField value="{!objB.Account__c}" required="true"/>
                <apex:inputField value="{!objB.SVU_SITE_ID__c}" required="true"/>
                <apex:inputField value="{!objB.Start_Date__c}" required="true"/>
                <apex:inputField value="{!objB.Implementation_Date__c}" required="true"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Gary Stark 3Gary Stark 3
This has resolved the issue with the error message from the preview, but when I open the visualforce page from an account and save. Nothing saves to the Account_Property__c object. 
Gary Stark 3Gary Stark 3
Actually I just got this to work with your suggestion Amit. However I don't want the Account Name field to display as this seems to overwrite the account name from where the account property is being created from. is there way we can set this value in the controller and hide the field in the visualforce page?
Amit Chaudhary 8Amit Chaudhary 8
You want to insert Account record or only Account_Property__c  record ? or both ?
Gary Stark 3Gary Stark 3
Just the Account_Property__c record. 
Gary Stark 3Gary Stark 3
If the Account__c is the master detail relationship field, shouldn't this just create the record and associate the Account Name based on this. I'm effectively selecting the account I want the record to go against, so why would I need to insert an account?
Amit Chaudhary 8Amit Chaudhary 8
This button you added on account page layout ?
Gary Stark 3Gary Stark 3
Yes, this is on the Account page layout
Amit Chaudhary 8Amit Chaudhary 8
I hope you can want to set same account on Account_Property__c

Update your code like below
public with sharing class accountProperty {
    
    ApexPages.StandardController sc;
    public Account_Property__c objB {get; set;}
    
    public accountProperty(ApexPages.StandardController sc)
    {
        this.sc = sc;
        objB = new Account_Property__c();
		ObjB.Account__c = sc.id;
    }
    
    public ApexPages.PageReference SaveBoth()
    {
        insert objB;
		return new PageReference('/'+objB.id)
    }

}
<apex:page StandardController="Account" extensions="accountProp" sidebar="false">
    <apex:sectionHeader title="Edit AccountProperty" />
    <apex:form >
        <apex:pageBlock title="Edit AccountProperty" id="thePageBlock" mode="edit">
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!SaveBoth}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
            <apex:actionRegion >
                <apex:pageBlockSection title="Property Information" columns="1">
                    
                    <apex:pageBlockSectionItem >                       
                        <apex:outputLabel value="Property_Name__c"/>
                        <apex:outputPanel >                            
                            <apex:inputField value="{!objB.Property_Name__c}">
                                <apex:actionSupport event="onchange" rerender="thePageBlock"
                                                    status="status"/>
                            </apex:inputField>
                            <apex:actionStatus startText="applying value..." id="status"/>
                        </apex:outputPanel>
                    </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
            </apex:actionRegion>
            <apex:pageBlockSection title="SVU Channel" columns="1"
                                   rendered="{!objB.Property_Name__c == 'Sunrise ID'}">
                <apex:inputField value="{!ObjB.Account__c}" required="true"/>
                <apex:inputField value="{!objB.Sunrise_ID__c}" required="true"/>
                <apex:inputField value="{!objB.Start_Date__c}" required="true"/>
                <apex:inputField value="{!objB.Implementation_Date__c}" required="true"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="SVU Channel" columns="1"
                                   rendered="{!objB.Property_Name__c == 'SVU Site ID'}">
                <apex:inputField value="{!objB.Account__c}" required="true"/>
                <apex:inputField value="{!objB.SVU_SITE_ID__c}" required="true"/>
                <apex:inputField value="{!objB.Start_Date__c}" required="true"/>
                <apex:inputField value="{!objB.Implementation_Date__c}" required="true"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>


Let us know if this will help you

 
This was selected as the best answer
Gary Stark 3Gary Stark 3
Hi Amit,
Setting the same account on the account property was actually one of the requirements. It seems to work now. I changed the class as follows as sc.id was erroring and i wanted to return back to the account page after save. code below 
 
01
public with sharing class accountProperty {
02
     
03
    ApexPages.StandardController sc;
04
    public Account_Property__c objB {get; set;}
05
     
06
    public accountProperty(ApexPages.StandardController sc)
07
    {
08
        this.sc = sc;
09
        objB = new Account_Property__c();
10
        ObjB.Account__c = sc.getId();
11
    }
12
     
13
    public ApexPages.PageReference SaveBoth()
14
    {
15
        insert objB;
16
        //return new PageReference('/'+objB.id)
        return new PageReference('/'+sc.getId());
17
    }
18
 
19
}



 
Gary Stark 3Gary Stark 3
Really appreciate your help by the way. Thank you so much!