0

I have one date "TRIG_DATE" date variable.

I want to check where it falls under period1 or period2

In period 1 i have two fields "UperDate1__c" and "LowerDate1__c"

In period 2 i have two fields "UperDate2__c" and "LowerDate2__c"

Eg: "UperDate1__c"=20 "LowerDate1__c"=5 and "UperDate2__c"=6 "LowerDate2__c" =15

My "TRIG_DATE"=25 so it fals under period1

How to achieve this ?

1
  • 1
    25 isn't between 20 and 5. Why would it be in period 1?
    – martin
    Commented Nov 17, 2016 at 4:53

1 Answer 1

1

There is no way to achieve you goal as 25 is not between 5 and 25.

Or you could simply do this which would be best if the period changes for each records:

for(sObject rec : YOURRECORDS){
     if(Period1.LowerDate__c <= rec.TRIG_DATE__c && Period1.UpperDate__c >= rec.TRIG_DATE__c){
         //is period 1
     }else if(Period2.LowerDate__c <= rec.TRIG_DATE__c && Period2.UpperDate__c >= rec.TRIG_DATE__c){

        //is period 2
     }
}

Or if the Period1 and 2 are static for all records (depending on profiling)

Set<Integer> period1 = New Set<Integer>();
Set<Integer> period2 = New Set<Integer>();

//You will need to add obligatory error checking for null fields, etc

for(Integer x=Period1__c.LowerDate__c;x<=Period1__c.UpperDate__c;x++)
     Period1.add(x);

for(Integer x=Period2__c.LowerDate__c;x<=Period2__c.UpperDate__c;x++)
     Period2.add(x);

for(sObject rec : YOURRECORDS){
     if(Period1.contains(rec.TRIG_DATE__c){
         //is period 1
     }else if(Period2.contains(rec.TRIG_DATE)){
        //is period 2
     }
}

Profiling both with bulk data may be in order as I do not recall if contains is more or less costly than getting a two fields and comparing them each time. Even so it may be negligible so this would be the way to go me thinks

Depending on your data model this could be modified based on your needs. I am sure there are better ways but it is late...How you design your data model will determine how well this can scale. You would not want say 20 different periods to be done this way. If that was the case I would look into using custom settings/metadata to define periods. If the periods are different for each record that is another story.

but your question is vague so this answer may not cover your use case

You must log in to answer this question.

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