11

I have a VF page where I am rendering out some subscription information and a couple of the fields are date fields. The issue (sort of) I am seeing is when a record is null and the VF markup calls a date field on that null record it is placing in today's date instead of nothing.

VF markup:

<apex:pageBlockSection columns="2">
    <apex:outputText value="Upcoming Plan:"/>
    <apex:outputField value="{!nextMembership.Start_Date__c}"/>
    <apex:outputField value="{!nextMembership.Expiration_Date__c}"/>
</apex:pageBlockSection>

Start_Date__c and Expiration_Date__c are Date fields, not Datetime.

In the controller the value for nextMembership is null.

Debug Image:

debug image showing value is null

Line 83 is the same object type as 84, but 83 is not null just to show the object structure.

Apex Snippet:

code snippet image

Just curious what I am missing here? Not a huge issue as I am not even going to render that section if the object is null. But I am really curious why it is showing today's date instead of the empty string???

7
  • Does the field has Default value set at schema level? Aug 3, 2016 at 13:53
  • @MohithShrivastava They do not. Aug 3, 2016 at 13:54
  • I've personally come across one situation where JSON serialization returns null. What happens when you try to debug nextMembership by itself? Are you possibly initializing, cloning, or querying for an instance of CE_Membership__c somewhere else in your code?
    – Derek F
    Aug 3, 2016 at 15:13
  • 4
    I think we need to see where you instantiate the nextMembership property?
    – Adrian Larson
    Aug 3, 2016 at 15:21
  • 1
    @AdrianLarson I never instantiate it. I have a list of them and I assign the list record to nextMembership if it meets criteria. I have it declared as public CE_Membership__c nextMembership {get; private set;}. Aug 3, 2016 at 15:49

2 Answers 2

1

I did some study on this issue and try to reproduce it on my org and it reproduced sucessfully. As per my observation what I found :

  1. I think this is due to <apex:outputfield>. If you will bind start_date__c field with <apex:outputtext>, it will show actual value of the field, which is blank.

  2. On my org I created a new field on Account object, which is numeric, and didn't set default value, not initialize in controller (same as you did), it shows value 0 when I run visual-force page. When I did the same with <apex:outputtext> it shows blank value.

  3. I think it is possibly some inbuilt functionality, that's why it shows some default value with <apex:outputfield> , <apex:inputfield>.

Let me know if you are agree with my points.

1

Rather than keeping the property nextMembership as null, instantiate it with object type. i.e.

YourSOBjectAPIName__c nextMembership = new YourSOBjectAPIName__c();

< apex:outputField value="{!nextMembership.Start_Date__c}" />

will now show blank rather than today's date.

You must log in to answer this question.

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