I am new to Salesforce, so i'd be glad, if I could get some help.
I am having a custom button, when I will click it, it'll check some condition, if it satisfied, then it will render a panel with an error message.
VF Page:
<apex:commandButton action="{!doSomething}" value="Save" onComplete="refreshTab({!isError});" />
JavaCsript :
<script type="text/javascript">
function refreshTab(isError) {
if (!isError) {
sforce.console.getEnclosingPrimaryTabId(refreshTabById);
}
}
function refreshTabById(tabId) {
if(sforce && sforce.console && sforce.console.isInConsole()) {
sforce.console.refreshPrimaryTabById(tabId.id, true);
}
}
</script>
Controller:
public PageReference doSomething(){
isError = false;
Date todayDate = System.today();
system.debug('Todat Date'+todayDate );
system.debug('Contact_Date'+caseObj.Next_Customer_Contact_Date__c); // CaseObject is an instance of an Object.
if (caseObj.Next_Customer_Contact_Date__c < todayDate) {
ApexPages.addMessage(new ApexPages.Message(
ApexPages.Severity.ERROR,
'Next Customer Contact Date must not be in the past'));
isError = true;
return null;
}
if(caseObj.Current_Situation__c != null){
caseObj.Current_Situation__c= caseObj.Current_Situation__c.substring(0, Math.min(caseObj.Current_Situation__c.length(), 255));
}
if(caseObj.Next_Steps__c != null){
caseObj.Next_Steps__c = caseObj.Next_Steps__c.substring(0, Math.min(caseObj.Next_Steps__c.length(), 255));
}
string myText = String.valueOf(
'Next Customer Contact Date: ' + caseObj.Next_Customer_Contact_Date__c.format())
+ '\n\nCurrent Situation: ' + caseObj.Current_Situation__c
+ '\n\nNext Steps: ' + caseObj.Next_Steps__c;
// Feeding to chatter
ConnectApi.ChatterFeeds.postFeedItem(null, ConnectApi.FeedType.Record, caseObj.Id, myText);
try{
update caseObj;
}catch (Exception ex) {
ApexPages.addMessage(new ApexPages.Message(
ApexPages.Severity.ERROR,
ex.getMessage()));
isError = true;
}
return null;
}
Attribution to: SFDC Geek
Possible Suggestion/Solution #1
You use apex page messages
<apex:pageMessages id="msg"/>
and on the button click rerender it.
There are two way to display message on visualforce page from controller
- Normal Case:
to display simple message follow this
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info,constantClass.'Hello'));
- Exception Case:
Try {
// your code
}catch(Exception e)
{
ApexPages.addMessages(e) ;
}
Please mind that there two method I have explained above
ApexPages.addMessage(pMessage)
& ApexPages.addMessages(pAPEX_OBJECT)
Attribution to: Sagar Pareek
Possible Suggestion/Solution #2
Set an id
for your outputPanel
and introduce a rerender
attribute to your commandButton
VF Page:
<apex:commandButton action="{!doSomething}" value="Save" rerender="myPanel" />
<apex:outputPanel id="myPanel" layout="block" rendered="{!isError}">
Error should go here in this panel
</apex:outputPanel>
Attribution to: highfive
Possible Suggestion/Solution #3
The platform has an error handling framework built in that is the easiest thing to use. Also if you do not use it you are in danger of failing to report errors to your user such as errors added in triggers.
With this in your page:
<apex:pageMessages/>
your controller will be able to add an error using code like this:
public PageReference doSomething() {
if (isError) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING,
'Error message'));
return null;
}
return ...;
}
and the apex:pageMessages
tag will output this error message and any others.
Attribution to: Keith C
Possible Suggestion/Solution #4
Do something like this
<apex:page controller="message">
<apex:pagemessages />
<apex:form >
<apex:pageblock >
<apex:commandButton action="{!doSomething}" value="Save" />
<apex:outputPanel layout="block" rendered="{!isError}">
<apex:pageMessage summary="This is a pageMessage" severity="error" strength="3"/>
</apex:outputPanel>
</apex:pageblock >
</apex:form >
public class message{
public boolean isError{get;set;}
public message()
{
isError=false;
}
public PageReference doSomething() {
if (condition) {
isError = true;
}
return null;
}
}
Attribution to: Salesforce developer
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33442