Here’s a little diddy about how to use the Is Computed field property in LightSwitch.
Here’s the scenario…
It takes time to build relationships with customers. Building relationships requires good communication skills. Keeping in regular contact with existing customers is what helps me generate new business, including leads to new customers. I like to keep in regular contact with my customers so that they always know I am there for them, as well as keeps my name on the front of their minds when new work comes up.
The more customers I have, the more it becomes a challenge to remember when I last contacted someone. That’s why I created a DateLastContacted field in the Customer table of my LightSwitch table. This is great at letting me see when it was I last contacted a customer. With a large list of customers, scanning a list of dates can be tedious.
What I want to do is create a simple and separate indicator to tell me that it is time to call someone. One option is to create a Query for my table and generate a screen from that. But instead, I want to simply have something show up on my screens that will put it in front of my face that I need to call the customer. To do this, I decide to create a computed field on my Customer table.
The idea is that when I open a specific screen, if the last time I contacted the selected customer was more than 30 days ago, the screen will tell me so.
So, back in the designer for my Customer table, I add a new String type field named CallThem. In the properties window for the new field, I check the Is Computed property.
Notice that setting the Is Computed property on the CallThem field displays an icon to the left of the field name in the designer. Also, setting the Is Computed property also enables the Edit Method link, immediately below the is computed property…
I click on the Edit Method link. LightSwitch opens up a code editing window that presents me the stub for adding code to compute the CallThem field.
Namespace MyFirstApplication
Public Class Customer
Private Sub CallThem_Compute(ByRef result As String)
' Set result to the desired field value
End Sub
End Class
End Namespace
This is where I am going to figure out what the value of the CallThem field should be.
What is important to understand here is how LightSwitch is working. Notice that the “result” variable into this method is passed by reference (the “ByRef”), and not by value (or “ByVal”). This tells me that “result” is actually something that LightSwitch is managing automagically. I don’t need to worry about where that result object is, LightSwitch is taking care of it for me. Changing the result object, in this case a string type, will be persisted to wherever else the object is being used. This is great because anytime we use this computed field, the computed result is automatically used. This is an excellent example of how this model centric design works; the model being the Customer entity and anytime that model is used in the application, the computed result is available.
Now back to what I want to accomplish. I want to take a look at the date I last contacted the customer and determine how many days it’s been since I last contacted them. If it’s been more than a 30 days, I want the result to be a message to say so. If less than 30 days, then all is good and I don’t need to see anything.
I know that date I last contacted a customer is stored in a field named DateLastContacted, so I should be able to get that date and calculate how many days it has been. One problem though is to first make sure I even have a date in that DateLastContacted field. The DateLastContacted field is not a required field, so that means there could be some empty data.
Here is what I come up with…
Namespace MyFirstApplication
Public Class Customer
Private Sub CallThem_Compute(ByRef result As String)
' Create a variable and default it as an empty string.
Dim resultString = String.Empty
' Is there a DateLastContacted value?
If Not Me.DateLastContacted Is Nothing Then
' Calculate the number of days since the DateLastContaced.
Dim daysSince = Date.Now.Subtract(Me.DateLastContacted).Days
' If the number of days is greater than 30, then set the result string variable.
If daysSince > 30 Then
resultString = daysSince.ToString & " Days Since Last Contact!"
End If
End If
' Set the result to use for the CallThem field.
result = resultString
End Sub
End Class
End Namespace
A littled cluttered in there but I wanted to add as much commenting so that you understood what was actually happening in the code. It’s really not that complicated. With that set, anytime the CallThem field is used, the value in the field should be the result of the computed value above.
I need to now get it on a screen, but not every screen. I only want it to appear on the List and Details screen I created for my Customers. I open the CustomerList screen I had already created prior to adding the new field.
Because I am using both a list and details, the CustomerList screen designer shows me tree of stuff on the screen. I want to add the CallThem field to the right side of the screen, which happens to be called the (RIGHT COLUMN) Vertical Stack in the tree.
I select the CustomerDetails vertical stack item which enables the Add feature at the bottom of the stack. I then click Add item which then drops down to display a list of the fields in my Customer entity. In the list of fields I see the CallThem field. By default the field shows as a label because the field has been configured as a string type that is calculated.
I click the CallThem item in the list and LightSwitch adds the item to the form.
Easy enough, now lets run the application to see what happens – I pres F5 to start debugging…
Awesome! The computed value for the CallThem field is working. I select a customer who has a DateLastContacted value greater than 30 days ago, and the computed value appears in the details area of the screen…
Poking around the rest of the application and I notice a couple of things. The first is the label for on the screen above. The label should be named something more appropriately, or even better, not displayed at all.
I stop the application and head back to the screen designer for this screen. I select the CallThem item I added earlier. On the property window of the CallThem field I clear the display name. The display name is the label that is displayed on the form, before the actual value. This way, if the computed value is empty, nothing, not even the label will show. If the computed value contains something, then only the computed value will show – adding more of an attentive feature to my screen.
When I cleared the Dsiplay Name property of the CallThem field in the screen designer, this resulted in the item in the tree to also not show the display name…
I press F5 again to see what it now looks like in the application…
Groovy! The screen now only shows the computed value, or is blank if nothing is resulted from the computed value…
Hmmm. Checking out some of the other screens in the application I notice that still see “CallThem” showing as a label in the default edit screen (I clicked Edit in the CustomerList screen above).
I don’t really care if the field is even there in that screen. As mater of fact, I don’t want to see the screen anywhere else other than the screens I want it to show on.
I stop the application and fire up the table designer for my Customer table. I select the CallThem field via the designer and uncheck the Is Visible On Screen checkbox.
Oky Doky, now lets hit F5 again and see what happens…Great! It worked. No default screens, like the add or edit screens, show the CallThem field. Only my CustomerList screen shows the computed CallThem field in the details section of the screen.
Well, my lunch hour is over. Not sure what I will try tomorrow. Probably the Custom Validation feature for a field. Until then, l8r.
Cheers!





























