How to set a user-defined list item on a Udef field

This example demonstrates how to set a user-defined list item value on a user-defined field on a given contact.

We will be using the user-defined field companydropdownlistbox and populate a list box with the list items for it. When an item is picked from the list box, the selected value is displayed. Clicking the Save button will set the selected value to the user-defined field companydropdownlistbox for the contact.

The following screenshot shows how the application displays the list of values for the given user-defined field.

A list of values for the given user-defined field -screenshot

Populate the list box

[!code-csharpCS]

The above code segment shows how the population of the list box is done. To get the list items, the base class for the list table should be identified. This is retrieved with the ListTableId property of the user-defined field. Based on that ID, the Task table is identified as the base class for getting the list items.

As shown below, the TaskRows are retrieved for the given table ID using the GetFromCustomSearch method of the TaskRow entity. Next, we have set the collection as the data source for the list box.

[!code-csharpCS]

Set the user-defined field value

Contact contact = Contact.GetFromIdxContactId(int.Parse(txtContactId.Text.Trim()));
if (contact != null)
{
  // Get the prodId of the udefField of interest
  string progId= contact.UdefHelper.GetProgIdFromFieldLabel("companydropdownlistbox");

  // Modify the value for the udef field for the current contact to the selected value
  contact.UdefHelper.SetValue(progId,this.lstFieldList.SelectedValue);

  // Save the contact details
  contact.Save();
  MessageBox.Show("Contact details saved successfully.");
}

We have used the SetValue method of the UdefHelper of the contact to set the selected value. This method accepts the progId, which is used to refer to a particular field, and the value to set as the updated value for the field.

Next, the Save method of the Contact entity is used to update the contact entity.

Click to download source code (zip)