+ Start a Discussion
Amita TatarAmita Tatar 

try catch not showing message on vf page

Hi all,

I have below code. I want to show error on VF page when particular condition not met and records not found.I have added my code in try catch block and added apex:messages as well.
Please suggest me what is wrong.
The error should be displayed when combination of service family and sub service category is not found in database.
 
Class:


public with sharing class SurveyClass{

Public Account acc{get;set;}
Public Proposal_Form__c pfc;
Public Proposal_Form__c pfc1 {get;set;}
public ProposalFields__c pf {get;set;}
public List<String> fieldSet {get;set;}
Public ID rid;
private ApexPages.StandardController standardController;
    
public SurveyClass(ApexPages.StandardController controller) {
    this.standardController = standardController;
    pfc1 = new Proposal_Form__c();
  }

public void Selected(){
    system.debug(pfc1.Service_Family__c +'------------'+pfc1.Sub_Service_Category__c);
    
    try{
        pfc = [SELECT Fields_Associated__c FROM Proposal_Form__c where Service_Family__c =: pfc1.Service_Family__c and Sub_Service_Category__c =: pfc1.Sub_Service_Category__c LIMIT 1];
        
    } catch(Exception ex){
     ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'No such combination exists');
     ApexPages.addMessage(myMsg);
      
    }
     
    fieldSet = new List<String>();
    List<String> fields = new List<String>();
    if(pfc.Fields_Associated__c != null && pfc.Fields_Associated__c != ''){
        fields = String.valueof(pfc.Fields_Associated__c).split(',');
        system.debug('*********Fields******'+fields);
    }
    else{
        Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No questions to Display"'));
    
    }
    Map<String,String> labelMap = new Map<String,String>();
    labelMap = getLabel.retLabelMap('ProposalFields__c');
    system.debug(labelMap);
    system.debug(labelMap);
    for(String s : fields){
        fieldSet.add(labelMap.get(s));
        system.debug('********fieldset*******'+fieldSet);
    }
    
}
}


VF Page:;


<apex:page standardController="Account" extensions="SurveyClass" sidebar="false" showHeader="false" tabStyle="Account">

<apex:form >
<apex:pageMessages rendered="true"/>
<apex:pageBlock title="Client Survey Form"> 
<apex:pageBlockSection title="Service Requirements" >

<apex:inputField value="{!pfc1.Service_Family__c}"/>
 
<apex:pageblocksectionItem >
    <apex:outputLabel value="Sub Service Category"/>
    <apex:outputPanel >
        
            <apex:inputfield value="{!pfc1.Sub_Service_Category__c}">
                <apex:actionSupport event="onchange" action="{!Selected}" rerender="fieldst" />
                <apex:pageMessages rendered="true"/>
            </apex:inputField>
       
    </apex:outputPanel>
</apex:pageblocksectionItem>
</apex:pageBlockSection> 

<apex:pageblockSection title="Question Set" id="fieldst">
    <apex:repeat value="{!fieldSet}" var="f">
      <apex:inputField value="{!pf[f]}"/> 
    </apex:repeat>
</apex:pageblockSection>

<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Submit" action="{!Save}"/>
</apex:pageBlockButtons>



</apex:pageBlock>
</apex:form> 
</apex:page>


 
Best Answer chosen by Amita Tatar
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Amita, 

Can you check with this code, 
 
public with sharing class SurveyClass{

Public Account acc{get;set;}
Public Proposal_Form__c pfc;
Public Proposal_Form__c pfc1 {get;set;}
public ProposalFields__c pf {get;set;}
public List<String> fieldSet {get;set;}
Public ID rid;
private ApexPages.StandardController standardController;
    
public SurveyClass(ApexPages.StandardController controller) {
    this.standardController = standardController;
    pfc1 = new Proposal_Form__c();
  }

public void Selected(){
    system.debug(pfc1.Service_Family__c +'------------'+pfc1.Sub_Service_Category__c);
    
    try{
        pfc = [SELECT Fields_Associated__c 
			   FROM Proposal_Form__c 
			   WHERE Service_Family__c =: pfc1.Service_Family__c 
			   AND Sub_Service_Category__c =: pfc1.Sub_Service_Category__c];
        
    } catch(Exception ex){
		Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, 'No such combination exists'));
    }
     
    fieldSet = new List<String>();
    List<String> fields = new List<String>();
    if(pfc.Fields_Associated__c != null && pfc.Fields_Associated__c != ''){
        fields = String.valueof(pfc.Fields_Associated__c).split(',');
        system.debug('*********Fields******'+fields);
    }
    else{
       Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, 'No such combination exists'));
    
    }
    Map<String,String> labelMap = new Map<String,String>();
    labelMap = getLabel.retLabelMap('ProposalFields__c');
    system.debug(labelMap);
    system.debug(labelMap);
    for(String s : fields){
        fieldSet.add(labelMap.get(s));
        system.debug('********fieldset*******'+fieldSet);
    }
    
}
}

Visualforce page
 
<apex:page standardController="Account" extensions="SurveyClass" sidebar="false" showHeader="false" tabStyle="Account">

<apex:form >
<apex:pageMessages rendered="true" id="errMsg" showDetail="false"/>
<apex:pageBlock title="Client Survey Form"> 
<apex:pageBlockSection title="Service Requirements" >

<apex:inputField value="{!pfc1.Service_Family__c}"/>
 
<apex:pageblocksectionItem >
    <apex:outputLabel value="Sub Service Category"/>
    <apex:outputPanel >
        
            <apex:inputfield value="{!pfc1.Sub_Service_Category__c}">
                <apex:actionSupport event="onchange" action="{!Selected}" rerender="fieldst, errMsg" />
                <apex:pageMessages rendered="true"/>
            </apex:inputField>
       
    </apex:outputPanel>
</apex:pageblocksectionItem>
</apex:pageBlockSection> 

<apex:pageblockSection title="Question Set" id="fieldst">
    <apex:repeat value="{!fieldSet}" var="f">
      <apex:inputField value="{!pf[f]}"/> 
    </apex:repeat>
</apex:pageblockSection>

<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Submit" action="{!Save}"/>
</apex:pageBlockButtons>



</apex:pageBlock>
</apex:form> 
</apex:page>

Thanks,
Prosenjit

All Answers

LBKLBK
Hi Amita,

Try this class.
public with sharing class SurveyClass{
	Public Account acc{get;set;}
	Public Proposal_Form__c pfc;
	Public Proposal_Form__c pfc1 {get;set;}
	public ProposalFields__c pf {get;set;}
	public List<String> fieldSet {get;set;}
	Public ID rid;
	private ApexPages.StandardController standardController;

	public SurveyClass(ApexPages.StandardController controller) {
		this.standardController = controller;
		pfc1 = new Proposal_Form__c();
	}

	public void Selected(){
		system.debug(pfc1.Service_Family__c +'------------'+pfc1.Sub_Service_Category__c);

		pfc = [SELECT Fields_Associated__c FROM Proposal_Form__c where Service_Family__c =: pfc1.Service_Family__c and Sub_Service_Category__c =: pfc1.Sub_Service_Category__c LIMIT 1];

		if(pfc == null){
			ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'No such combination exists');
			ApexPages.addMessage(myMsg);
		}
		else{
			fieldSet = new List<String>();
			List<String> fields = new List<String>();
			if(pfc.Fields_Associated__c != null && pfc.Fields_Associated__c != ''){
				fields = String.valueof(pfc.Fields_Associated__c).split(',');
				system.debug('*********Fields******'+fields);
			}
			else{
				Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No questions to Display"'));
			}
			Map<String,String> labelMap = new Map<String,String>();
			labelMap = getLabel.retLabelMap('ProposalFields__c');
			system.debug(labelMap);
			system.debug(labelMap);
			for(String s : fields){
				fieldSet.add(labelMap.get(s));
				system.debug('********fieldset*******'+fieldSet);
			}
		}
	}
}
Let me know if this helps.
 
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Amita, 

can you try this for apex class, 
 
// previous code

try{
        pfc = [SELECT Fields_Associated__c 
			   FROM Proposal_Form__c 
			   WHERE Service_Family__c =: pfc1.Service_Family__c 
			   AND Sub_Service_Category__c =: pfc1.Sub_Service_Category__c];
        
    } catch(Exception ex){
		Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, 'No such combination exists'));
    }

