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

jagbarcelo
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.
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.
0
Answers
-
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 empty0 -
Thanks a lot. You put me on the right track. Problem solved.
Regards.0 -
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 Singleton0 -
David Singleton wrote: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?0 -
Mark Brummel wrote:Where is the LIKE button?
The option is available in "Report this Post" button with Font Red "!" sign.CA Sandeep Singla
http://ssdynamics.co.in0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions