0

I have two date fields

  1. Manager_recruitment_date__c
  2. Recruitment_officer_date__c

I would want to write a utility class where I want to compare both these fields, and take the Max date and use it somewhere else in my logic.

Date Mydate;

If (o.Manager_recruitment_date__c != null || o.recruitment_officer_date__c != null) {

    if (o.Manager_recruitment_date__c > o.recruitment_officer_date__c) {
         Mydate = o.Manager_recruitment_date__c;
    } else {
        Mydate = o.recruitment_officer_date__c;
    }

}

I am I doing it right? Does this qualify for all combinations of any of the date field being null?

Thanks

2 Answers 2

0

No, that would not work with first if-statement, BOTH dates need to be NOT null:

Manager_recruitment_date__c != null && recruitment_officer_date__c != null

The second error is that you're not assigning max value to your variable, needs to be:

Mydate = Manager_recruitment_date__c;

and

Mydate = recruitment_officer_date__c;

The third error is that you're using fields without actual sObject, so I severely doubt that your class will compile.

2
  • My bad, o.Manager_recruitment_date__c is the sobject reference I am using,. What is the 2nd comment you have made? I am assigning inside the loop is it not right? Mar 20, 2020 at 6:44
  • But there is no loop in your example. And you want to have max value assigned to Mydate and not to the fields. Mar 20, 2020 at 7:03
0

If you just want to use the Maximum of Manager_recruitment_date__c and Recruitment_officer_date__c in your code , then you can create a formula field on the same object which will give you the Maximum of these two days and query that field to use in your logic.

IF( (Date1 - Date2) > 0 , Date1, IF((Date1 - Date2) < 0, Date2, IF(ISBLANK( Date1 ) && NOT( ISBLANK( Date2 ) ), Date2 , IF(ISBLANK( Date2 ) && NOT( ISBLANK( Date1 ) ), Date1 , null) ) ) )

You must log in to answer this question.

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