You need to sign in to do that
Don't have an account?
Amita 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.
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>
Can you check with this code,
Visualforce page
Thanks,
Prosenjit
All Answers
Try this class. Let me know if this helps.
can you try this for apex class,
and this for visualforce page,
Thanks,
Prosenjit
I tried with above solutions,but the messages is not getting displayed on the VF page.
Thanks,
Amita
System.debug(pfc);
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
Can you check with this code,
Visualforce page
Thanks,
Prosenjit
Thanks a lot.It worked.
-Amita
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
You have to create a save method and a add a pageblockbutton in visualforce
Thanks,
prosenjit