CategoryItemField - How to get a list of option id's and text values

335 views Asked by At

I am trying to create a new entry into podio via the API I can set text type fields and date type fields but I cannot seem to sort out how to set a category type field. The example for .NET shows setting the OptionId = x where x is a int value of the category field. But what if I do not know that Id value. I have a string value from my DB that matches the text value found in the category (dropdown style) field but not sure I can use that to set it?

This example doesn't work b/c it has no values in option.

var typeCategory_ModuleStr = podioNewItem.Field("module");

            IEnumerable<CategoryItemField.Option> categories = typeCategory_ModuleStr.Options;
            List<CategoryItemField.Option> moduleOptions = categories.ToList();
            foreach (CategoryItemField.Option option in moduleOptions)
            {
                if (option.Text == tableRow["MODULE"].ToString())
                {
                    typeCategory_ModuleStr.OptionId = Convert.ToInt32(option.Id);
                }

            }
2

There are 2 answers

1
Cijo Jose V On

You can get the matching option with the option text you have by using the below code sample.

   var app = podioClient.ApplicationService.GetApp(123);
            var options = app.Field<CategoryApplicationField>("category-field-external-id").Options;
            var option = options.FirstOrDefault(x => x.Text == "MyOptionText");
            if (option != null)
            {
                var optionId = option.Id;
            }

then you can set the option like this

Item myNewItem = new Item();
var categoryField = myNewItem.Field<CategoryItemField>("categories");

// Set value to a single option
categoryField.OptionId =  2;  //option_id: 2

C# Client Library Link and Documentation link for any reference.

0
Elias Westerberg On

You can find the option ids for a category in your app by using the sandbox at the bottom of this page: https://developers.podio.com/doc/applications/get-app-22349

Or you can match the text value to the option id by something like this (PHP):

   $country_list = PodioAppField::get( $this::$app_id, 'country' );
   $all_options = $country_list->config['settings']['options'];
   foreach ($all_options as $option) {
      if ($option['text'] === $option_text_value){
         $option_int_value = $option['id'];
      }
   }