Question about Option fields

ResenderResender Member Posts: 119
Ok, I got a question that needs a quick answer.
I got a codeunit to import a csv, I can't use a dataport.
Amongst the fields are 3 option Fields, in the csv i got the values of their OptionStrings,for example Sales Order.
Now I need to put in that into the option field in the table, can I get the value of an Option based on the option string.

Answers

  • ajhvdbajhvdb Member Posts: 672
    evaluate(Optionfield, 'Sales Order') will work
  • ChinmoyChinmoy Member Posts: 359
    Considering, Field - "Type" - Option - Contract,Permanent

    You can write code similar to the following:

    TypeText := FORMAT(Type);

    CASE TypeText OF
    'Contract':
    MESSAGE('Contract');
    'Permanent':
    MESSAGE('Permanent');
    END

    Hope this helps.

    Chn
  • krikikriki Member, Moderator Posts: 9,112
    First thing I would try is to have the integer-values of the options. They are a more secure and language-independent.
    (to export an option as an integer: intDocumentType := SalesHeader."Document Type"; export the intDocumentType. It will export an integer and the the caption of the option).

    Otherwise you can use EVALUATE. BUT it is language-dependent!
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • ResenderResender Member Posts: 119
    kriki wrote:
    First thing I would try is to have the integer-values of the options. They are a more secure and language-independent.
    (to export an option as an integer: intDocumentType := SalesHeader."Document Type"; export the intDocumentType. It will export an integer and the the caption of the option).

    Otherwise you can use EVALUATE. BUT it is language-dependent!
    Yes this is more or less what I want, I was given the assignement to make the code unit to import from a csv file into 2 tables.(As an exta I don't even know how the csv looks)
    I have very little info further, but I do have the database.
    What I want is to get the integer value of the option based on a string.
    For instance Sales Retour Order has an integer of 4, Verkoopretourorder is the dutch option for this.
    I want to know of there's a way to get the numeric value based on the strings in the optionstrings.
  • ChinmoyChinmoy Member Posts: 359
    Variables:
    RecRef: Type - RecordRef
    FRef: Type - FieldRef
    OptionText: Type - Text250

    Code:
    RecRef.OPEN(<Your Table Id>);
    FRef := RecRef.FIELD(<Your option field id>);
    OptionText := FRef.OPTIONSTRING;

    I think this may help. Once you get the OptionString from the field and the string data from your file, you can compare the values and see which option it is. Now option values starting from 0, you can easily find out what the integer value will be.

    Chn
  • krikikriki Member, Moderator Posts: 9,112
    Resender wrote:
    I want to know of there's a way to get the numeric value based on the strings in the optionstrings.

    EVALUATE(SalesHeader."Document Type",txtTheOptionValueAsText);
    intDocumentType := SalesHeader."Document Type";
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • ResenderResender Member Posts: 119
    kriki wrote:
    Resender wrote:
    I want to know of there's a way to get the numeric value based on the strings in the optionstrings.

    EVALUATE(SalesHeader."Document Type",txtTheOptionValueAsText);
    intDocumentType := SalesHeader."Document Type";
    This works but like you said in a previous post it's not language dependant.
  • ajhvdbajhvdb Member Posts: 672
    Resender wrote:
    This works but like you said in a previous post it's not language dependant.

    That wasn't a requirement in your question.. :)

    Look in codeunit 8611,
    ValidateOptionValue(VAR FieldRef : FieldRef;OptionValue : Text[250]) Valid : Boolean
  • ResenderResender Member Posts: 119
    ajhvdb wrote:
    Resender wrote:
    This works but like you said in a previous post it's not language dependant.

    That wasn't a requirement in your question.. :)

    Look in codeunit 8611,
    ValidateOptionValue(VAR FieldRef : FieldRef;OptionValue : Text[250]) Valid : Boolean
    Ok had to copy and alter to do this
    I added that we should both look in the optionstring as well as the optioncaption
    IF OptionValue = '' THEN
      EXIT;
    IF EVALUATE(OptionValueAsInteger,OptionValue) THEN BEGIN
      StartPos := 1;
      OptionNo := 0;
    
      WHILE NOT (StartPos = STRLEN(FieldRef.OPTIONCAPTION) + 1) DO BEGIN
        IF COPYSTR(FieldRef.OPTIONCAPTION,StartPos,1) = ',' THEN
          OptionNo := OptionNo + 1;
        StartPos := StartPos + 1;
      END;
    
      EXIT(NOT(OptionValueAsInteger > OptionNo));
    END ELSE BEGIN
     IF NOT (GetOption(OptionValue,FieldRef.OPTIONCAPTION,OptionValueAsInteger)) THEN BEGIN
        EXIT(GetOption(OptionValue,FieldRef.OPTIONSTRING,OptionValueAsInteger))
     END ELSE BEGIN
         EXIT(GetOption(OptionValue,FieldRef.OPTIONCAPTION,OptionValueAsInteger))
     END;
    END;
    
    
Sign In or Register to comment.