C/AL: Iterate through all possible values of an Option field

jagbarcelojagbarcelo Member Posts: 3
Is there any way to iterate through all possible values of an option field? Let's suppose I have a table with an option field called MyField that can take the following OptionString 'Red,Green,Blue,Yellow'
I'd like to write some C/AL code, completely agnostic about the possible values. I do not want to know if there are only 4 or 40 options. Any way of knowing the upper boundary?

Writing something like:
FOR var := MyTable.MyField::Red TO MyTable.MyField::Yellow DO
...
would imply that you know the possible values.

Is there any way to write this without having to hard code them in advance. I mean, any way that would not need to be rewritten, if the optional values are ever modified (added new values for instance).

Thanks in advance.

Answers

  • SogSog Member Posts: 1,023
    options are stored as integers and are interchangable.
    I haven't tried to do what you want to do, but I quess if you try something like this:
    Dim i as integer;
    dim rec as rec with an optionfield
    repeat
      i+=1;
      (temp)rec.optionfield := i;
    until format(rec.optionfield) = ""//or maybe format(rec.optionfield) = format(i);
    
    I'm definitly not sure about the format check, but I believe that when formatting the optionvalue, you receive the selected optionstring, and if else just the number or empty
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • jagbarcelojagbarcelo Member Posts: 3
    Thanks a lot. You put me on the right track. Problem solved.

    Regards.
  • David_SingletonDavid_Singleton Member Posts: 5,479
    If you have an option field where you don't know the upper value, it means you should have done it a different way, most likely by lining to a supporting table. So basically this means your fundamental database design is wrong.

    You should be able to structure your code in a CASE statement, and if later there is an actual business need to add a new option, then you add to the CASE. Colors are NOT a case where you would use an option, except if the colors are representative of something that is not a color. Say for example on Resistors where the color (Black,Brown, Red,Orange,Yellow,Green etc) represent the value in Ohms of the resistor (1,1.2,1.5,1.8,2.2 etc) those values are fixed and don't change. But in the case of the color of a Tee Shirt, the manufacturer could add more colors so don't use an option.

    The solution is the fix the problem at the source, not finding hacks to work around a bad design.
    David Singleton
  • Marije_BrummelMarije_Brummel Member, Moderators Design Patterns Posts: 4,262
    If you have an option field where you don't know the upper value, it means you should have done it a different way, most likely by lining to a supporting table. So basically this means your fundamental database design is wrong.

    You should be able to structure your code in a CASE statement, and if later there is an actual business need to add a new option, then you add to the CASE. Colors are NOT a case where you would use an option, except if the colors are representative of something that is not a color. Say for example on Resistors where the color (Black,Brown, Red,Orange,Yellow,Green etc) represent the value in Ohms of the resistor (1,1.2,1.5,1.8,2.2 etc) those values are fixed and don't change. But in the case of the color of a Tee Shirt, the manufacturer could add more colors so don't use an option.

    The solution is the fix the problem at the source, not finding hacks to work around a bad design.

    Where is the LIKE button? :mrgreen:
  • ssinglassingla Member Posts: 2,973
    Where is the LIKE button?

    The option is available in "Report this Post" button with Font Red "!" sign.
    CA Sandeep Singla
    http://ssdynamics.co.in
Sign In or Register to comment.