function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jsallen79jsallen79 

Evaluating multi-select picklist value on Visualforce page

We have a Visualforce page that contains a multi-select picklist field called Topics__c.  When a value of "Other" is selected I need to dynamically require another field called Other_Topic__c to be populated before the record can be saved.  There are several other fields on this page that are required and the page controller is checking that those fields are populated before the record can be saved.  My challenge is that I can't seem to correctly evaulate the values in the multi-select picklist prior to the record being saved to determine if Other_Topics__c needs to be required as well.  Below is the snippet of code from the page controller where I'm trying to evaluate the multi-select picklist values.

 

if (cl.Topics__c.contains('Other') && cl.Other_Topic__c ==''){
			bOk=false;
			ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Other Topic is a required field.');
			ApexPages.addMessage(myMsg);
		}

 I've found examples of how to query multi-select fields via SOQL, but since the record hasn't been written to the database yet I'm kind of at a loss.  With the code written as above I can choose "Other", with no value in Other_Topic__c and still save the record, which is bad.

 

Any help would be appreciated as I'm a relatively inexperienced coder, especially when it comes to APEX and Visualforce!

 

amarcuteamarcute

Hi,

 

You can add a validation rule on the object to check if the dropdown value is "Other" & Field value is empty. This will be helpful even if you use DataLoader or any other tool for loading the data.

or 

You can use Javascript method to validate the user input before submitting the form.

jsallen79jsallen79

amarcute wrote:

Hi,

 

You can add a validation rule on the object to check if the dropdown value is "Other" & Field value is empty. This will be helpful even if you use DataLoader or any other tool for loading the data.

or 

You can use Javascript method to validate the user input before submitting the form.


I did try a validation rule at one point, which prevented the record from being saved, but it was not a user friendly error message at all.  The end user would have no way of knowing what was preventing them from saving the record.

testrest97testrest97

I achieved this by converting mutliselect picklist to checkboxes.....and when the particualar checkbox is selected, i called javascript method to enable another field....

 

 

converting multiselectpicklist to checkboxes:

 

public List<selectoption> getOthers() {
list<selectoption> options = new list<selectoption>();
try {
Schema.DescribeFieldResult fieldResult = Account.other__c.getDescribe();
list<schema.picklistentry> values = fieldResult.getPickListValues();
for (Schema.PicklistEntry a : values) {
options.add(new SelectOption(a.getLabel(), a.getValue()));
}
} catch (Exception e) {
ApexPages.addMessages(e);
}
return options;
}

 

visualforce:

 

<apex:pageblocksection title="Other" id="pbso">
<apex:selectcheckboxes onchange="enableFields(this);" value="{!other}" id="other" >
<apex:selectoptions value="{!others}" />
</apex:selectcheckboxes>

 

javascript&colon;

 

function enableFields(id){
if(id.value=='Selected Value'){

 document.getElementById('{!$Component.text1}').disabled=false;

 

}

 

 

 

amarcuteamarcute

Hi,

 

You can setup your own error message to be shown when the condition specified in the validation rule is met. You can also select the location of the error message (top of the page or just next to the field). 

Here are the steps:

 

To create validation rules:-

1. Go to the Object you would like to create the validation rule on. To get to that object click Setup and then Customize. If it is a standard object like Account, Contact, Opportunity, Product, etc.

2. If it is a Customize Object then click on Setup==> Create==>Objects and then click on the object you like.

3. Now click on Validation Rule and click New.

4. Give your Validation a Name

5. Give it a Description (not needed but helps in the long run and good practice)

6. Check the Active Box to make it Active

7. Error Condition Formula: This is where you place in your expression to validate the field.

8. Now in the Error Message Place in the Error you would like the User to see or get.

9. Error Location: Do you want to display the error on top of the page or next to the field itself. If you want it next to the field then click field and choose your field from the Picklist.

10. Click Save to finish or Save & New to create additional validation rules.