Paul… couldn't figure out how to send you an email… send me one … I have a question for you.
Thanks!
-Dave
Hi Dave,
My email is paul(at sign here)selectsystems.ca.
Cheers!
[...] LightSwitch – Using the Entity Field Custom Validation Aug 31 LightSwitch In my last post I demonstrated how to use the LightSwitch Is Computed field property. In this post I am going to [...]
Hi Paul,
Do you know if you can use a computed field in a query in Lightswitch? I'm trying to use one in a filter and it appears that they don't show up in the object listing?
Thanks!
Hi Donna,
This is a great question! I don't know. I'll post this question to the the LightSwitch forums and see what kind of response we get.
…here's the thread I created on the MSDN LightSwitch forums… http://social.msdn.microsoft.com/Forums/en/lights…
Cheers.
Paul
…followup;
A couple of answers have come in on this question. Short answer is; no you can't use a computed property in the query designer, however the original query result set can be filtered before the results are sent to the execution operation (before sending the data to the screen). This would be achieved via the Executed method of the query.
Check out the answers on the forum thread for some details on how to do this… http://social.msdn.microsoft.com/Forums/en/lights…
Hope this helps.
How do you get around this then? What are some easy tricks to do so?
Why can't you filter a computed value by the query wizard?
Will it be difficult to make a computed field to show a fiscal year
Showing a fiscal year should be relatively easy.
I just threw this together and haven't tested it yet, but you get the idea….
Private Sub FiscalYear_Compute(ByRef result As Integer)
' fiscal year starts in April.
Dim fyStartMonth As Integer = 4
' fiscal year start day is the first day of the month.
Dim fyStartDay As Integer = 1
' fiscal year offset starts the previous year,
Dim fyOffset As Integer = -1
' the date being evaluated
Dim theDate As Date = Me.DateFromTable
Dim theFiscalYear As Integer
If theDate < DateSerial(Year(theDate), fyStartMonth, fyStartDay) Then
theFiscalYear = Year(theDate) – fyOffset – 1
Else
theFiscalYear = Year(theDate) – fyOffset
End If
result = theFiscalYear
End Sub