1

In my controller I have a static list of select options:

public List<SelectOption> getVesselTypeList() {
    List<SelectOption> options = new List<Selectoption>();
    options.add(new selectOption('OTH', 'SOLAS Vessel'));
    options.add(new selectOption('NONM', 'Non-SOLAS Vessel'));
    options.add(new selectOption('NOT', 'Not a Vessel'));
    return options;
}

Selected options value should be stored in custom field VesselType__c. Field is of type text with length of 8 characters which is enough for all values:

<apex:selectList value="{!ZInstallation__c.VesselType__c}" size="1"> 
    <apex:selectOptions value="{!VesselTypeList}" /> 
</apex:selectList>

Produced HTML:

<select name="j_id0:j_id2:j_id4:j_id11:j_id57:j_id59" size="1">
    <option value="OTH">SOLAS Vessel</option>
    <option value="NONM">Non-SOLAS Vessel</option>
    <option value="NOT">Not a Vessel</option>
</select>

On update/insert I get this error message:

Update failed. First exception on row 0 with id a8qO0000000CbIEIA0; first error: STRING_TOO_LONG, Type of ship:: data value too large: SOLAS Vessel (max length=8): [VesselType__c] Error is in expression '{!submit}' in component in page z_installation: Class.ZInstallationController.submit: line 414, column 1

It seems as if Salesforce stores the options text in field VesselType__c and not its value. How can I save the options value in my text field?

1

Create a String attribute like selectedValue in Controller and during save use that value.

Visualforce

String selectedValue {get;set;}

<apex:selectList value="{!selectedValue}" size="1"> 
    <apex:selectOptions value="{!VesselTypeList}" /> 
</apex:selectList>

Controller during update or insert

ZInstallation__c.VesselType__c = selectedValue;

In case you need to assign value on edit mode and display to the user

selectedValue = ZInstallation__c.VesselType__c;
  • thanks, that prevents the error. But how I get the selectedValue on submit exactly? Your example seems to reset the old selected value on update. – Obi_77 Apr 7 '17 at 15:48
  • Ok, now it works – Obi_77 Apr 7 '17 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.