You need to sign in to do that
Don't have an account?
apex:inputField standard name field value not getting set in the controller
Hi All,
Hope all are doing well.
I'm facing a problem which I have never faced earlier doesn't know what exactly is the problem. Kindly help me to find the solution.
I have following page and controller code snippet:
---- VF Page code ----
<apex:page controller="MyController">
<apex:form id="formId">
<apex:pageBlock>
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Save" action="{!saveNewStudent}"/>
<apex:commandButton value="Cancel" action="{!cancelNewStudent}"/>
</apex:pageBlockButtons>
<table cellspacing="5px" cellpadding="10px">
<tr>
<td style="width:270px; vertical-align:top;">
<apex:outputLabel styleClass="outputLabelClass" value="{!$ObjectType.Student__c.fields.Name.label}"/><br/>
<apex:inputField styleClass="inputFieldClass" value="{!objStudent.Name}"/>
</td>
</tr>
</table>
</apex:pageBlock>
</apex:form>
</page>
-------Controller code------------
public with sharing class MyController
{
public Student__c objStudent{get; set;}
public MyController()
{
objStudent = new Student__c();
}
public void saveNewStudent()
{
system.debug('------objStudent.Name-----'+objStudent.Name);
if(objStudent.Name != null && objStudent.Name != '')
{
//Some logic
}
else
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'The name cannot be left blank.'));
}
}
}
-------My Problem-------
My problem is that when I click on the "Save Button" then in the debug the value for the Student object's standard name field is coming NULL. (The data type of standard name field is text)
But when I use a custom field instead of standard name field then the value is getting reflected in the debug log. Doesn't know what is the main source of problem.
Also, there is no validation rule or other required field for the custom object which is obstructing the save operation.
Thanks,
Vivek Shinde
if(objStudent.Name != null && objStudent.Name != '')
{
//Some logic
}
because i tried using your same code and it works fine for me.
Hi Dhaval,
The problem is that because of the null value of objStudent.name, the if condition is failing and the else part is getting executed.
Thanks,
Vivek Shinde