I'm using a trigger to insert a list of invoices to a contract object, what I want to do is for the iterations of the loop have the Effective_date__c have todays date, then the second iteration be for next month and so on, is that possible?

if (sc.Payment_Type__c == 'monthly'){ //sc is the service contract
    List<invoice__C> invoiceMonthly = new list<invoice__C>();
    for(Integer intr = 0; intr < 12; intr++)
        i.effective_date__c = date.today();
        i.vat__c = '13.5%';
        i.Service_Contract__c = sc.id;
        i.Invoice_date__c = date.today();
        i.Due_date__c = date.today();
        i.Net_Value__c = sc.TotalPrice/12;
    }//end for intr
    insert invoiceMonthly;
}//end if monthly
  • What is an iteration in your example? – greenstork Nov 19 '14 at 16:48

It sounds like you want something like this:

Date d = Date.today().addMonths(intr);
i.effective_date__c = d;

This makes use of the Date class's addMonths method.

If you want to move to the first day of the next month instead, please add that to the question.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.