0

In my controller i try to compare two dates and if it tru return the error message.

Date Today = system.Today();
Date StartDateParsed = Date.parse(startdate);   
if(StartDateParsed <= Today){bring ERROR message}

the problem is when the conditional is true it works like it is false. In the debug it shows:

|DEBUG|StartdateP=2016-09-26 00:00:00Today=2016-09-26 00:00:00

Why it doen't work?

Upd:

date dt = system.Today();
String st = dt.format();
startDate = st; 

startDate = |DEBUG|startdate=9/26/2016

Upd 2 If i ran this code (And startDate < or = Today):

    if(StartDateParsed <= Today){ 
        System.debug ('True');
    } 
    else{
        System.debug('False');
    }

Debug is True

Upd 3 I have the another page messages that shows correctly:

if (context.Title == NULL){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select the Title for Contact'));
        }

but the next code doen't shows the error message at all

if (context.SelectedType == 'Planned' && numberDaysDue >= 0){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please specify the future date'));
            system.debug('true');
        }

And the debug log says true. This Error message doesn't shows at all, but the rest messages shows fine.

Upd 4 VisualForce section Error messages:

<apex:outputPanel id="messages">
<apex:pageMessages />
</apex:outputPanel>

My save button:

<apex:commandButton id="save" value="Save" action="{!save}" style="padding: 5px;" status="nextActionStatus" reRender="messages,screens,buttons" rendered="{!Context.Type != NULL && Context.Contacts != NULL}"/>

I don't think that the proble is in my vf page, because the rest messages are shown correctly.

Upd 5

if (context.SelectedType == 'Planned' && StartDateParsed <= Today){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please specify the future date'));
        }
18
  • 1
    Can you edit your post to share with us the startDate string?
    – Adrian Larson
    Commented Sep 26, 2016 at 11:42
  • @AdrianLarson added
    – Viktor
    Commented Sep 26, 2016 at 12:26
  • @Viktor from where are you getting "startdate" value? Meanwhile try this - Date startDateParsed = today - 5; if(today > startDateParsed){ System.debug ('today is greater than startDateParsed');} else{System.debug('why??');}
    – mritzi
    Commented Sep 26, 2016 at 12:33
  • 1
    if you just want to check the different between 2 dates then you can use daysBetween(secondDate) method developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… Can you just give a try with this method? hope this will solve your problem
    – Ratan Paul
    Commented Sep 26, 2016 at 13:13
  • 1
    You need to be a bit more clear. From what I read in your question you expect 9/26/2016 <= 9/26/2016 to be false?? It is not, it is true as it is equals to. Maybe try restating your question without all of the extraneous detail. If your issue is with the page message, well you are not showing how you are creating it. Maybe post the entire code block for the date compare. Also, make sure you are not calling any action functions that then would cause the page message to be cleared.
    – Eric
    Commented Sep 26, 2016 at 14:21

1 Answer 1

0

I really don't know why the message doesn't appear. But i'm find another way to make this work correctly.

//Create the list of string for the error messages
List<String> errors =new List<String>();
String allErrors ='';

//Make the condition for error massage appear
if (context.SelectedType == 'Planned' && StartDateParsed <= Today){

     //Add the message in to a list 
     errors.add('Please specify the future date');
}

//Create the string of errors for show up as extension
//Make sure you set escape="false" in apex:pageMessages section
if (!errors.isEmpty()){                
                    for (String val : errors) {
                            allErrors = allErrors + val + '<br/>';
                    }                
            throw new TaskSchedulerException(allErrors);
        }

You must log in to answer this question.

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