How To: Use a Composite SmartObject Created from a Many to Many SmartObject Relationship in a Check Box List scenario
This topic shows you how to automatically create a Composite SmartObject based on a many-to-many SmartObject relationship and use the data in a check box list to maintain a list of users and the groups they belong to.
Example
Scenario
Your company has two departments, Call Center and Administration, that share employees. Depending on the workflow, some people work in both departments. You create two SmartObjects, User and Group, and a many-to-many association between them. You then use a Check Box List control along with SmartForm rules to save groups that are checked and delete those that are unchecked. You create views and a form to manage group details.
In this step, you create a User and a Group SmartObject.
- Create a SmartObject called User with the following properties:
Hyperlinks SmartObject Type Key Required Unique ID Autonumber Yes No Yes User Name Text No No No - Create a SmartObject called Group with the following properties:
Hyperlinks SmartObject Type Key Required Unique ID Autonumber Yes No Yes Group Name Text No No No
In this step, you create an association between the SmartObjects.
- From the category tree, right click the User SmartObject and click Edit.
- Go to the Associations tab and click Add to open the Add Association wizard.
- On the SmartObject tab of the wizard click Add, select the Group SmartObject from the User Groups category, then click Next.
- On the Association tab of the wizard, make sure the associations are defined as shown below and type UserGroup for the Composite SmartObject value. This creates a new composite SmartObject called UserGroup, which is essentially a junction table. Click Finish.
- The association shows on the Associations tab of the User SmartObject. Click Finish.
- The UserGroup SmartObject shows in the User Groups category.
In this step, you generate an Item view based on the User SmartObject.
- From the category tree, right click the User SmartObject and click Generate Views.
- On the Generate Options page, in the Views section, make sure that the views are generated in the correct category (in this example, User Groups). Check the Item check box for the User SmartObject and then click OK.
In this step, you add and configure a check box list control and integrate the control with rules.
- From the K2 Designer, right-click the User Item view and select Edit.
- On the Layout tab, click the cell where the Create button is located and click the Insert Row Above icon on the toolbar to insert a row.
- Drag the Check Box List control into the new row.
- Click the ellipsis next to Type in the control's properties.
- Select the option Display and save a composite SmartObject value, click the ellipsis next to Composite SmartObject, and select the Group SmartObject. Make sure the details display as shown below. The many-to-many relationship exists on the Group SmartObject and the UserGroup SmartObject contains the junction table in SQL.
Click OK.
- Go to the Rules tab, select the When Save Button is Clicked rule and click Edit Rule.
- Click the Execute a method on a control for values that are in a specific state action twice and configure it as shown below. This saves and deletes the UserGroup values according to the selection you make in the check box list. Make sure you select then complete the following in a batch execute method.
- Click configure next to the Save method action.
- Click the Output Mappings tab and map the ID return property to the User SmartObject's ID field.
Click Finish.
- Click configure next to Save all the items that have been Checked.
- Expand User Item > Controls > Check Box List > UserGroup in the Context Browser and drag Group ID into the Group ID field. This is the check box list control’s associated field that you map to the composite SmartObject using Group ID. Expand User Item > User and drag ID into the User ID field.
This is the User item view's ID that you map to the composite SmartObject using User ID. Click Finish.
- Click configure next to Delete all the items that have been Unchecked and configure the same mappings. Click Finish.
- Click OK to save the rule.
-
Select the When Create Button is Clicked rule and click Edit Rule.
- Click the Execute a method on a control for values that are in a specific state action and configure it as shown below. This creates the composite SmartObject (UserGroup) values according to the selection you make in the check box list. Make sure you select then complete the following in a batch execute method.
- Click configure next to the Create method action.
- Click the Output Mappings tab and map the ID return property to the User SmartObjects's ID field.
Click Finish.
- Click configure next to Create all the items that have been Checked.
-
Expand User Item > Controls > Check Box List > UserGroup in the Context Browser and drag Group ID into the Group ID field. This is the check box list control’s associated field that you map to the composite SmartObject using Group ID. Expand User Item > User and drag ID into the User ID field.
This is the User Item view's ID that you map to the composite SmartObject using User ID. Click Finish.
- For a complete solution you can repeat steps 15-19 for the When Delete Button is Clicked rule.
- Click OK to save the rule.
- Click Finish to save the view.
In this step, you generate views and create group details.
-
From the K2 Category Grid, Ctrl + click the Group and User SmartObjects, then select Generate Views from the drop-down.
- On the Generate Options page, in the Views section, make sure that the views are generated in the correct category (in this example, User Groups). Check the Item check box for the Group SmartObject and the List check box for the User SmartObject, and then click OK.
- From the K2 Designer, locate the Group Item view and click Run.
-
Type Call Center and click Create, then type Administration and click Create.
In this step, you generate a form and extend the rules to clear the views after creating and saving an item.
-
From the K2 Category Grid, Ctrl + click the User Item and User List views, then select Generate Forms from the drop-down.
- On the Generate Form page, make sure that the form is generated in the correct category. Type Users in the Form Name text box and then click OK.
- From the K2 Designer, right-click the Users form and select Edit.
- Go to the Rules tab, select the On User Item, when Create Button is Clicked rule and then click Edit Rule.
- Click the Execute a View method and configure it as shown in the image below. Click OK. This clears the view after an item is created. This is a good idea when you want to create more than one item, one after the other.
- Select the On User Item, when Save Button is Clicked rule and do the same. Click OK.
- Click Finish to save the form.
In this step, you run the Users form, create some details in the item view, and click a record in the list view to see how the check box list gets updated.
-
From the K2 Designer, locate the Users form and click Run.
- Create some records in the item view. The list view updates when you click Create.
-
Click one of the records in the list view. Note how the item view loads with the record’s detail.
- If you want to create multiple items in sequence, it is a good idea to configure a Clear view method to clear the view after an item is added to ensure you don't create duplicate items.
- If you edit an item, it is a good idea to load the record first before you change it to ensure linked data loads into the control.