locked
Adding days to date in the HTML client using addChangeListener RRS feed

  • Question

  • Hi,

    I have the following code on my HTML screen to detect when the invoice date is changed on an invoice. What I would like to do though is once that invoice date is set, automatically set the invoice due date. Here is what I have so far:

    myapp.AddEditInvoice.created = function (screen) {
        function onPropertyChanged() {
            if (screen.Invoice.Cust != null) {
                // TODO set due date here
            }
        }
    
        screen.Invoice.addChangeListener("InvoiceDate", onPropertyChanged);
    
        screen.details.rootContentItem.handleViewDispose(function () {
            screen.Invoice.removeChangeListener("InvoiceDate", onPropertyChanged);
        });
    };

    The Invoice.Cust has a PaymentTerms Integer property which is the number of days from invoice date that payment is due. Ideally what I'd like to do is set the DueDate by adding the PaymentTerms integer to the InvoiceDate. It's dead simple in c# but I seem to be scratching my head here on how to do it. Can anyone help?

    Thanks.

    Paul.


    Friday, April 4, 2014 12:37 AM

Answers

  • Hi Paul,

    Try this:

    myapp.AddEditInvoice.created = function (screen) {
        function onPropertyChanged() {
            if (screen.Invoice.Cust != null) {
                days = screen.Invoice.Cust.PaymentTerms;
                var due = screen.Invoice.InvoiceDate;
                due.setDate(due.getDate() + days);
                screen.Invoice.InvoiceDueDate = due;
            }
        }
    
        screen.Invoice.addChangeListener("InvoiceDate", onPropertyChanged);
    
        screen.details.rootContentItem.handleViewDispose(function () {
            screen.Invoice.removeChangeListener("InvoiceDate", onPropertyChanged);
        });
    };

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Sunday, April 6, 2014 1:59 PM

All replies

  • Saturday, April 5, 2014 3:37 PM
  • Hi Paul,

    Try this:

    myapp.AddEditInvoice.created = function (screen) {
        function onPropertyChanged() {
            if (screen.Invoice.Cust != null) {
                days = screen.Invoice.Cust.PaymentTerms;
                var due = screen.Invoice.InvoiceDate;
                due.setDate(due.getDate() + days);
                screen.Invoice.InvoiceDueDate = due;
            }
        }
    
        screen.Invoice.addChangeListener("InvoiceDate", onPropertyChanged);
    
        screen.details.rootContentItem.handleViewDispose(function () {
            screen.Invoice.removeChangeListener("InvoiceDate", onPropertyChanged);
        });
    };

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Sunday, April 6, 2014 1:59 PM
  • Hi Michael and Dave.

    Thank you both for your help.

    I'd tried this originally and couldn't get it to work:

    screen.Invoice.DueDate = screen.Invoice.InvoiceDate.getDate() + screen.Invoice.Cust.PaymentTerms;

    Breaking it down like Dave suggests did the job. Guessing the answer lies in the due.setDate(). 

    I'll post the code incase someone needs it later as there was a small typeo in Dave's code. ;)

    myapp.AddEditInvoice.created = function (screen) {
        function onPropertyChanged() {
            if (screen.Invoice.Cust != null) {
                days = screen.Invoice.Cust.PaymentTerms;
                var due = screen.Invoice.InvoiceDate;
                due.setDate(due.getDate() + days);
                screen.Invoice.DueDate = due;
            }
        }
    
        screen.Invoice.addChangeListener("InvoiceDate", onPropertyChanged);
    
        screen.details.rootContentItem.handleViewDispose(function () {
            screen.Invoice.removeChangeListener("InvoiceDate", onPropertyChanged);
        });
    };

    Thanks gentlemen!

    Paul.


    Sunday, April 6, 2014 2:30 PM