locked
Setting default on screen based on selection RRS feed

  • Question

  • I have a table containing "budget items". Each "Budget item" has an ledger account.

    Another table "Invoices". This table also has a ledger account and a relation to "budget items".

    My problem is this:
    When the user add an invoice from the CreateNewInvoice screen, I want the field "Ledger account" to be initililized with the default Ledger Accuont from the "Budget Item" table.

    This "default" would be fired whenever the user updates the control "Budget Item" on the Create Invoice Screen.

    The purpose is that for 80% of the invoices for a specific BudgetItem, is using the same account.

    Have searched a great deal, and while I understand how to the set a permanent default on a field on a Screen, I have failed to fint the solution for setting the value of a field by retrivene the value from another table, based on a value that i entered (but not saved) on a screen.

    SAMPLE TABLES (simplfied)
    Table BudgetItem
      BudgetNumber (int)
      DefaultERPAccount (string)

    Table Invoices
      Date (datetime)
      Amount (currency)
      BudgetNumber (int)
      ERPAccount (string)

    See? Very simple: whenever the user leaves the field "BudgetNumber" on the  "CreateNewInvoice" screen, I want to change the field "ERPAccount" (if it's empty of course), with the value retrived from the "BudgetItem" table, based on the entered "BudgetNumber")

    I can do it when programming an .ASP webpage and a SQL call on one of the "CHANGE" event handlers - but how to do in LightSwitch?

    Any ideas/strategies to fix this problem will be very much appreciated :-)

    Happ RAD'ing!

    Friday, January 18, 2013 12:11 PM

Answers

  • You can do this in the Invoice entity's BudgetNumber_Changed method.

    VB:

    Private Sub BudgetNumber_Changed()
        'if ERPAccount isn't empty, do nothing
        If (String.IsNullOrEmpty(Me.ERPAccount) = False) Then Return
    
        'get budget item
        Dim item = Me.DataWorkspace.ApplicationData.BudgetItems.Where( _
            Function(x) x.BudgetNumber = Me.BudgetNumber).FirstOrDefault()
    
        'if a budget item was retrieved, set the ERPAccount value
        If (item IsNot Nothing) Then
            Me.ERPAccount = item.DefaultERPAccount
        End If
    End Sub


    C#:

     
    private void BudgetNumber_Changed()
    {
        //if ERPAccount isn't empty, do nothing
        if (string.IsNullOrEmpty(this.ERPAccount) == false)
        {
            return;
        }
    
        //get budget item
        var item = this.DataWorkspace.ApplicationData.BudgetItems.Where(
            (x) => x.BudgetNumber == this.BudgetNumber).FirstOrDefault();
    
        //if a budget item was retrieved, set the ERPAccount value
        if (item != null)
        {
            this.ERPAccount = item.DefaultERPAccount;
        }
    }


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    • Marked as answer by Angie Xu Tuesday, February 5, 2013 1:50 AM
    Friday, January 18, 2013 2:58 PM
    Moderator