[SalesForce] How to add days with Date field

I have one date field called "Billing date" and one Number Field called "Billing Delay". I want to update one Date field "DueDate" which computes "Billing date"+"Billing Delay"

for Eg:

"Billing date"=15.11.2016 "Billing Delay"=10 "DueDate"=25.11.2016

While Computation It will Exclude the Saturday and Sunday.

How to achieve this ?

Apex:

for(BEV_BillingEvent__c BillingEvent:lstBillingEvent) {
    Date d=BillingEvent.Billingdate__c;
    Integer i=BillingEvent.BillingDelay__c;  
}

Best Answer

Try the below code. Its much simpler and covers your requirement (I believe).

    for(BEV_BillingEvent__c BillingEvent:lstBillingEvent) 

{

    Datetime dt = DateTime.newInstance(BillingEvent.Billingdate__c, 3, 3, 3,0);
    //system.debug('dt** = '+ dt);

        for (Integer i = 1; i <= BillingEvent.BillingDelay__c;) {
          dt = dt.addDays(1);
          system.debug('dt = '+ dt+dt.format('EEE'));
          if(dt.format('EEE') != 'Sun' && dt.format('EEE') != 'Sat') i++;
        }
}
Related Topic