0

I have a custom VF page. When the page is refreshed, i need to display custom message in page "There is no record to save". when i am clicking save button message will be displaying. But i need without clicking the save button i want to show custom message. Can anyone help? Thanks

Page:

<apex:page standardController="Account" extensions="myaccountcontroller">
<apex:form >
<apex:pagemessages ></apex:pagemessages>
    <apex:commandButton value="Save" action="{!Save}"/>
</apex:form>
</apex:page>

Controller:

public class myaccountcontroller
{
  public string accId {get;set;}
  public String finalamount {get;set;}

  public myaccountcontroller(ApexPages.StandardController sc)
  {
    accId = ApexPages.CurrentPage().getParameters().get('Id');

    doCalculation();
  }

  public void doCalculation()
  {
    Account AccObj = [SELECT Id,Name,Amount__c,Total_Amount__c FROM Account WHERE Id=:accId ];
    system.debug('TotalAmount ###:'+AccObj.Total_Amount__c );
   try{
        finalamount  = String.ValueOf(AccObj.Amount__c/0);
        system.debug('finalamount###:'+finalamount);
     }catch(Exception e)
     {
        if(e.getMessage() == 'System.NullPointerException: Attempt to de-reference a null object')
        {

          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Unite Price should not be empty'));
        }
     }
  }


}
0

You can use apex:pageMessage to display message on page load and then on button click you can hide this message.

<apex:page standardController="Account" extensions="myaccountcontroller">
<apex:form >
<apex:pageMessage summary="There is no record to save." severity="info" strength="3" rendered="{!messageflag}"/>

and in controller

public class myaccountcontroller
{
  public string accId {get;set;}
  public String finalamount {get;set;}
  public boolean messageflag {get;set;}  //declare the variable.

  public myaccountcontroller(ApexPages.StandardController sc)
  {
    accId = ApexPages.CurrentPage().getParameters().get('Id');
     messageflag  = true;
    doCalculation();
  }

  public void doCalculation()
  {
        messageflag  = false;
  • i want to display the custom error message on page when page is reloading, page contains only save button. However, i want to show the only custom error message on page at that time i want to hide save button. – Roman's YARD Dec 12 '18 at 13:24
  • @Roman'sYARD then you need to follow the same approach. Just render, rerender the section. Like on button click set the messageflag to false and hide the button and show the message. – Tushar Sharma Dec 13 '18 at 5:15
  • Thank you Tushar Sharma, its working fine. at the same time how can i redirect that page to another page in same method. – Roman's YARD Dec 13 '18 at 7:21
  • @Roman'sYARD you can use pagereference method for that. But if you redirect to new page then how you will show the message? – Tushar Sharma Dec 13 '18 at 8:15

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.