// next code
and this for visualforce page, 
 
<apex:pageMessages showDetail="flase" />

Thanks, 
Prosenjit
 
Amita TatarAmita Tatar
Hi Guys,

I tried with above solutions,but the messages is not getting displayed on the VF page.

Thanks,
Amita
LBKLBK
Can you add the following code above line 20 in the code I have given and let me know what is being printed in log?

System.debug(pfc);
Amita TatarAmita Tatar
Hi LBK,

pfc retrieves 0 rows if no match is found and shows list has no rows for assignment message in debug log for user entered data else 1 row is retrieved.

Thanks,
Amita
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Amita, 

Can you check with this code, 
 
public with sharing class SurveyClass{

Public Account acc{get;set;}
Public Proposal_Form__c pfc;
Public Proposal_Form__c pfc1 {get;set;}
public ProposalFields__c pf {get;set;}
public List<String> fieldSet {get;set;}
Public ID rid;
private ApexPages.StandardController standardController;
    
public SurveyClass(ApexPages.StandardController controller) {
    this.standardController = standardController;
    pfc1 = new Proposal_Form__c();
  }

public void Selected(){
    system.debug(pfc1.Service_Family__c +'------------'+pfc1.Sub_Service_Category__c);
    
    try{
        pfc = [SELECT Fields_Associated__c 
			   FROM Proposal_Form__c 
			   WHERE Service_Family__c =: pfc1.Service_Family__c 
			   AND Sub_Service_Category__c =: pfc1.Sub_Service_Category__c];
        
    } catch(Exception ex){
		Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, 'No such combination exists'));
    }
     
    fieldSet = new List<String>();
    List<String> fields = new List<String>();
    if(pfc.Fields_Associated__c != null && pfc.Fields_Associated__c != ''){
        fields = String.valueof(pfc.Fields_Associated__c).split(',');
        system.debug('*********Fields******'+fields);
    }
    else{
       Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, 'No such combination exists'));
    
    }
    Map<String,String> labelMap = new Map<String,String>();
    labelMap = getLabel.retLabelMap('ProposalFields__c');
    system.debug(labelMap);
    system.debug(labelMap);
    for(String s : fields){
        fieldSet.add(labelMap.get(s));
        system.debug('********fieldset*******'+fieldSet);
    }
    
}
}

Visualforce page
 
<apex:page standardController="Account" extensions="SurveyClass" sidebar="false" showHeader="false" tabStyle="Account">

<apex:form >
<apex:pageMessages rendered="true" id="errMsg" showDetail="false"/>
<apex:pageBlock title="Client Survey Form"> 
<apex:pageBlockSection title="Service Requirements" >

<apex:inputField value="{!pfc1.Service_Family__c}"/>
 
<apex:pageblocksectionItem >
    <apex:outputLabel value="Sub Service Category"/>
    <apex:outputPanel >
        
            <apex:inputfield value="{!pfc1.Sub_Service_Category__c}">
                <apex:actionSupport event="onchange" action="{!Selected}" rerender="fieldst, errMsg" />
                <apex:pageMessages rendered="true"/>
            </apex:inputField>
       
    </apex:outputPanel>
</apex:pageblocksectionItem>
</apex:pageBlockSection> 

<apex:pageblockSection title="Question Set" id="fieldst">
    <apex:repeat value="{!fieldSet}" var="f">
      <apex:inputField value="{!pf[f]}"/> 
    </apex:repeat>
</apex:pageblockSection>

<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Submit" action="{!Save}"/>
</apex:pageBlockButtons>



</apex:pageBlock>
</apex:form> 
</apex:page>

Thanks,
Prosenjit
This was selected as the best answer
Amita TatarAmita Tatar
Hi Prosenjit,

Thanks a lot.It worked.

-Amita
Amita TatarAmita Tatar
Hi Prosenjit,

For this same code, I have standard controller Account. I want to save the VF form  question and answers in some other object.
One Account can have multiple survey forms. How should i achieve the save functioanlity? Can anyone guide me?

-Amita Tatar
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Amrita, 

You have to create a save method and a add a pageblockbutton in visualforce

Thanks,
prosenjit