You need to sign in to do that
Don't have an account?
Picklist validation
Greetings,
I have a dependent picklist that I need some validation on. When "Disqualified" or "Recycled" is selected from the controlling field, I want the dependent field to be required. The dependent field is only editable when "Disqualified" or "Recycled" are selected. I implemented some code in the "Save" function but it does not work. I am very new to APEX so any help is greatly appreciated. My code is as follows:
***************************************************CONTROLLER******************************************************
public class NewController {
public List<Account> myAccounts;
public Boolean isEditEnabled;
public NewController(){
myAccounts = new List<Account>();
IsEditEnabled = false;
}
public List<Account> getMyAccounts(){
myAccounts = [select Id,name,type, dq_reason__c,preferred_mode_s__c,micro_industry__c from Account WHERE (owner.id =: userinfo.getuserid())];
return myAccounts;
}
public PageReference edit() {
isEditEnabled = true;
return null;
}
public PageReference save() {
for (account a: myaccounts){
if(a.type == 'Disqualified' || a.type == 'Recycled'){
If(a.dq_reason__c == 'NULL'){
apexpages.addmessage(new apexpages.message(ApexPages.severity.ERROR,'Please enter a DQ reason'));
}
}
}
update myAccounts;
isEditEnabled = false;
return null;
}
public Boolean getIsEditEnabled(){
return isEditEnabled;
}
}
*******************************************VISUALFORCE PAGE***********************************************
<apex:page controller="NewController" >
<apex:form id="theForm">
<apex:pageBlock title="My Accounts">
<apex:pageblocktable value="{!myaccounts}" var="acct">
<apex:column headervalue="Account">
<apex:outputField value="{!acct.Name}" />
<apex:inputField value="{! acct.Name}" rendered="{!IsEditEnabled}"/>
</apex:column>
<apex:column headervalue="Preferred Mode">
<apex:outputfield value="{!acct.Preferred_Mode_s__c}"/>
<apex:inputfield value="{!acct.Preferred_Mode_s__c}" rendered="{!IsEditEnabled}"/>
</apex:column>
<apex:column headervalue="Type">
<apex:outputField value="{!acct.type}" />
<apex:inputField value="{! acct.type}" rendered="{!IsEditEnabled}"/>
</apex:column>
<apex:column headervalue="DQ Reason">
<apex:outputField value="{!acct.DQ_Reason__c}" />
<apex:inputField value="{! acct.dq_reason__c}" rendered="{!IsEditEnabled}"/>
</apex:column>
</apex:pageblocktable>
<apex:commandButton value="Save" action="{!save}" rerender="theForm"/>
<apex:commandbutton value="Edit" action="{!edit}" rerender="theForm"/>
</apex:pageblock>
</apex:form>
</apex:page>
Hi,
Please modify the code snippet as
If(a.dq_reason__c == null){
apexpages.addmessage(new apexpages.message(ApexPages.severity.ERROR,'Please enter a
DQ reason'));
return null;
}
Thanks,
Puneet
All Answers
Hi,
Please modify the code snippet as
If(a.dq_reason__c == null){
apexpages.addmessage(new apexpages.message(ApexPages.severity.ERROR,'Please enter a
DQ reason'));
return null;
}
Thanks,
Puneet
Hello and thank you for your reply. I edited my code with what you provided. When I change the "Type" field to "Disqualified" (Leaving the DQ Reason blank) and click save, the error message does not pop up and the "Type" field reverts back to the previous value.
<apex:pageMessages id="msg">
<apex:commandButton value="Save" action="{!save}" rerender="theForm,msg"/>
Puneet,
Thank you for your help. That worked perfectly!!