0

Hi can someone help me figure out why my Validation error is not showing. I try system.debug if the flow is right and it is. It is able to save if there is no Error though.

My goal is to show validation error if some of the fields are blank when I click Create Profile

This is a snip of my visualforce page enter image description here

This is a snippet of the code I use in the Create Profile Button

public PageReference CreateProfileTab6(){

    System.debug('CreateProfileTab6');

    flag=false;

    try{

        listBoolean.clear();

        system.debug('listBoolean'+listBoolean);

        for(Integer i=0;i<pe_wrappers.size();i++){   

        listBoolean.add(false);


           //No Missing Field
        if (pe_wrappers[i].profexp.Job_Title__c != null && pe_wrappers[i].profexp.Start_Date__c != null && pe_wrappers[i].profexp.Name != null) {
             //populateProfessionalExperience();
             //pe_wrappers[i].profExpReady = true;
             listBoolean[i] = false; 
        } else {
             //pe_wrappers[i].profExpReady = false;  
             listBoolean[i] = false;
        }                                        
           //Missing Job_Title__c && Start_Date__c. Company_Name__c != null
        if (pe_wrappers[i].profexp.Name != null && pe_wrappers[i].profexp.Job_Title__c == null && pe_wrappers[i].profexp.Start_Date__c == null) {
            pe_wrappers[i].jtError = true;
            pe_wrappers[i].startError = true;
            pe_wrappers[i].jdError = true;                       
            listBoolean[i] = true;
        } else {
            pe_wrappers[i].jtError = false;
            pe_wrappers[i].startError = false;
            pe_wrappers[i].jdError = false;
        }

        //Missing Start_Date__c
        if (pe_wrappers[i].profexp.Name != null && pe_wrappers[i].profexp.Job_Title__c != null && pe_wrappers[i].profexp.Start_Date__c == null) {
            pe_wrappers[i].jtError = false;
            pe_wrappers[i].startError = true;                    
            listBoolean[i] = true;    
        }

        //Missing Job_Title__c
        if (pe_wrappers[i].profexp.Name != null && pe_wrappers[i].profexp.Job_Title__c == null && pe_wrappers[i].profexp.Start_Date__c != null) {
            pe_wrappers[i].jtError = true;
            pe_wrappers[i].startError = false;                    
            listBoolean[i] = true;   
        } 

        //Companyname is missing || Job Title is missing
        if (pe_wrappers[i].profexp.Name == null && pe_wrappers[i].profexp.Job_Title__c == null && pe_wrappers[i].profexp.Start_Date__c != null) {
            pe_wrappers[i].jtError = true;
            pe_wrappers[i].companyNameError = true;
            pe_wrappers[i].startError = false;                    
            listBoolean[i] = true;    
        } 

        //CompanyName is Missing && Start_Date__c is missing
        if (pe_wrappers[i].profexp.Name == null && pe_wrappers[i].profexp.Job_Title__c != null && pe_wrappers[i].profexp.Start_Date__c == null) {
            pe_wrappers[i].jtError = false;
            pe_wrappers[i].companyNameError = true;
            pe_wrappers[i].startError = true;                    
            listBoolean[i] = true;  
        }             

        if ((pe_wrappers[i].profexp.Job_Title__c != null || pe_wrappers[i].profexp.Start_Date__c != null || pe_wrappers[i].profexp.Job_Description__c != null) && pe_wrappers[i].profexp.Name == null) {
            pe_wrappers[i].companyNameError = true; 
            listBoolean[i] = true;
        } else {
            pe_wrappers[i].companyNameError = false;               
        }  


        System.debug('listBoolean After ForLoop' + listBoolean);

    }

    system.debug('pevalidation ' + peValidation);

    system.debug('finalListBoolean outPut for Professional Experience. True There is error||False No Error' + listBoolean);
    //ListBoolean for Professional End 

     //return pageRef;


         // -----------------

        if(!flag){  



        for (Boolean lstBoolean: listBoolean) {
        System.debug('listboolean inside Flag' + listBoolean);
            if (lstBoolean == true) {
                peValidation = true;   
            } 
        } 


        system.debug('peValidation for Inserting. if False Insert' + peValidation);


            if (peValidation  == false ) {                
                customSecQuest();
                c.AccountId = '0012800001B2ofk'; 
                insert c;

                populateListsWithContactId(c);

                pageRef = new PageReference('/'+c.Id); 
                pageRef.setRedirect(true);

            } 

            //If peValidation == True. There is Error
            //System.debug('Error Message' + '' + 'Comapny Error' + pe_wrappers.companyNameError + 'Job Title' + pe_wrappers.jtError + 'Start Date Error' + pe_wrappers.startError);

            else {

                System.debug('There should be an error message');
                pageRef = new PageReference(PAGE_CREATE_NEW_PROFILE); 
                pageRef.setRedirect(false);

            }

        }

        else{
            pageRef = new PageReference(PAGE_CREATE_NEW_PROFILE); 
            pageRef.setRedirect(false); 
        }

    }catch(Exception e){
        //ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage());
        //ApexPages.addMessage(myMsg);
        clearLists(); 
    }

    pageRefresh = true;
    return pageRef;
}
4
  • What is the use of flag in your code? Commented May 16, 2017 at 8:45
  • 1
    Are you aiming to use ApexPages.Message (that is presently commented out)? One reason those might not appear is that you don't have an apex:pageMessages in your Visualforce. Another reason is that if you route to a new page the messages will be lost.
    – Keith C
    Commented May 16, 2017 at 10:00
  • @KeithC Hi. I'm not using ApexPages.Message cause i need to show the error message beside the field that is required. As you can see I'm routing it to else { System.debug('There should be an error message'); pageRef = new PageReference(PAGE_CREATE_NEW_PROFILE); pageRef.setRedirect(false); } If there is an error message. Any Idea on what should i do with it or How can show the error message.
    – imajmdf
    Commented May 17, 2017 at 0:52
  • @KeithC I tried to remove the page redirect if there is an error and it works now. Thanks KeithC.
    – imajmdf
    Commented May 17, 2017 at 0:59

0

You must log in to answer this question.

Browse other questions tagged .