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
SFDC-NOOBSFDC-NOOB 

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>

Best Answer chosen by Admin (Salesforce Developers) 
Puneet SardanaPuneet Sardana

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

Puneet SardanaPuneet Sardana

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

This was selected as the best answer
SFDC-NOOBSFDC-NOOB

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.

Puneet SardanaPuneet Sardana
Please add this to your visualforce page

<apex:pageMessages id="msg">

<apex:commandButton value="Save" action="{!save}" rerender="theForm,msg"/>
SFDC-NOOBSFDC-NOOB

Puneet,

 

Thank you for your help.  That worked perfectly!!