2

I have written a Trigger for required field.. I have created one vf page that has checkbox and if i select checkbox it will show pageblock section otherwise it will not show pageblock section. My Trigger is working fine but problem is when i select the checkbox it will flush all error messages.My requirement is whether i select or not it will show error message for required field

vf page

<apex:page sidebar="false" standardController="DetailLog__c" extensions="checkboxctrl">
    <script type="text/javascript">

        function showhide()
        {
            display:none

        }


    </script>
 apex:form >


     <apex:pageBlock id="dir" >
     <apex:pageBlockButtons >
         <apex:commandButton value="Save" action="{!Save}"/>
     </apex:pageBlockButtons>

         <apex:pageBlockSection title="Perosnal Detail">

             <apex:inputField value="{!DetailLog__c.First_Name__c}"/>
             <apex:inputField value="{!DetailLog__c.Last_Name__c}"/>
             <apex:inputField value="{!DetailLog__c.Email_ID__c}"/>
              <apex:inputField value="{!DetailLog__c.Date_of_Birth__c}"/>

         </apex:pageBlockSection>

         <apex:inputCheckbox value="{!recheck}" onchange="showhide()" title="Working">
            <apex:actionSupport action="{!reMethod}" event="onclick" reRender="dir,exp,wed"/>
     </apex:inputCheckbox>

     <apex:outputPanel id="exp" rendered="{!recheck}">

      <apex:pageBlockSection id="wed" title="Work Experience Detail" rendered="{!if(re==true,true,false)}">

             <apex:inputField value="{!DetailLog__c.Company_Name__c}"/>
             <apex:inputField value="{!DetailLog__c.Company_Address__c}"/>
             <apex:inputField value="{!DetailLog__c.Joining_Date__c}"/>
              <apex:inputField value="{!DetailLog__c.Notice_Period_Month__c}"/>
              <apex:inputField value="{!DetailLog__c.Desgination__c}"/>
              <apex:inputField value="{!DetailLog__c.Department__c}"/>



         </apex:pageBlockSection>
         </apex:outputpanel>
 apex:pageBlockSection title="Additional Information" >

             <apex:inputField value="{!DetailLog__c.Graduation__c}"/>
             <apex:inputField value="{!DetailLog__c.Post_Graduation__c}"/>
             <apex:inputField value="{!DetailLog__c.University__c}"/>
             <apex:inputField value="{!DetailLog__c.College_Name__c}"/>
             <apex:inputField value="{!DetailLog__c.Known_Programming_Languages__c}"/>
         </apex:pageBlockSection>
     </apex:pageBlock>
 </apex:form>

</apex:page>

Controller

public class checkboxctrl {

   public String recheck{get;set;}
   public Boolean re{get;set;}

    public checkboxctrl(ApexPages.StandardController controller) {



    }

    public PageReference reMethod()
    {


        re=true;
        System.debug('--Re--'+re);
        return null;
    }

    public PageReference reFalseMethod()
    {


        re=false;
        System.debug('--Re--'+re);
        return null;
    }


}

Trigger

trigger RequiredFieldTrigger on DetailLog__c (before insert) {

for(DetailLog__c dl:Trigger.new)
{

    if(dl.First_Name__c==''||dl.First_Name__c==NULL)
    {
        dl.First_Name__c.adderror('Please Enter First Name');
    }

    if(dl.Last_Name__c==''||dl.Last_Name__c==NULL)
    {
        dl.Last_Name__c.adderror('Please Enter Last Name');
    }
    if(dl.Company_Name__c==''||dl.Company_Name__c==NULL)
    {
        dl.Company_Name__c.adderror('Please Enter Company Name');
    }

    if(dl.Company_Name__c==''||dl.Company_Name__c==NULL)
    {
        dl.Company_Name__c.adderror('Please Enter Company Name');
    }


    if(dl.Joining_Date__c==NULL)
    {
        dl.Joining_Date__c.adderror('Please Enter Joining Date');
    }

    if(dl.Graduation__c==''||dl.Graduation__c==NULL)
    {
        dl.Graduation__c.adderror('Please Enter Graduation Degree');
    }

     if(dl.College_Name__c==''||dl.College_Name__c==NULL)
    {
        dl.College_Name__c.adderror('Please Enter College Name');
    }





}

}
1

I m not sure why you have showhide() function,because it does not specific what component it should hide.I think you should remove it and maybe enough for you to use actionSupport only.

Try change this

<apex:pageBlockSection id="wed" title="Work Experience Detail" rendered="{!if(re==true,true,false)}">

to

<apex:pageBlockSection id="wed" title="Work Experience Detail" rendered="{!re}">

I don't see any need to use Page reference,you might to change this

public PageReference reMethod()
    {
       re=true;
        System.debug('--Re--'+re);
        return null;
    }

To

   public void reMethod()
        {
           re=true;
            System.debug('--Re--'+re);

        }

When you should call reFalse method?I don't see where it being called from?It seem the code only call reMethod.

|improve this answer|||||
0

If you don't want to show all the error messages on the page, just the one for the required field you can try to use the tag. You'll find more info here https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionRegion.htm

Hope this helps

|improve this answer|||||

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.