locked
how do you populate parent key into child screen when wanting to use custom screen for creating child records? RRS feed

  • Question

  • Hi, I have looked at the number of examples so far in the forum and not quite getting my code to work with respect to the above.

    I have a parent table with service request and a child table with service request notes. ( many notes to one service request)

    Below is pic of table:

    In my application I have both the parent and child in a list details screen. But I don't want to use the default popup for adding new notes because I want to default the lastupdated  and written by values. (And the ServiceRequest field needs to be defaulted from parent). Only the note description field should be filled out.

    I have created a createnewservicerequestnote screen  and in the initializedataworkspacemethod I have

    Private Sub CreateNewServiceRequestNote_InitializeDataWorkspace(saveChangesTo As List(Of Microsoft.LightSwitch.IDataService))
                ' Write your code here.
                Me.ServiceRequestNoteProperty = New ServiceRequestNote()
                Me.ServiceRequestNoteProperty.WrittenBy = Application.User.Name
                Me.ServiceRequestNoteProperty.LastUpdated = Now()
            End Sub

    In the pic above , how do I populate the service request dropdown with the key from the parent?

    In my application ,I am making the service requests notes an add only ( no edit) so that the history of conversation is as it was time stamped etc.

    I have seen the examples of passing the parameter from the parent screen. That part I understand. You create a property and set it as a parameter in the child screen, and then when you call that screen from the parent it prompts for value of parameter to be passed. Above the RequestID is that property.

    If I attempt to Assign ServiceRequestNoteProperty.ServiceRequest.Id = to that RequestID property- I get the typical null object error. How do I code it so that the RequestID passed from the parent record gets assigned to the ServiceRequest Key field above.

    The answer is probably patently obvious, but I seem to be coming up with the null object error even when I hardcode a value in for that assignment. When I debug , I see that the parameter does get passed from parent record.

    What am I missing here? The use case is so typical here.

    Thanks

    Sunday, September 22, 2013 1:40 AM

Answers

  • ok I finally got this figured out.

    I wanted to assign the parameter directly to the parent field in the child record. But you cant

    The correct assignment was (in the child screen code):

    Me.ServiceRequestNoteProperty.ServiceRequest = DataWorkspace.ApplicationData.ServiceRequests_SingleOrDefault(RequestID)

    Problem solved.

    Thanks Amr  for your questions and help.

    • Marked as answer by lvsund Friday, September 27, 2013 3:35 PM
    Friday, September 27, 2013 3:34 PM

All replies

  • Hi,

    Instead of going through the troubles of creating a custom screen, why don't use populate the new record with the default values when the record is created? you can do that by going to the entity designer "ServiceRequestNoteProperty" and click on the WriteCode and select "ServiceRequestNoteProperty_Created" method, then add your code there (something like the following, just remember my code in C#):

            partial void ServiceRequestNote_Created()
            {
                this.WrittenBy = Application.User.Name;
                this.LastUpdated = System.DateTime.Now;
            }

    with this, you don't need to create a custom screen and you can use the default AddEdit screen.

    Tuesday, September 24, 2013 2:40 PM
  • Thanks Amr.

    I ended up doing a workaround somewhat similar to what you suggested. I ended up putting those assignments above into the entity inserting code.

    My screen was a list details screen with many tabs in it for multiple child tables. of which the service request notes was one of the child tables. I realized after fooling around a bit that I could get rid of the standard addandeditnew button which brought up the default dialog box for data entry, and replace it with an addnew button only. That forced it to create a new record right in the grid on the service request notes child  table tab right on the service request list details screen. I made the fields other than the notes description itself as label fields to prevent entry of those in the grid.

    I still need to tweak even that further somehow because once the record is entered as a note, I don't want even the note description to be changed- ie it becomes an audit trail for notes. So editable on add but then unchangeable once entered.

    My thoughts had been that going to a new addedit screen would allow for more control then of the behavior of the add notes .

    While what I did is a workaround that works, the reason for asking the original question was that 'that' use case- having a list details screen with parent and multiple children tabs and where you don't want the default dialog for child record entry in child tab grids- must be a pretty standard use case. And the above assignments for those two items was easy and it worked- I put those  in the initialize workspace method.

    What was constantly causing problems was trying to bring over the parent key field to be prepopulated in the child record as a default. In the initialize workspace method- it kept giving me a null reference object error for only that assignment. So I was trying to understand under what conditions that happens in the initialize workspace method- and what was the correct coding syntax to make that  assignment of the  parent key to the child work- such that the null error wouldn't occur.

    Bottom line- yes I was able to do something similar to what you described as a workaround solution. But still end up wondering how to resolve that problem for the above use case.

    Thanks for your suggestion and for the review of my question- It is appreciated!

    Tuesday, September 24, 2013 3:22 PM
  • Hi,

    One question about your code in the 1st post, How are you passing the value of the parameter from the parent to the child screen. Furthermore, in the Parent screen are you selecting an existing row, or are you trying to create a new ServiceReuqestNote out of a new ServiceRequest record?

    Tuesday, September 24, 2013 4:26 PM
  • The use cases  for service request notes in my design would be that the service request typically already exists. The reason being that the purpose of the note is to add information beyond the original description provided in the request. The parent record has a request title and request description  field. The whole idea here is that since a request may end up being modified- the desire is to see the original description as it was originally entered- then any adjustments to that description through service request notes- showing who said what when. The request notes as well should be enter once- time and person stamp- and not modify.

    Currently, I am using list detail for parent  child - because I have multiple children tables showing history of status assignment, team assignment , and team member assignment and service request notes. These are on different tabs by the default design of list details screen.

    To pass the  value from parent to child, I have a 'createnewrequestnote' screen. On it I have a property called RequestID, marked as parameter. The rest of the createnewrequestnote screen is 'default' design. It shows the child table fields, along with the parent key field. In vanilla design of course- this parent key field isn't populated. IT wants you to select what parent this child records belongs to.

    By having set a property in this screen as parameter, it does indeed cause the calling of this screen to prompt for a field (  the key from the parent record is passed ). When I debug- that key value (id) is indeed coming down to the child screen. But when you attempt to assign the parent key field in the child screen to this 'RequestID' -property/parameter, that is when I get the null reference error- even though those other two defaults for date and user don't result in that error. If I hard code the parent key- I still get the same error on the assignment of a value to the parent key value in the child record.

    The createnewservicerequest screen is called from the add button on the parent screen- using the standard override  add code functionality. That button is the add button that exists on the tab for service request notes  on the parent list details screen.

    So in summary- I get defaults to work in the newly created service request notes for the lastupdated date field and written by 'application.user.name'  field- just not for  the parent key field in the child record. That assignment of value to that one causes the null reference error.

    Examples of assignment that causes the error in the initializedataworkspacecode:

    'Me.ServiceRequestNoteProperty.ServiceRequest.Id = 2

    'Me.ServiceRequestNoteProperty.ServiceRequest.Id = RequestID

    Tuesday, September 24, 2013 7:53 PM
  • ok I finally got this figured out.

    I wanted to assign the parameter directly to the parent field in the child record. But you cant

    The correct assignment was (in the child screen code):

    Me.ServiceRequestNoteProperty.ServiceRequest = DataWorkspace.ApplicationData.ServiceRequests_SingleOrDefault(RequestID)

    Problem solved.

    Thanks Amr  for your questions and help.

    • Marked as answer by lvsund Friday, September 27, 2013 3:35 PM
    Friday, September 27, 2013 3:34 PM
  • I have been looking days and days, pages and pages to find this solution. Thank you for ending it!
    Thursday, December 5, 2013 10:02 PM