How To: Create views to capture data for associated SmartObjects

You can define Associations between SmartObjects, which is typically used to establish relationships between records in the associated SmartObjects. For example, you may have State, Country and City SmartObjects, where a State can have one or more Counties, and a County can have more than one City. When you define the SmartObject, you can define these associations, which can in turn be useful when you need to work with the data in a view.

This how-to explains how to create user interfaces to easily capture data for to populate associated SmartObjects. The sample solution is a form with views for State, County and City. The City editable list contains fields for ID, City, County and State. On this view the City field is an editable field, which means you can type a value into it. County is a drop down with data from the County SmartObject, which is also how State is set up.

You use the City editable list view to add cities in the City field and then you select a County and State from the drop down menus. When you save your changes the new cities are added and linked to their county and state.

Capturing associated data on the sample form

Try it yourself!

Scenario

You want to build a user interface to capture data for Cities, Counties and States. You add the city information by typing it into the City field and then select the county and state from those drop down menus. When you save the view, the city is added and linked to the county and the state because the SmartObjects have associations.

Steps

In this scenario you build three SmartObjects, City, County and State then create associations between them. You then create views based on the SmartObjects and a form that contains the three views

Setup Steps

The following steps set up the scenario from start to finish. If you want to skip the setup steps and view the topic step, go to Create the Associations.

  1. Create the City, County and State SmartObjects.
    In this step, you create three SmartObjects - City, County and State.
    We're using a naming convention where _FK means 'Foreign Key'. This is the ID of the 'foreign' record associated with the current record. In the City SmartObject, for example, State_FK will contain the record ID of the State associated with the City.
    1. From the K2 Designer, create the City SmartObject as shown below.
      City
      PropertiesTypeKeyRequiredUnique
      IDAutonumberYesNoYes
      State_FKNumberNoNoNo
      County_FKNumberNoNoNo
      CityTextNoNoNo
    2. From the K2 Designer, create the County SmartObject as shown below.
      County

      PropertiesTypeKeyRequiredUnique
      IDAutonumberYesNoYes
      State_FKNumberNoNoNo
      CountyTextNoNoNo
    3. From the K2 Designer, create the State SmartObject as shown below.
      State

      PropertiesTypeKeyRequiredUnique
      IDAutonumberYesNoYes
      StateTextNoNoNo

Create the Associations

In this step, you create the associations between the City and State SmartObjects and between the City and County SmartObjects.

  1. Create the Associations
    1. From the K2 Designer, right-click the City SmartObject and select Edit.
    2. Select the Associations tab and click the Add button.
    3. On the SmartObject tab of the Add Association page, browse to the State SmartObject. Select it and click Next.
    4. On the Association tab, make sure the associations are defined as shown below. Click Next.
    5. On the Relationships tab, select State_FK and click the Assign button.
    6. On the Assign property page, select ID from the drop down and click OK.
    7. The Bound To field shows the association between State_FK and State:ID. Click Finish.
    8. Click Add to add another association to the City SmartObject.
    9. On the SmartObject tab of the Add Association page, browse to the County SmartObject. Select it and click Next. On the Association tab, make sure the associations are defined as shown below. Click Next.
    10. On the Relationships tab, select County_FK and click the Assign button.
    11. On the Assign property page, select ID from the drop down and click OK.
    12. The Bound To field shows the association between County_FK and County:ID. Click Finish.
    13. Click Finish to close the City SmartObject.
    14. Next you create an association between the County and State SmartObjects

    15. From the K2 Designer, right-click the County SmartObject and select Edit.
    16. Select the Associations tab and click the Add button.
    17. On the SmartObject tab of the Add Association page, browse to the State SmartObject. Select it and click Next.
    18. On the Association tab, make sure the associations are defined as shown below. Click Next.
    19. On the Relationships tab, select State_FK and click the Assign button.
    20. On the Assign property page, select ID from the drop down and click OK.
    21. The Bound To field shows the association between State_FK and State:ID. Click Finish.
    22. Click Finish to close the County SmartObject.
  2. Generate Editable List Views for all the SmartObjects.
    In this step, you generate editable list views for the State, County and City SmartObjects.
    1. From the K2 Designer, right-click the State SmartObject and select Generate Views.
    2. On the Generate Options page, make sure that the editable list view is generated in the correct category. In this example, they are generated in the Views category. Check the Editable List check box and then click OK.
    3. The generated list view shows in the Views category.
    4. Use the same steps to generate an editable list view for the County and City SmartObjects. The editable list views show in the Views category.
  3. Add data to the State and County SmartObjects.
    In this step, you run the editable list views for the State and County SmartObjects, and then add sample data to them.
    1. From the K2 Designer, locate the State Editable List view and click Run.
    2. Click the Add button and add data as shown below.
    3. Use the table below to add data to the State SmartObject.
      IDState
      1California
      2Illinois
      3Tennessee
      4Texas
    4. The completed State list view.
    5. Run the County Editable List view and use the table below to add data to the County SmartObject.
      State_FKCounty
      CaliforniaLos Angeles
      CaliforniaSan Diego
      IllinoisMcLean
      IllinoisCook
      TennesseeShelby
      TennesseeDavidson
      TexasHarris
      TexasBexer
    6. The completed County list view.
  4. Apply layout changes to the City Editable List View.
    In this step, you change the column locations and the column headers.
    1. From the K2 Designer, right-click the City Editable List view and select Edit.
    2. Click and drag to rearrange the columns, on the layout, in this order: ID, City, County FK, State FK.
    3. Select the County FK column and change the header name to County.
    4. Use the same steps to change the State FK column header to State. Click Finish.
  5. Create a Country Form.
    In this step, you create a Country form and add three views (State, County and City) to the layout.
    1. From the K2 Designer, right-click the Forms category and select New Form.
    2. Name this form Country and click Create.
    3. From the Views tab, click and drag the State Editable List view onto the canvas. Change the view title to State.
    4. Use the same steps and drag County and City Editable List views onto the canvas. Change the view titles to County and City. Click Finish to save the form.
  6. Test the Solution.
    In this step, you run the Country form and use the City view on the form to add a new city and link it to the corresponding County and State.
    1. From the K2 Designer, locate the Country form and click Run.
    2. In this example, you can collapse the State and Country views to allow for more space on the form. In the City view, click Add.
    3. In the new row, type Malibu in the City field, select Los Angeles from the County drop down and California from the State drop down. Click Save.
    4. By clicking Save, Malibu is linked as a city to Los Angeles county and California.
  7. Each SmartObject could be from a different source, such as SQL, SmartBox, SharePoint or a web service, as long as associations can be built between them.
Review

In this How To, you built three SmartObjects called State, City and County, then created associations between them. You then generated three views, created a form and added the views to the form. You used the City editable list view to add cities in the City field and then selected a county and state from the drop down menus. When you saved your changes the new cities were added and linked to their county and state.